Search code examples
androidffmpegexoplayermediastoreandroid-11

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


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.