我已经实现了媒体播放器。我只有一个活动。当我开始活动时,我的音乐开始播放,当我按下后退按钮时,音乐剪辑处于暂停状态,当我恢复活动时,音乐再次正常恢复。但是在歌曲结束后我尝试恢复我的应用程序后出现错误。应用程序崩溃了,我IllegalStateException
在 back-press 方法(mp.pause();
)中遇到了异常。
请查看下面的代码并建议我是否做错了什么。
public class Audio_Activity extends Activity
{
private MediaPlayer mp;
Button btnStartStop ;
int length;
SharedPreferences prefs;
ImageView imgVw;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.audio);
init();
imgVw.setImageResource(R.raw.teddy_two);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
mp=MediaPlayer.create(Audio_Activity.this,R.raw.ennamo_yadho);
Log.e("Song is playing","in Mediya Player ");
Log.e("Current ","Position -> " + length);
mp.setLooping(false);
mp.start();
btnChapter.setEnabled(false);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
// TODO Auto-generated method stub
mp.stop();
mp.release();
btnChapter.setEnabled(true);
System.out.println("Music is over and Button is enable !!!!!!");
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPause()
{
super.onStop();
SharedPreferences. Editor prefsEdit = prefs.edit();
int position = mp.getCurrentPosition();
prefsEdit.putInt("mediaPosition", position);
prefsEdit.commit();
}
@Override
protected void onResume()
{
super.onResume();
System.out.println("Activity is Resume !!!");
int position = prefs.getInt("mediaPosition", 0);
mp.seekTo(position);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
if(mp!= null)
{
f(mp.isPlaying())
{
mp.pause();
}
}
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
这是我的原木猫
07-09 11:52:01.057: I/System.out(6854): Music is over and Button is enable !!!!!!
07-09 11:52:03.297: D/AndroidRuntime(6854): Shutting down VM
07-09 11:52:03.297: W/dalvikvm(6854): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-09 11:52:03.339: E/AndroidRuntime(6854): FATAL EXCEPTION: main
07-09 11:52:03.339: E/AndroidRuntime(6854): java.lang.IllegalStateException
07-09 11:52:03.339: E/AndroidRuntime(6854): at android.media.MediaPlayer.isPlaying(Native Method)
07-09 11:52:03.339: E/AndroidRuntime(6854): at com.audio_demo.Audio_Activity.onKeyDown(Audio_Activity.java:203)
07-09 11:52:03.339: E/AndroidRuntime(6854): at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
07-09 11:52:03.339: E/AndroidRuntime(6854): at android.app.Activity.dispatchKeyEvent(Activity.java:2375)
07-09 11:52:03.339: E/AndroidRuntime(6854): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
07-09 11:52:03.339: E/AndroidRuntime(6854): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
07-09 11:52:03.339: E/AndroidRuntime(6854): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
07-09 11:52:03.339: E/AndroidRuntime(6854): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
07-09 11:52:03.339: E/AndroidRuntime(6854): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 11:52:03.339: E/AndroidRuntime(6854): at android.os.Looper.loop(Looper.java:137)
07-09 11:52:03.339: E/AndroidRuntime(6854): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-09 11:52:03.339: E/AndroidRuntime(6854): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 11:52:03.339: E/AndroidRuntime(6854): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 11:52:03.339: E/AndroidRuntime(6854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-09 11:52:03.339: E/AndroidRuntime(6854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-09 11:52:03.339: E/AndroidRuntime(6854): at dalvik.system.NativeStart.main(Native Method)
07-09 11:52:28.787: I/Process(6854): Sending signal. PID: 6854 SIG: 9