Stop playing sound when exiting the app

Asked

Viewed 512 times

1

I’m creating an app, in JAVA, that plays songs by pressing a certain button. But when you press the button again, the music is repeated. And I would like to stop it by touching the same button.

package dagustin.southamericamemes;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void onClickTocar(View view){

        MediaPlayer mp = MediaPlayer.create(this , R.raw.morre);
        mp.start();

    }

}
  • Murilo, could you put an excerpt from the code?

  • Opa clear I’ll catch him here.

  • Oops, buddy I’m new to Stackoverflow like I do to glue it like a code?

  • Murilo, click edit on the question you asked, then paste the code.

  • It keeps getting like a normal text

  • To add formatting you must put in your question, comments cannot insert specific formatting click edit your answer, and in the field with {} select your code and click this button. the code will indent itself automatically remember to put spaces and the like, since the indentation does not put paragraphs, just hits the patterns

  • Okay, thanks I’ll try

  • Whoa, William, I just got the code down below.

  • If you have any doubts about how to do it correctly follow the tour of the site http://answall.com/tour

  • Thank you, I had already seen an answer in that "forum" but had not understood the answer.

Show 5 more comments

1 answer

3


Thus:

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends AppCompatActivity {
    private MediaPlayer mp

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

    public void onClickTocar(View view) {
        if (mp != null) {
            mp.reset();
        } else {
            mp = MediaPlayer.create(this, R.raw.morre);
            mp.start();

        }
    }


    @Override
    public void onPause() {
        super.onPause();
        if (mp != null) {
            mp.stop();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mp != null) {
            mp.release();
        }

    }
}
  • Oops, thank you :)

  • It will also work so when the person leaves the app, stop playing the sound?

  • Yes. To get out.

  • Okay, thanks I’ll make the test compilation and test.

  • Reginaldo, I put the code here in my Android Studio, but it is giving error in the code, in the parts: Mp.Prepare(); , @Override(the 2 Override) and: Public void Onpause() {( He says that it was expected a point and comma after the quotation marks of onPause, I put and does not leave the error), knows how to help me?

  • I wrote on the Notepad. That’s why the keys aren’t closing. I changed the code. See now.

  • Oops, thank you, I tried a lot of different ways to solve it.

  • Friend, now they’re only giving error in: Mp.()

  • @Murilodagustin what error appears? if it is something in relation to the last post/comment implementation to look at!

  • In fact, this was the last line I put. I was without, but I remembered that after a stop, prepare should be called. Finally, take it off and see if it runs smoothly. https://developer.android.com/reference/android/media/MediaPlayer.html#prepare()

  • Opa Reginaldo, it worked yes, thank you very much!

  • Friends, I discovered another problem, If I press the button again it stops playing the sound and does not play anymore, You have to close the APP and open again, the same happens if you wait to finish the sound and press again the button it does not play, You have to close the app and open it again.

  • @Murilodagustin I changed the code.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.