Android media3 exoplayer, Play video from SD card or Raw directory
Asked Answered
B

1

6

I'm using Android new Media3 Exoplayer library following this code-lab, but there is no workaround to play video from the res/raw directory. My code for playing from the res/raw directory is

    val uri = RawResourceDataSource.buildRawResourceUri(R.raw.my_video)
    val mediaItem = MediaItem.fromUri(uri)
    exoPlayer.setMediaItem(mediaItem)

The above code is not working and if I create MediaItem with an mp4 URL then it is working. Code for mp4 URL is

    val mediaItem = MediaItem.fromUri(getString(R.string.media_url_mp4))
    exoPlayer.setMediaItem(mediaItem)

Can anyone please help me how to resolve this issue?

Thanks in advance!

Boggs answered 30/8, 2022 at 6:53 Comment(0)
C
0

Here is the code that is working for me.

  1. Code for video from HTTP URL.

    val player: ExoPlayer = ExoPlayer.Builder(requireContext()).build()
    val mediaItem = MediaItem.Builder().setUri("https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4").build()
    binding.videoPlayerMedia3.player = player
    player.addMediaItem(mediaItem)
    player.play()
    

2.Code for video from Raw folder.

     val videoUri = RawResourceDataSource.buildRawResourceUri(R.raw.test_video_2)
     val mediaItem = MediaItem.fromUri(videoUri)
Crashing answered 15/9, 2023 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.