我试图改变这个项目媒体播放器的一些功能,从原始文件夹而不是从 SD 卡中读取 mp3。但它给我一个错误“尝试在没有有效媒体播放器的情况下调用getDuration”请任何建议?
public void playSong(int songIndex){ // parameter is not used. is set static content
try {
mp.reset();
mp = mp.create(this, R.raw.ledzepellin); // static content!
mp.prepare();
mp.start();
String songTitle = "title";
songTitleLabel.setText(songTitle);
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mp.getDuration(); // ---- HERE IT GIVE ME ERROR
long currentDuration = mp.getCurrentPosition();
songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
songProgressBar.setProgress(progress);
mHandler.postDelayed(this, 100);
}
};
在这里,我开始 playSong 方法,它首先打开我一个可用歌曲的列表视图,当我选择 ti 时给我返回结果,但它不重要的原始源是静态设置的
btnPlaylist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), PlayListActivity.class);
startActivityForResult(i, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100){
currentSongIndex = data.getExtras().getInt("songIndex");
playSong(currentSongIndex);
}
}