gpt4 book ai didi

android - 尝试获取 CurrentPosition 时在 MediaPlayer 中获取 IllegalStateException

转载 作者:行者123 更新时间:2023-11-29 20:12:08 24 4
gpt4 key购买 nike

我一直在尝试让我的轨迹栏与我刚开始使用的音乐播放器一起工作。但是当我按下一个时,我得到一个异常。这是我的代码:

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),我都会得到这个错误

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();

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

当MediaPlayer对象刚用new创建或调用reset()后,处于Idle状态; release() 调用后,处于 End 状态。

调用setDataSource(FileDescriptor),或setDataSource(String),或setDataSource(Context, Uri),或setDataSource( FileDescriptor, long, long),或者setDataSource(MediaDataSource)将Idle状态的MediaPlayer对象转为Initialized状态。

如果在任何其他状态下调用 setDataSource(),将抛出 IllegalStateExceptionfor more

关于android - 尝试获取 CurrentPosition 时在 MediaPlayer 中获取 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34592596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com