o19830015 2013-02-22 07:11 采纳率: 12.5%
浏览 15695
已采纳

结束activity中出现MediaPlayer异常

在结束mediaplayer activity时出现异常,代码:

结束按钮:

ImageView imageAllBack = (ImageView)this.findViewById(R.id.imageAllBack);
        imageAllBack.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {                   

                //mediaPlayer.stop();
                if(mediaPlayer != null)
                {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }

                 finish();

            }
          });

mediaPlayer事件:

@Override
  protected void onStop() {
    super.onStop();     
    mediaPlayer.stop();
    mediaPlayer.release();
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    mediaController.show();
    return false;
  }

  //--MediaPlayerControl methods----------------------------------------------------
  public void start() {
    mediaPlayer.start();
  }
  public void pause() {
    mediaPlayer.pause();
  }

  public int getDuration() {
    return mediaPlayer.getDuration();
  }

  public int getCurrentPosition() {

          return mediaPlayer.getCurrentPosition();
  }
  public void seekTo(int i) {
    mediaPlayer.seekTo(i);
  }

  public boolean isPlaying() {
    return mediaPlayer.isPlaying();
  }

  public int getBufferPercentage() {
    return 0;
  }

  public boolean canPause() {
    return true;
  }

  public boolean canSeekBackward() {
    return true;
  }

  public boolean canSeekForward() {
    return true;
  }
  //--------------------------------------------------------------------------------

  public void onPrepared(MediaPlayer mediaPlayer) {
    mediaController.setMediaPlayer(this);
    mediaController.setAnchorView(findViewById(R.id.main_audio_view));

    handler.post(new Runnable() {
      public void run() {
        mediaController.setEnabled(true);
        mediaController.show();
      }
    });
  }

  public class MyMediaController extends MediaController {

      public MyMediaController(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
          public void hide() {
              // Do Nothing to keep the show the controller all times
          }
      }

异常:

02-21 21:03:33.829: E/AndroidRuntime(8889): FATAL EXCEPTION: main
02-21 21:03:33.829: E/AndroidRuntime(8889): java.lang.IllegalStateException
02-21 21:03:33.829: E/AndroidRuntime(8889):     at android.media.MediaPlayer.isPlaying(Native Method)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at rce.android.naturalocal.NaturaPlayer.isPlaying(NaturaPlayer.java:126)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at rce.android.naturalocal.NaturaPlayer.getCurrentPosition(NaturaPlayer.java:115)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at android.widget.MediaController.setProgress(MediaController.java:436)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at android.widget.MediaController.access$500(MediaController.java:74)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at android.widget.MediaController$3.handleMessage(MediaController.java:407)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at android.os.Looper.loop(Looper.java:137)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at android.app.ActivityThread.main(ActivityThread.java:4441)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at java.lang.reflect.Method.invoke(Method.java:511)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-21 21:03:33.829: E/AndroidRuntime(8889):     at dalvik.system.NativeStart.main(Native Method)
  • 写回答

3条回答 默认 最新

  • franzhong 2013-02-23 06:02
    关注

    试下注释掉下句

    if(mediaPlayer != null)
                {
                    mediaPlayer.stop();
                    //mediaPlayer.release();
                }
    

    将下面另一个release用try catch括住

    try{
    mediaPlayer.release();
    }catch(Exception e){
    e.printStackTrace();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面