I want to use exoPlayer to play local files(in internal or external storage). I have tried some code but didn't find anything useful. code:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
String filePath = Constant.allMediaList.get(position).toString();
Uri videoUrl = Uri.parse(filePath);
Log.d("filePathLocation",filePath);
simpleExoPlayer = new SimpleExoPlayer.Builder(this).build();
DataSpec dataSpec = new DataSpec(videoUrl);
final FileDataSource fileDataSource = new FileDataSource();
try {
fileDataSource.open(dataSpec);
} catch (FileDataSource.FileDataSourceException e) {
e.printStackTrace();
}
DataSource.Factory factory = new DataSource.Factory() {
@Override
public DataSource createDataSource() {
return fileDataSource;
}
};
simpleExoPlayer.prepare(buildMediaSourceNew(videoUrl));
playerView.setPlayer(simpleExoPlayer);
simpleExoPlayer.setPlayWhenReady(true);
buildMediaSourceNew():
private MediaSource buildMediaSourceNew(Uri uri){
DataSource.Factory datasourceFactroy = new DefaultDataSourceFactory(this,
Util.getUserAgent(this,"Your App Name"));
return new ExtractorMediaSource.Factory(datasourceFactroy).createMediaSource(uri);
}
after using this code some files can be played and some not. For example:
1st file Location:
/storage/emulated/0/Download/Short Animated Film.mp4
2nd file location:
/storage/emulated/0/browser/#1 Django tutorials _ What is Django_ _ Python Web Framework.mp4
1st file can be played but for 2nd file below errors occured.
if file name contains "#" or "?" exoplayer can't play it.
2020-09-18 20:40:56.985 10985-11347/com.example.learning E/ExoPlayerImplInternal: Source error
com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: uri has query and/or fragment, which are not supported. Did you call Uri.parse() on a string containing '?' or '#'? Use Uri.fromFile(new File(path)) to avoid this. path=/storage/emulated/0/browser/,query=null,fragment=1 Django tutorials _ What is Django_ _ Python Web Framework.mp4
at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:97)
at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:177)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:956)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: uri has query and/or fragment, which are not supported. Did you call Uri.parse() on a string containing '?' or '#'? Use Uri.fromFile(new File(path)) to avoid this. path=/storage/emulated/0/browser/,query=null,fragment=1 Django tutorials _ What is Django_ _ Python Web Framework.mp4
at com.google.android.exoplayer2.upstream.FileDataSource.openLocalFile(FileDataSource.java:112)
at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:88)
at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:177)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:956)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.io.FileNotFoundException: /storage/emulated/0/browser: open failed: EISDIR (Is a directory)
at libcore.io.IoBridge.open(IoBridge.java:485)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:288)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:151)
at com.google.android.exoplayer2.upstream.FileDataSource.openLocalFile(FileDataSource.java:108)
at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:88)
at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:177)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:956)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: android.system.ErrnoException: open failed: EISDIR (Is a directory)
at libcore.io.IoBridge.open(IoBridge.java:475)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:288)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:151)
at com.google.android.exoplayer2.upstream.FileDataSource.openLocalFile(FileDataSource.java:108)
at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:88)
at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:177)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:956)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
How to avoid this error?