0

I have a video under firebase storage.And I want to display it on the screen.According to my research some sources say it's not possible to display video from firebase storage.But chat gpt says it's possible and it gave me a code to implement to my code.I implemented the code but when I try to display it,it gives me a dialog box which says "can't play this video". This is what I have see when I try to run video

And here is what is my log When I ask about this error to chat gpt.It says it may be happen by wrong path name,wrong video format and etc.Even my path is correct and video format is .mp4 I still can't display to video. Here is where my video located My first question: Is it possible to display video from firebase storage as chat gpt says?(Some sources says it's not possible, that's why I wanted to ask that)

Second question: if it's not possible what can be the solution for my case?I mean what can I do for displaying video without downloading or storing in app.Just getting videos from Cloud storage.

Third question: When I asked that to chat gpt.It says that problem may occurs when file path is wrong,video format is wrong and etc. In my case my filepath is correct probably.Beacuse my video is under "videos" folder and name is video.mp4.Only I am not sure if .mp4 is suitable for that usage.I guess it is not but I just wanted to be sure.

Here is my Kotlin code

package com.example.parkinsonvethaichi

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.MediaController
import android.widget.VideoView
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import com.google.firebase.storage.FirebaseStorage
import com.google.firebase.storage.ktx.storage
import kotlinx.android.synthetic.main.activity_movements.*

class Movements : AppCompatActivity() {
    private lateinit var db : FirebaseFirestore
    private lateinit var auth : FirebaseAuth
    private lateinit var storage:FirebaseStorage

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_movements)
        var actionBar=supportActionBar
        actionBar?.title="Tai Chi Movements"
        


        val videoPath = "gs://tai-chi-ve-parkinson.appspot.com/videos/video.mp4"
        val videoUri = Uri.parse(videoPath)
        val mediaController = MediaController(this)
        main_video_view.setMediaController(mediaController)
        main_video_view.setVideoURI(videoUri)
        main_video_view.start()


    }
    override fun onBackPressed() {
        val intent = Intent(this, HomeActivity::class.java)
        startActivity(intent)
        finish()
    }
}

I tried to change the rule of storage for accesing and reading there like this in firebase

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read;
    }
  }
}

enter image description here

I also find some tutorials about this.Even they didn't fix my problem.They were adding some permissions to manifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

I am not sure if that permissions are neccesarry.I just wanted to add that anyways.

Edit:I guess I can't add my storage to SDK even I press the button it doesn't change.Button doesn't change to green tick.

button not green tick

Edit2:I solved that problem it caused by wrong version implemantations.But still have same error as can't play this video. enter image description here

I wish I can find a solution on here.

Thanks to everyone who helps in advance.

Ömer
  • 13
  • 5
  • If the path is ok then why dont you show us how it looks like? And why didnt you tell how you checked that it was ok? – blackapps May 11 '23 at 16:42
  • Firebase? https://stackoverflow.com/questions/38806490/what-does-gs-protocol-mean – blackapps May 11 '23 at 16:47
  • @blackapps I added it's location with screenshot.You can check it. – Ömer May 11 '23 at 17:08
  • Know how you are getting the file, see [Enforce authentication for firebase-storage downloadable urls](https://stackoverflow.com/a/41129510/295004) – Morrison Chang May 11 '23 at 19:33
  • @MorrisonChang It didn't help me.I just want to acces my video.Even I try uri with https,it's still same. – Ömer May 12 '23 at 13:49
  • Right now you aren't giving enough info for anyone to help. I don't see you use the Firebase SDK from Android to access cloud storage so (a) you need to access the file using the Firebase SDK with credentials or (b) change the permissions on your cloud storage to allow access by anything with the video file URL. – Morrison Chang May 13 '23 at 03:30
  • @MorrisonChang Yeah you are right indeed I couldn't do that I guess.Cause when I click the button for Add Cloud Storage to your app.It says me it will add some implementations to build gradle .Even I accept the changes.It adds that implementations.But the button not be green tick.I supposed that is kinda a bug,it adds the implemantations anyways.But the problem is on here I guess.Is there anyway that I can solve that? I added my screenshots about it also. – Ömer May 13 '23 at 09:49
  • @MorrisonChang I fixed that problem.Dependencies set up correctly now.It caused by wrong version implemantation.But still I have same problem.Can't play this video. – Ömer May 13 '23 at 10:04
  • You've added the SDK but not showing code that you are using it. Just making sure you've seen/following: https://firebase.google.com/docs/storage/android/start I would start with being able to show a simple text file from cloud storage in android app then add in video playing to identify where the any problem is. Good luck. – Morrison Chang May 13 '23 at 20:39
  • @MorrisonChang I guess already showed my code below.I mean which code you want to see I am not sure but thanks for advices anyways. – Ömer May 14 '23 at 06:29
  • Please read: https://stackoverflow.com/a/41767410/295004 as you are under the belief that any code can read a 'gs://' prefixed file. – Morrison Chang May 14 '23 at 06:37
  • @MorrisonChang I have no belief like that.I mean I am just trying to find correct way.I am not saying it should work with 'gs://'.I just don't know what I should I do really. – Ömer May 15 '23 at 08:35

2 Answers2

0

Seems like you are missing setMediaPlayer()

Please use the code below

 val videoPath = "gs://tai-chi-ve-parkinson.appspot.com/videos/video.mp4"
        val videoUri = Uri.parse(videoPath)
        main_video_view.setVideoURI(videoUri)
        val mediaController = MediaController(this)
        mediaController.setMediaPlayer(main_video_view)
        main_video_view.setMediaController(mediaController)
        main_video_view.start()

Also answering the questions you mentioned :

  1. Yes it is possible to stream the video from cloud storage to Android.
  2. Already answered.
  3. Path may be wrong? I am not sure but please check if you have access right for that video ( i.e, you are able to authenticate and get the video from your Firebase project's Storage )
oyeraghib
  • 878
  • 3
  • 8
  • 26
  • I tried but got same error again.And also saw Failed to open file 'gs://tai-chi-ve-parkinson.appspot.com/videos/video.mp4'. (No such file or directory). error on logs.Is it about connecting firebase? – Ömer May 11 '23 at 20:52
  • I also added permissions to allow read; method.But it's still same – Ömer May 12 '23 at 13:50
0

It's all about uri.I solved it. "gs://tai-chi-ve-parkinson.appspot.com/videos/video.mp4" uri was wrong for me and I was having can't play this video diaolog box. So I used "https://tai-chi-ve-parkinson.appspot.com/videos/video.mp4" or just "videos/video.mp4" is enough too. I dislpayed it on videoview.But I needed another displays so I changed it to exoplayer.

        val storageRef = Firebase.storage.reference
        val videoRef = storageRef.child("videos/video.mp4")
        simpleExoplayer=SimpleExoPlayer.Builder(this).
    videoRef.downloadUrl.addOnSuccessListener { uri ->
                val videoUrl = uri.toString()
         
                val videoUri = Uri.parse(videoUrl)
                val mediaItem=MediaItem.fromUri(videoUri)
                simpleExoplayer.setMediaItem(mediaItem)
                simpleExoplayer.prepare()
    
                simpleExoplayer.play()
    
                
            }.addOnFailureListener { exception ->

            }
Ömer
  • 13
  • 5