Stop MediaPlayer - java

I made an android application where you can click buttons to play sounds. Every time, you click the button, a new MediaPlayer is getting created and the problem is, when you push a button during another sound is still playing, they are playing at the same time, but I want, that the previous sounds stops when you push a button. I already tried this code but my application crashes every time I press the button:
MediaPlayer mp;
Button button;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()){
mp.stop();
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
}});
Is there a way to solve my problem?

It might be crashing because 'mp' is not initialized. Try this:
#Override
public void onClick(View v) {
//if mp exists and is playing...
if(mp != null && mp.isPlaying()){
mp.stop();
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
}});
EDIT You could also avoid checking for null by initializing 'mp' when you declare it on top.
For instance, instead of checking if mp != null. Change your declaration of 'mp' from:
MediaPlayer mp;
to
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
and your onclick method could look like:
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
}
//start mp from beginning
}

It seems like starting too many MediaPlayer isn't good, because when i click the sounds a couple times, the app crashes. I already changed mp.start to mp.reset and release.

Notes
You have to release the player before creating a new one
I would not bet on isPlaying() check to stop the player; since you don't need it release it!
Code
#Override
public void onClick(View v) {
if(mp != null) {
mp.release();
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
mp.start();
}});

Related

MediaPlayer cannot paused in Adapter Recyclerview with once Click Button

please help, I always experience errors here, I create mediaplayer in the audio list using 1 button, I have created the condition and isPlaying function on the mediaplayer in the adapter class, when the audio button is clicked successfully run, but when I click a second time, audio can not be paused, just repeat from the beginning, help me, what is the right solution?
this is my code:
public void onBindViewHolder(#NonNull MurottalAdapter.MurotalHolder holder, int position) {
Item item = itemList.get(position);
holder.titles.setText(item.getTitle());
holder.btnplays.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//I've tried replacing this with holder.itemview.getContext() and I put it outside the onclick method, but the problem is the same
mediaPlayer = MediaPlayer.create(view.getContext(),item.getAudio());
if (mediaPlayer.isPlaying()){
if (mediaPlayer != null) {
mediaPlayer.pause();
holder.btnplays.setImageResource(R.drawable.ic_play_circle_filled_black_24dp);
}
} else {
if (mediaPlayer != null){
mediaPlayer.start();
holder.btnplays.setImageResource(R.drawable.ic_pause_circle_filled_black_24dp);
}
}
}
});
}
Pls try to move following code outside of onClick() method?
mediaPlayer = MediaPlayer.create(view.getContext(),item.getAudio());

How to stop the sound/audio when you press the back button in the phone

how can i stop the audio from playing when i press the back button in my phone. when i go to another activity the audio is still playing unless i close the app. here's my code:
FloatingActionButton fab7 = (FloatingActionButton) this.findViewById(R.id.fab7);
final MediaPlayer mp = MediaPlayer.create( this, R.raw.cebu);
fab7.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {;
if(mp.isPlaying()){
mp.pause();
} else {
mp.start();
}
}
});
1.) declare MediaPlayer mp; outside onCreate
2.) override onStop and apply the same logic of click listener inside onStop
why onStop not onBackPress?
Because you want to pause the player when you go outside app as well as when you go to another activity
MediaPlayer mp;
onCreate(..){
mp = MediaPlayer.create( this, R.raw.cebu);
fab7.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {;
pausePlayer();
}
});
}
#Override
public void onStop(){
super.onStop()
pausePlayer();
}
void pausePlayer(){
if(mp.isPlaying()){
mp.pause();
} else {
mp.start();
}
}
#Override
public void onBackPress() {
pausePlayer();
mp.stop();
}
Use this to detect and pause the Mediaplayer
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "Back button pressed Song paused");
mp.pause();
}
return super.onKeyDown(keyCode, event);
}
write mediaplayer.stop();
on start of back activity when u gone
Override Activity#onBackPress() method to stop your player when pressing Back button:
#Override
public void onBackPress() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
If you want to stop your player when your Activity finish, and don't care why (may be Back press, another Activity started, app crashed,..), you can override Activity#onStop() so you will make sure your music is immediately stop whenever your Activity gone!
#Override
public void onStop() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
super.onStop();
}
Btw, note that you should use mp.stop() instead of mp.pause() then mp.release() to avoid memory/resources leak!

android app crashes when starting/stopping media

I am writing a small practice app that plays a sound clip when a button is tapped. In my previous code, this amounts to just the creation of a MediaPlayer object and a call to mp.start() to start the audio.
This works, but now I would like that same button to play only when no sound is playing yet. If sound is playing, stop the audio. A play/stop button.
I tried to do this using the following code:
```
public class MainActivity extends Activity {
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void goButtonClicked(View v) {
if(mp == null) {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.wordt);
}
if(mp.isPlaying()) {
mp.stop();
mp.release();
}
else {
mp.start();
}
}
}
```
However now when I run the app, the app crashes when I tap the button. Where did I go wrong?
Just change your code like this,
public void goButtonClicked(View v) {
if(mp == null) {
mp = MediaPlayer.create(getApplicationContext(), R.raw.wordt);
}
....
}
What happens here is, MediaPlayer mp = ... means you are creating a local variable inside if condition. But still your field variable is null. And when the app executes the second if condition, it throws a NullPointerException.
You didn't initialize your first mp object, so you see nullPointerException,
and you can delete second mp object

Playing audio repeat Android studio

I am playing some audio via a button is pressed. The audio plays fine, but i want to be able to spam the button and let the audio play play play play. Even stop where its playing and play again if i press the button. Now when i press the button, the audio just plays all the way through and nothing happens if i press the button while the audio is playing. Here is my code:
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
mp.setLooping(true);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
Replace this code with your existing code. if you want to repeat your audio then you have to set the looping property to true.
OK - I hope I got this right:
You want to play the audio clip for every time you press the button and the clip should be played completely, before the next clip starts.
Here's what I'd do:
Create a new integer attribute.private int i = 0;
Increment it every time you press the button.
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
IUP();
}
});
public synchronized void IUP(){
i++;
}
public synchronized void IDOWN(){
i--;
}
public synchronized int getI(){ return i; }
public void startAudio(){ mp.start(); }
public boolean isPlaying(){ return mp.isPlaying(); }
Use a seperate thread for the player.
Thread player_thread = new Thread(new Runnable(){
#Override
public void run(){
while(true){
if(getI() > 0 && !isPlaying()){
IDOWN();
startAudio();
}
}
}
}).start();
Or use an OnCompletionListener on the MediaPlayer.
Tell me if this is what you were looking for.
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(play_button.getText().toString().equals("Stop")
{
mp.stop();
play_button.setText("Play");
}
else
{
mp.start();
play_button.setText("Stop");
}
}
});

Play and stop MediaPlayer in Android

Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
MediaPlayer mp = MediaPlayer.create(main.this, R.raw.lastmohican);
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mp.isPlaying()) {
mp.stop();
} else {
mp.start();
}
}
}
I can play this music by one click and with another click music is stopped, but when I click together music does not start. Any idea why?
I'm not sure I understand your problem, but if you are wondering why the audio will not start back up after you press the button a third time, it is because you are calling stop() on the media player when you should be calling pause(). Stop requires you to prepare the media again before you can begin playback. Have a look at the MediaPlayer Documentation. If that is not your problem you need to provide a better description.
Its solved you should change code for this type
if (mp.isPlaying()) {
mp.stop();
mp.prepareAsync();
mp.seekTo(0);
} else {
mp.start();
}

Categories

Resources