rentPosition时在MediaPlayer中获取Ille

rentPosition时在MediaPlayer中获取Ille

本文介绍了尝试获取CurrentPosition时在MediaPlayer中获取IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使轨迹栏与刚开始使用的音乐播放器一起使用.但是,当我按下一步时,会出现异常.这是我的代码:

I've been trying to make my trackbar work with the music player I just started working on. But when I press next, I get an Exception.Here's my code:

public class ThePlayer extends AppCompatActivity {

  private static final String TAG = "MyActivity";
  static MediaPlayer mp;
  int position;
  SeekBar mSeekBar;
  Button btnPlay, btnFF, btnFB, btnNext, btnPrev;
  int[] w2 = {R.raw.w2l01f01, R.raw.w2l01f03, R.raw.w2l01f04};
  ArrayList<Integer> mysongs;
  Thread updateSeekBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_the_player);
    position = 0;
    btnPlay = (Button) findViewById(R.id.btnPlay);
    btnPlay.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            if (mp.isPlaying()) {
                btnPlay.setText(">");
                mp.pause();
            } else {
                btnPlay.setText("||");
                mp.start();
            }
        }
    });
    btnNext = (Button) findViewById(R.id.btnNext);
    btnNext.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            mp.stop();
            mp.release();
            position = (position + 1) % w2.length;
            try {
                mp = MediaPlayer.create(getApplicationContext(), w2[position]);
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
            } catch (IllegalStateException e) {
                Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
            }
            mSeekBar.setMax(mp.getDuration());
            mp.start();
        }
    });

    btnPrev = (Button) findViewById(R.id.btnPrev);

    if (mp != null) {
        mp.stop();
        mp.release();
    }
    mSeekBar = (SeekBar) findViewById(R.id.seekBar);
    updateSeekBar = new Thread() {
        @Override
        public void run() {
            int currentPostion = 0;
            while (mp.isPlaying() && mp != null && mp.getCurrentPosition() < mp.getDuration()) {
                try {
                    sleep(500);

                    currentPostion = mp.getCurrentPosition();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {

                    e.printStackTrace();
                }
                mSeekBar.setProgress(currentPostion);
            }
        }

    };





    mp = MediaPlayer.create(this, w2[0]);
    mp.start();
    mSeekBar.setMax(mp.getDuration());
    updateSeekBar.start();
    mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            mp.seekTo(seekBar.getProgress());
        }

    });

}

}

每次我单击下一步按钮(btnNext)时,都会出现此错误

Everytime I click on the button next (btnNext), I get this Error

01-04 08:45:01.028 1567-1585/com.wizardarapiraca.wizardplayer E/AndroidRuntime: FATAL EXCEPTION: Thread-93
01-04 08:45:01.028 1567-1585/com.wizardarapiraca.wizardplayer E/AndroidRuntime: Process: com.wizardarapiraca.wizardplayer, PID: 1567
01-04 08:45:01.028 1567-1585/com.wizardarapiraca.wizardplayer E/AndroidRuntime: java.lang.IllegalStateException
01-04 08:45:01.028 1567-1585/com.wizardarapiraca.wizardplayer E/AndroidRuntime:     at android.media.MediaPlayer.getCurrentPosition(Native Method)
01-04 08:45:01.028 1567-1585/com.wizardarapiraca.wizardplayer E/AndroidRuntime:     at com.wizardarapiraca.wizardplayer.ThePlayer$4.run(ThePlayer.java:107)

推荐答案

来自评论:

您可以通过将其删除到语句mp.stop(); mp.release();并添加mp.pause();

You can try it by removing these to statement mp.stop(); mp.release(); and add mp.pause();

在此状态下,媒体播放器无法停止和释放.因为...

At this state media player can not stop and release.because...

当刚使用new创建MediaPlayer对象时,或者在调用reset()之后,它处于Idle状态;在release()被调用之后,它处于End状态.

When a MediaPlayer object is just created using new or after reset() is called, it is in the Idle state; and after release() is called, it is in the End state.

调用setDataSource(FileDescriptor)setDataSource(String)setDataSource(Context, Uri)setDataSource(FileDescriptor, long, long)setDataSource(MediaDataSource)会将处于空闲状态的MediaPlayer对象转移到初始化"状态.

Calling setDataSource(FileDescriptor), or setDataSource(String), or setDataSource(Context, Uri), or setDataSource(FileDescriptor, long, long), or setDataSource(MediaDataSource) transfers a MediaPlayer object in the Idle state to the Initialized state.

如果在任何其他状态下调用setDataSource(),都会抛出IllegalStateException.更多

An IllegalStateException is thrown if setDataSource() is called in any other state.for more

这篇关于尝试获取CurrentPosition时在MediaPlayer中获取IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 04:30