我正在製作一首搖籃曲應用程序。在我的/ res/raw文件夾中有兩首曲目。 在完成第一首曲目後,我希望它播放下一首曲目。 我這樣做使用onCompletionListener.But它顯示力關閉錯誤。 以下是我的代碼。 那麼誰能告訴我該怎麼做?MediaPlayer播放來自/ res/raw的下一首曲目
public class PlayRandom extends Activity implements OnCompletionListener{
MediaPlayer mp;
int rnum;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.playrandom);
mp.setOnCompletionListener(this);
mp = MediaPlayer.create(this, R.raw.lullaby1);
mp.start();
}
@Override
public void finish() {
// TODO Auto-generated method stub
super.finish();
mp.stop();
}
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
File pdfFile = new File("res/raw/lullaby2.mp3");
Uri path = Uri.fromFile(pdfFile);
mp.stop();
try{
mp.setDataSource(getApplicationContext(), path);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我試過,但它仍然無法正常工作。 這個問題在OnClickListener中有問題。
public class PlayRandom extends Activity implements OnCompletionListener{
MediaPlayer mp;
int rnum;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.playrandom);
mp.setOnCompletionListener(this);
mp = MediaPlayer.create(this, R.raw.lullaby1);
mp.start();
}
@Override
public void finish() {
// TODO Auto-generated method stub
super.finish();
mp.stop();
}
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Toast.makeText(this,"Yuppieee!!", Toast.LENGTH_SHORT);
}
}
發佈錯誤日誌請 –