我试图改变此项目的一些功能Media player从原始文件夹而不是从SD卡中读取mp3。但它会引发错误“尝试在没有有效的媒体播放器的情况下调用getDuration”请给出任何建议?尝试在没有有效媒体播放器的情况下调用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给我回导致演员,但它不是重要的原料来源设置staticly
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);
}
}
你在哪里运行'playSong'方法? –
代码更新看 – Yurdaaaaa
@Yurdaaaaa,我面临同样的问题,请告诉我你是如何解决这个问题? –