Home > Software design >  Android: How to open a video file using MediaStore with ExoPlayer?
Android: How to open a video file using MediaStore with ExoPlayer?

Time:12-26

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.

CodePudding user response:

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.

CodePudding user response:

You got an Uri from MediaStore.

Then open an InputStream for it:

InputStream is = getContentResolver().openInputStream(uri);

Then give Exoplayer your stream.

O sorry... after reread i see that i misreaded.. sorry.

But then.. which methods does Exoplayer have?

  • Related