Android MediaPlayer在第一次播放后停止

时间:2016-01-11 03:37:14

标签: android listener media-player android-mediaplayer file-descriptor

//这是编辑后的代码:

我一直试图找到不同的解决方法,但看起来我不得不寻求帮助。

媒体播放器第一次播放文件而不再播放。 这是代码:

function signForCurrency(currencyCode) {
  var data =
    Object.values(countryMap).find(data => data.currencyCode == currencyCode)
  return data ? data.currencySign : undefined;
}

和logcat: 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(); } });

我还修改了关闭屏幕的方法:

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)

2 个答案:

答案 0 :(得分:0)

请你删除以下两行吗?

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();
}

希望这会对你有所帮助。

答案 1 :(得分:0)

尝试添加sentencePlayer.reset();

在setDataSource之前