//這是編輯的代碼工作:的Android的MediaPlayer播放後的第一時間停止
我一直在試圖尋找不同的方式來解決它,但它看起來像我要尋求幫助。
媒體播放器第一次播放該文件,從不再次播放。 這是代碼:
btn_default_acc = (ImageButton) layout.findViewById(R.id.btn_df_acc);
btn_default_acc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {if(sentencePlayer==null){
sentencePlayer = new MediaPlayer();
}sentencePlayer.reset();
sentencePlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
sentencePlayer.prepare();
sentencePlayer.start();
} catch (IllegalArgumentException 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();
}
}
});
sentencePlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer sentencePlayer) {
sentencePlayer.stop();
}
});
和logcat的: 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: java.lang.IllegalStateException 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at android.media.MediaPlayer._setDataSource(Native Method) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1254) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at com.testapp.imre.testapp.Lesson1Reading$11.onClick(Lesson1Reading.java:1134) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at android.view.View.performClick(View.java:5156) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at android.view.View$PerformClick.run(View.java:20755) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at android.os.Handler.handleCallback(Handler.java:739) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at android.os.Looper.loop(Looper.java:145) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5832) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at java.lang.reflect.Method.invoke(Native Method) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at java.lang.reflect.Method.invoke(Method.java:372) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 01-11 13:28:36.744 9044-9044/com.testapp.imre.testapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
我還修改了關閉屏幕的方法:
private View.OnClickListener close_popup_screen_listener = new View.OnClickListener() { public void onClick(View v) { if (sentencePlayer != null){ sentencePlayer.stop(); } popUpScreen.dismiss(); try { fd.close(); } catch (IOException e) { e.printStackTrace(); } stopMediaRecorder(); } };
當你釋放一個MediaPlayer實例後,它不再可用。另外,如果你調用'reset()'(但不是'release()'),你必須重新初始化它,然後再使用它。 –
我修改了上面的代碼,但仍然收到錯誤:android.media.MediaPlayer._prepare(Native Method)的java.lang.SecurityException – Immy
因爲關閉了'FileDescriptor',所以出現了這個錯誤。 –