这是我的问题,当我启动Activity时音乐开始就像我想要的那样,但是当我离开Activity时(通过按后退按钮)应用程序崩溃并且"意外停止" :(我想做的就是让音频停止并进入另一个没有意外停止的活动。我该怎么做?
public class Run extends Activity {
/** The OpenGL View */
private GLSurfaceView glSurface;
private MediaPlayer mPlayer;
/**
* Initiate the OpenGL View and set our own
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create an Instance with this Activity
glSurface = new GLSurfaceView(this);
//Set our own Renderer and hand the renderer this Activity Context
glSurface.setRenderer(new Lesson06(this));
//Set the GLSurface as View to this Activity
setContentView(glSurface);
new Thread(new Runnable() {
public void run() {
try{
MediaPlayer mPlayer = MediaPlayer.create(Run.this, R.drawable.theygonelovemeformyambition);
mPlayer.setLooping(true);
mPlayer.start();
while(mPlayer.isPlaying()){
android.os.SystemClock.sleep(100);
}
}catch(Exception e){
String TAG = null;
Log.d(TAG,"ERROR PLAYING");
e.printStackTrace();
}
}}).start();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mPlayer.release();
}
}
答案 0 :(得分:0)
您必须释放媒体播放器资源之前。你应该停止下面的媒体播放器。
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if(mPlayer.isPlaying())
mPlayer.stop();
mPlayer.release();
super.onDestroy();
}
答案 1 :(得分:0)
你没有将mPlayer初始化 在public void run()中,您只是创建一个新的MediaPlayer 所以当你尝试mPlayer.release();你正在攻击null mPlayer,所以你得到一个nullPointerExceptionerror(我猜)