Home > Software design >  How can I use video in raw folder instead of the the videoUrl
How can I use video in raw folder instead of the the videoUrl

Time:10-10

VideoModel: Project video model is here,

data class VideoModel(
    var videoTitle:String,
    var videoDesc:String,
    var videoUrl:String)

and I have video url below, but I want to use the video in raw folder instead of the url, is it possible?

   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        arrVideoModel.add(VideoModel("Tree with flowers","The branches of a tree wave in the breeze, with pointy leaves ","https://www.youtube.com/watch?v=mncFy1H7OUM"))
   
        videoAdapter = VideoAdapter(arrVideoModel)
        viewPager.adapter = videoAdapter

    }

CodePudding user response:

Yes, it is possible to use a video from raw folder using ExoPlayer's RawResourceDataSource.buildRawResourceUri() method.

Here is a sample I have used to achieve the same:

Add the ExoPlayer dependency in your build.gradle

implementation 'com.google.android.exoplayer:exoplayer-core:2.14.1'

Get the Uri by using RawResourceDataSource.buildRawResourceUri()

val videoUri: Uri = RawResourceDataSource.buildRawResourceUri(R.raw.video_name)
arrVideoModel.add(VideoModel("Tree with flowers","The branches of a tree wave in the breeze, with pointy leaves ", videoUri.toString()))

This will give you Uri of the video you have added in the raw folder.

  • Related