简体   繁体   中英

Android mediaPlayer causing force close onquit

My code is force closing as soon as I hit the back button to leave the activity. Here it is. I think what it does is relatively self explanatory. When called, it shows some text plus a button that when tapped starts a recording playing. When someone hits the back button, it should go back to the previous activity and stop the recording. Thanks for helping me!

public class ToBeOrNot extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_be_or_not);

    }
MediaPlayer mediaPlayer;

public void react(View view) {
    mediaPlayer = MediaPlayer.create(ToBeOrNot.this, R.raw.achord);
    mediaPlayer.start(); 
}
protected void onStop(){
mediaPlayer.release();
mediaPlayer = null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_to_be_or_not, menu);
// Locate MenuItem with ShareActionProvider
return true;
}

}

Please provide log-cat error logs, otherwise its pain to look whats wrong.

But i guess you need to.

call stop() on media player in onPause()

If you want it to keep playing then you need to use service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM