Android: How to open a video file using MediaStore with ExoPlayer?

0

Issue

I am trying to play the local video files using MediaStore API but all I see is I can access it as an OpenFileDiscriptor object or as InputStream.
But ExoPlayer doesn’t have any of these methods to support.
So now how can I open this file using MediaStore API and Play it with Android 10+?

Also, does anyone knows this as well how can I create and save a video with Native Libs? such as FFmpeg using MediaStore API.

Solution

This is to get Content Uri

val contentUri: Uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI

Get the Cursor

...

val cursor: Cursor? = contentResolver.query(contentUri, projection,
    selection,
    selectionArgs, sortOrder)
...

Get the id of the file

...
val id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID))
...
val fileUri = ContentUris.withAppendedId(
                        MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        id
                    )

Using this uri, create exoplayer MediaSource

val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(
            context,
            "exoplayer-example"
        )
val mediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(fileUri)

Refer this and this.

Please post the second part as a separate question, I haven’t used any native libs with MediaStore APIs. Perhaps returned uri can be used.

Answered By – Varsha Kulkarni

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More