请教我如何解决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);
}
答案 0 :(得分:0)
问题在于您的onStop()
方法 - 您正在释放媒体播放器而不是在onRestart()
期间显示控制器之前重新创建媒体播放器。 onStop()
方法与onDestroy()
不同 - onStop()
并不代表您的活动在恢复时必须再次通过onCreate()
。
您应该查看Activity Lifecycle,以便更好地了解这些事件何时发生。