我已经集成了ExoPlayer,我写了一种在其中播放字幕的方法,但是ExoPlayer抛出此错误而不是加载,所以需要帮助!
我的想法:用户可以从网上下载字幕(.srt格式),当他们单击我编写的“ imgSubtitle”图像按钮时,它应该打开用户的存储空间,从中可以选择字幕文件,然后加载并在ExoPlayer中显示。
错误日志:
E/ExoPlayerImplInternal: Source error.
com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: java.io.FileNotFoundException: /root/storage/emulated/0/Download/subtitle.srt: open failed: ENOENT (No such file or directory)
at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:70)
at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:147)
at com.google.android.exoplayer2.source.SingleSampleMediaPeriod$SourceLoadable.load(SingleSampleMediaPeriod.java:355)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:308)
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:919)
Caused by: java.io.FileNotFoundException: /root/storage/emulated/0/Download/subtitle.srt: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:496)
at java.io.RandomaccessFile.<init>(RandomaccessFile.java:289)
at java.io.RandomaccessFile.<init>(RandomaccessFile.java:152)
at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:62)
at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:147)
at com.google.android.exoplayer2.source.SingleSampleMediaPeriod$SourceLoadable.load(SingleSampleMediaPeriod.java:355)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:308)
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:919)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
at android.app.activityThread$AndroidOs.open(activityThread.java:7581)
at libcore.io.IoBridge.open(IoBridge.java:482)
at java.io.RandomaccessFile.<init>(RandomaccessFile.java:289)
at java.io.RandomaccessFile.<init>(RandomaccessFile.java:152)
at com.google.android.exoplayer2.upstream.FileDataSource.open(FileDataSource.java:62)
at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:147)
at com.google.android.exoplayer2.source.SingleSampleMediaPeriod$SourceLoadable.load(SingleSampleMediaPeriod.java:355)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:308)
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:919)
我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imgSubtitle.setOnClicklistener(new View.OnClicklistener() {
@Override
public void onClick(View v) {
Intent fileintent = new Intent(Intent.actION_GET_CONTENT);
fileintent.setType("gagt/sdf");
try {
startactivityForResult(fileintent,PICKFILE_RESULT_CODE);
} catch (activityNotFoundException e) {
Log.e("tag","No activity can handle picking a file. Showing alternatives.");
}
}
});
}
protected void onactivityResult(int requestCode,int resultCode,Intent data) {
// TODO Fix no activity available
super.onactivityResult(requestCode,resultCode,data);
if (data == null)
return;
switch (requestCode) {
case PICKFILE_RESULT_CODE:
if (resultCode == RESULT_OK) {
String FilePath = data.getData().getPath();
setSubSelectedSubtitle(mediaSource,FilePath,this);
//FilePath is your file as a string
}
}
}
public void setSubSelectedSubtitle(MediaSource mediaSource,String FilePath,Context context) {
MergingMediaSource mergedSource;
if (FilePath != null) {
Uri subtitleUri = Uri.parse(FilePath);
Format subtitleFormat = Format.createTextSampleFormat(
null,// An identifier for the track. May be null.
MimeTypes.TEXT_VTT,// The mime type. Must be set correctly.
Format.NO_VALUE,// Selection flags for the track.
"en"); // The subtitle language. May be null.
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(context,Util.getUserAgent(context,getResources().getString(R.string.app_name)),new DefaultBandwidthMeter());
MediaSource subtitleSource = new SingleSampleMediaSource
.Factory(dataSourceFactory)
.createMediaSource(subtitleUri,subtitleFormat,C.TIME_UNSET);
mergedSource = new MergingMediaSource(mediaSource,subtitleSource);
player.prepare(mergedSource,false,false);
//resumePlayer();
} else {
Toast.makeText(context,"there is no subtitle",Toast.LENGTH_SHORT).show();
}
}
感谢您的帮助,谢谢!