gpt4 book ai didi

java - MediaPlayer 错误 - getCurrentPosition

转载 作者:太空宇宙 更新时间:2023-11-04 14:41:25 25 4
gpt4 key购买 nike

请教我如何解决 MediaPlayer 错误。我有 Mediaplayer、MediaController 和 listview。当我触摸列表时,歌曲会从 MediaController 开始,但是当我触摸 keycode_home 并重新启动我的应用程序时,会发生错误。这是我的代码和错误。

    java.lang.RuntimeException: Unable to resume activity {}: 
java.lang.IllegalStateException
Caused by: java.lang.IllegalStateException at android.media.MediaPlayer.getCurrentPosition(Native Method)


int si[]={R.raw.sample1,R.raw.sample2,R.raw.sample3};
onCreate

ItemBean IB1 = new ItemBean();
IB1.setName("sample1");
IB1.setUrl("http~");

ItemBean IB2 = new ItemBean();
IB2.setName("sample2");
IB2.setUrl("http~");

ItemBean IB3 = new ItemBean();
IB3.setName("sample3");
IB3.setUrl("http~");

List<ItemBean> list = new ArrayList<ItemBean>();
list.add(IB1);
list.add(IB2);
list.add(IB3);

mp = new MediaPlayer();
controller = new MediaController(this);
controller.setAnchorView(findViewById(R.id.mediaController));
controller.setMediaPlayer(this);

list2 = (ListView) findViewById(R.id.song_list);
SonglistAdapter_test adapter = new SonglistAdapter_test(getApplicationContext(),list);
list2.setAdapter(adapter);
list2.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

try {
mp.reset();
mp.release();
mp = MediaPlayer.create(SongsActivity.this,si[position]);
} catch (Exception e) {}

mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mediaPlayer) {
handler.post(new Runnable() {
@Override
public void run() {
controller.setEnabled(true);
controller.show(mp.getDuration());
mp.start();
try {
Method m = android.widget.MediaController.class.getDeclaredMethod("updatePausePlay");
m.setAccessible(true);
m.invoke(controller);
} catch (Exception e) {
}
}
});
}
});
}
@Override
public void start(){
mp.start(); }

@Override
public void pause(){
mp.pause(); }

@Override
public int getDuration(){
return mp.getDuration(); }

@Override
public int getCurrentPosition(){
return mp.getCurrentPosition(); }

@Override
public void seekTo(int pos){
mp.seekTo(pos); }

@Override
public boolean isPlaying(){
return mp.isPlaying(); }

@Override
public int getBufferPercentage(){
return 0; }

@Override
public boolean canPause(){
return true; }

@Override
public boolean canSeekBackward(){
return true; }

@Override
public boolean canSeekForward(){
return true; }

@Override
public int getAudioSessionId(){
return 0; }


@Override
protected void onPause(){
super.onPause();
}

@Override
protected void onResume(){
super.onResume();
}

@Override
protected void onStop(){
super.onStop();
controller.hide();
mp.stop();
mp.release();
}

@Override
public void onRestart(){
super.onRestart();
controller.show();
}

@Override
protected void onDestroy(){
super.onDestroy();
finish();
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.dispatchKeyEvent(event);
}

最佳答案

问题出在您的 onStop() 方法中 - 您正在那里释放媒体播放器,并且在 onRestart() 期间显示 Controller 之前没有重新创建它。 onStop() 方法与 onDestroy() 不同 - onStop() 并不意味着您的 Activity 一定会运行恢复时再次onCreate()

您应该看看 Activity Lifecycle更好地了解这些事件何时发生。

关于java - MediaPlayer 错误 - getCurrentPosition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24892707/

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