//这是经过编辑的有效代码:
我一直在试图找到不同的方法来修复它,但看起来我必须寻求帮助。
媒体播放器第一次播放该文件,以后再也不会播放。这是代码:
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();
}
};
尝试添加sentencePlayer.reset();
在设置数据源之前
你能去掉下面两行吗?
sentencePlayer.reset();
sentencePlayer.release();
你的方法应该如下所示:
sentencePlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer sentencePlayer) {
sentencePlayer.stop();
}
});
编辑1:
删除以下行:
sentencePlayer.reset();
在行下添加:
if(sentencePlayer==null){
sentencePlayer = new MediaPlayer();
}
希望这对你有帮助。