2

我的活动中有一个按钮。单击我正在呼叫的按钮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)
4

2 回答 2

4

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

由于您正在打电话finish,您可能还想覆盖onDestroy, sinceonPause用于当Activity进入后台但仍然存在时。

于 2012-12-12T07:14:20.347 回答
1

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

mHandler.removeCallbacks(mUpdateTimeTask);

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

它对我有用!这样我就永远不会遇到“IllegalStateException”错误。我希望这个解决方案可以帮助其他人。

于 2014-04-30T06:16:10.303 回答