user2590541
user2590541

Reputation: 512

How to stop audio by clicking button and jumped to other activity (layout)

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

Answers (2)

Piyush
Piyush

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

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

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

Related Questions