简体   繁体   中英

Exoplayer set mediaSource using ContentResolver#openFileDescriptor(Uri, String) , instead of Uri.parse(MediaStore.Audio.Media.DATA)

How to pass uri to mediaSource in Android Q since MediaStore.Audio.Media.DATA is deprecated

Earlier (Below Api 29 / Android 10/ Q) This is what i was using

mediaSource = new ProgressiveMediaSource. Factory(dataSourceFactory).createMediaSource(Uri.parse(SongsForQueue.get(i)._path)); simpleExoPlayer.prepare(mediaSource); simpleExoPlayer.setPlayWhenReady(true);

Where SongsForQueue.get(i)._path was the path of the file which we got from MediaStore.Audio.Media.DATA since Android Q deprecated MediaStore.Audio.Media.DATA what can we use

Android suggests

https://stackoverflow.com/a/59037794

use

ContentResolver#openFileDescriptor(Uri, String).

how can we do that any help would be useful

I solved this:

long mediaId = //composition id in media store
Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mediaId);

DataSpec dataSpec = new DataSpec(uri);
ContentDataSource dataSource = new ContentDataSource(context);
dataSource.open(dataSpec);

DataSource.Factory factory = () -> dataSource;
MediaSource mediaSource = new ProgressiveMediaSource.Factory(factory).createMediaSource(uri);
player.prepare(mediaSource);//your player

Exoplayer version: 2.11.3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM