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