程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> mediaplayer-結束activity中出現MediaPlayer異常

mediaplayer-結束activity中出現MediaPlayer異常

編輯:編程綜合問答
結束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)

最佳回答:


試下注釋掉下句

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

將下面另一個release用try catch括住

try{
mediaPlayer.release();
}catch(Exception e){
e.printStackTrace();
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved