Reputation: 512
on one activity an audio is being played.
now i want to click on a button so that audio should stopped and also an other activity opened using intent.
i used .stop() to stop audio. but when i click on button, a message is shown unfortunately application has stopped
, and my application is closed, what to do of it???
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button enterbutton;
MediaPlayer mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaPlayer mPlayer=MediaPlayer.create(this, R.raw.main_act);
mPlayer.start();
enterbutton=(Button)findViewById(R.id.btn_enter1);
enterbutton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mPlayer.stop();
Intent i=new Intent(MainActivity.this,Select_destination.class);
startActivity(i);
}
}
Upvotes: 1
Views: 302
Reputation: 18923
You have already make a Global varible for media player and also make a second time after oncreate() method.. So change it by:
MediaPlayer mPlayer=MediaPlayer.create(this, R.raw.main_act);
to
mPlayer=MediaPlayer.create(this, R.raw.main_act);
Upvotes: 2
Reputation: 7415
Change
MediaPlayer mPlayer=MediaPlayer.create(this, R.raw.main_act);
To
mPlayer=MediaPlayer.create(this, R.raw.main_act);
As it reinit the MediaPlayer .
Upvotes: 3