我在媒体播放器上实现了一个文件选择器,它返回外部SD 上.mp3 和.srt 的文件路径。音频播放良好。但是,当我使用 .srt 的路径调用 addTimedTextSource 时,它会引发空指针异常。所以,我输入了一个 If(file.exists)。它还返回一个空值。我尝试将文件移动到内部 SD,结果相同。有任何想法吗?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
buttonPause = (Button) findViewById(R.id.buttonPause);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
Bundle bundle = getIntent().getExtras();
if(bundle!=null) {
String removeString = "file:";
soundPath = bundle.getString("soundFile");
subPath = bundle.getString("subFile");
subPath = removeString(subPath,removeString);
soundFile = new File(soundPath);
subFile = new File(subPath);
}
player = new MediaPlayer();
try {
player.setDataSource(soundPath);
player.setOnErrorListener(this);
player.setOnPreparedListener(this);
player.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onPrepared(MediaPlayer mp){
mp.setOnTimedTextListener(this);
if(subFile.exists() {
try {
player.addTimedTextSource(subFile.getAbsolutePath(), MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
} catch (IOException e) {
Log.v("Hey Here is a Problem: ", e.getMessage());
}
TrackInfo[] ti = player.getTrackInfo();
for (int i = 0; i < ti.length; i++) {
if (ti[i].getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT) {
player.selectTrack(i);
break;
}
}
}else{
onBackPressed();
}
mp.start();
}