非法状态异常媒体播放器

时间:2012-12-12 06:31:32

标签: android media-player

我的活动中有一个按钮。点击按钮我正在呼叫finish(),问题是我正在接听Illegal state exception电话完成。请帮忙。

import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;

import com.mds.perfumastic.R;
import com.mds.perfumastic.constants.Constants;

public class AudioPlayerActivity extends Activity implements
    OnPreparedListener , MediaPlayerControl{
    private Handler handler = new Handler();
    private final MediaPlayer mediaPlayer = new MediaPlayer();
    private MediaController mediaController;
    private ProgressDialog progressBar;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audioplayer);
        progressBar =  new ProgressDialog(this);
        progressBar.setMessage("Playing...");
        progressBar.show();
        String audioUrl = getIntent().getStringExtra(Constants.INTENT_EXTRA_AUDIO_URL);
        mediaController = new MediaController(this);


        try {
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.reset();
            mediaPlayer.setDataSource(audioUrl);
            mediaPlayer.prepare();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @Override
    public void onPause() {
        super.onPause();
        progressBar.dismiss();
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer.release();
    }

    // --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(final MediaPlayer mediaPlayer) {
                Log.d("TAG", "onPrepared");
                progressBar.hide();
                mediaController.setMediaPlayer(this);
                mediaController.setAnchorView(findViewById(R.id.newView));
                mediaPlayer.start();

                handler.post(new Runnable() {
                    public void run() {
                        mediaController.setEnabled(true);
                        mediaController.show(mediaPlayer.getDuration());
                    }
                });
            }
            /** Use screen touches to toggle the video between playing and paused. */
            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.pause();
                    } else {
                        mediaPlayer.start();
                        mediaController.show(mediaPlayer.getDuration());
                    }
                    return true;
                } else {
                    return false;
                }
            }
            public void onClick(View v) {
                if(v.getId() == R.id.btn_back) {

                    finish();
                }
            }

}

这是日志消息:

12-12 11:55:34.791: E/AndroidRuntime(1336): FATAL EXCEPTION: main
12-12 11:55:34.791: E/AndroidRuntime(1336): java.lang.IllegalStateException
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.media.MediaPlayer.getCurrentPosition(Native Method)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at com.mds.perfumastic.activites.AudioPlayerActivity.getCurrentPosition(AudioPlayerActivity.java:80)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.widget.MediaController.setProgress(MediaController.java:415)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.widget.MediaController.access$500(MediaController.java:71)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.widget.MediaController$3.handleMessage(MediaController.java:386)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.os.Looper.loop(Looper.java:137)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at java.lang.reflect.Method.invoke(Method.java:511)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:4)

此异常的一个可能原因是MediaController可能仍在查询MediaPlayer对象的当前位置,即使您正在完成Activity并释放{{1对象。在执行MediaPlayer之前尝试添加mediaController.hide()

由于您正在呼叫mediaPlayer.stop(),因此您可能还想覆盖finish,因为onDestroy适用于onPause进入后台但仍然有效的情况。< / p>

答案 1 :(得分:1)

目前我正在开发一个使用MediaPlayer class的项目。当应用进入onDestroyonBackPressed状态时,我遇到了与您相同的错误。但是我已经调用了mp.release()方法来释放mediaplayer object.但是这还不够,我仍然遇到同样的错误。错误是由于我在getDuration()中使用的my Handler方法造成的。所以我只需在调用removeCallbacks()之前添加mp.release();方法:

mHandler.removeCallbacks(mUpdateTimeTask);

这里mHandler是我的Handler对象,mUpdateTimeTask是Runnable。

它对我有用!这样我就没有&#34; IllegalStateException&#34;错误。我希望这个解决方案有助于其