我試圖改變此項目的一些功能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,我面臨同樣的問題,請告訴我你是如何解決這個問題? –