how to stop several sounds when I click on a button start again





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















In my application I make a music background that works when click on a button start and the music changes loop sound1, sound2, sound3.



 public void startNewGame() {//initializing the sounds

final MediaPlayer sound1 = MediaPlayer.create(this, R.raw.music1);

final MediaPlayer sound2 = MediaPlayer.create(this, R.raw.music2);

final MediaPlayer sound3 = MediaPlayer.create(this, R.raw.music3);

//generate random number
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3) + 1;

//picking the right sound to play
switch (randomInt){
case 1:
if ( !sound1.isPlaying())
sound1.setLooping(true);
sound1.start();

break;
case 2: if ( !sound2.isPlaying() )

sound2.setLooping(true);
sound2.start();

break;
case 3:
if ( !sound3.isPlaying() )
sound3.setLooping(true);
sound3.start();

break;
}

mBtnChooseAndStart.setVisibility(View.GONE);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl_board_container,
GameFragment.newInstance(mBitmapBricks, GOAL_STATUS))
.commit();
}


the pobelem is that when I click on another button start again that generates a dialogue after confirmation I start the game but with another sound that works and the first does not stop
how to solve this problem thanks in advance



 public void restart(View view) {

if (mFullBitmap == null) {
// Not started, so cannot restart
UIUtils.toast(this, getString(R.string.not_started));
return;
}

new AlertDialog.Builder(this)
.setTitle(getString(R.string.restart))
.setMessage(getString(R.string.confirm_restart_msg))
.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

startNewGame();
}
})
.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}


and also how to stop sounds in onBackpressed ().










share|improve this question

























  • if (sound1 == null) ; Those ifs are not doing what you think they're doing (in fact they're doing nothing). I suggest you review how ifs work in Java.

    – m0skit0
    Nov 16 '18 at 18:54













  • thank you for targeting me the error i try if (sound1 == null) {} but application stops .if you can offer me a solution

    – MusAit
    Nov 16 '18 at 20:35











  • It seems that your problem is that you lack the basics of programming, you should try some simpler problems before attempting this.

    – m0skit0
    Nov 19 '18 at 10:13













  • I thought that the fact of trying to use this site is to have help if I encounter a difficulty to go ahead. not to orient me to redo java classes because I use one; and there are some examples of the same problem, and people try to give help, in my opinion the problem is not if it's the algorithm ... and instead of fading alone to solve my problem to find a solution I am gonna lose my time in no the base of the programming, no it is the algorithm. it is not you which will discourage me

    – MusAit
    Nov 20 '18 at 12:25











  • I'm definitely not trying to discourage you, however this site is a Q&A site for very specific questions with very specific answers. Your question is way too broad to answer here since it involves lack of very basic concepts which are too long to explain here and are already covered by a lot of other resources available on the Internet.

    – m0skit0
    Nov 20 '18 at 14:08




















0















In my application I make a music background that works when click on a button start and the music changes loop sound1, sound2, sound3.



 public void startNewGame() {//initializing the sounds

final MediaPlayer sound1 = MediaPlayer.create(this, R.raw.music1);

final MediaPlayer sound2 = MediaPlayer.create(this, R.raw.music2);

final MediaPlayer sound3 = MediaPlayer.create(this, R.raw.music3);

//generate random number
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3) + 1;

//picking the right sound to play
switch (randomInt){
case 1:
if ( !sound1.isPlaying())
sound1.setLooping(true);
sound1.start();

break;
case 2: if ( !sound2.isPlaying() )

sound2.setLooping(true);
sound2.start();

break;
case 3:
if ( !sound3.isPlaying() )
sound3.setLooping(true);
sound3.start();

break;
}

mBtnChooseAndStart.setVisibility(View.GONE);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl_board_container,
GameFragment.newInstance(mBitmapBricks, GOAL_STATUS))
.commit();
}


the pobelem is that when I click on another button start again that generates a dialogue after confirmation I start the game but with another sound that works and the first does not stop
how to solve this problem thanks in advance



 public void restart(View view) {

if (mFullBitmap == null) {
// Not started, so cannot restart
UIUtils.toast(this, getString(R.string.not_started));
return;
}

new AlertDialog.Builder(this)
.setTitle(getString(R.string.restart))
.setMessage(getString(R.string.confirm_restart_msg))
.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

startNewGame();
}
})
.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}


and also how to stop sounds in onBackpressed ().










share|improve this question

























  • if (sound1 == null) ; Those ifs are not doing what you think they're doing (in fact they're doing nothing). I suggest you review how ifs work in Java.

    – m0skit0
    Nov 16 '18 at 18:54













  • thank you for targeting me the error i try if (sound1 == null) {} but application stops .if you can offer me a solution

    – MusAit
    Nov 16 '18 at 20:35











  • It seems that your problem is that you lack the basics of programming, you should try some simpler problems before attempting this.

    – m0skit0
    Nov 19 '18 at 10:13













  • I thought that the fact of trying to use this site is to have help if I encounter a difficulty to go ahead. not to orient me to redo java classes because I use one; and there are some examples of the same problem, and people try to give help, in my opinion the problem is not if it's the algorithm ... and instead of fading alone to solve my problem to find a solution I am gonna lose my time in no the base of the programming, no it is the algorithm. it is not you which will discourage me

    – MusAit
    Nov 20 '18 at 12:25











  • I'm definitely not trying to discourage you, however this site is a Q&A site for very specific questions with very specific answers. Your question is way too broad to answer here since it involves lack of very basic concepts which are too long to explain here and are already covered by a lot of other resources available on the Internet.

    – m0skit0
    Nov 20 '18 at 14:08
















0












0








0








In my application I make a music background that works when click on a button start and the music changes loop sound1, sound2, sound3.



 public void startNewGame() {//initializing the sounds

final MediaPlayer sound1 = MediaPlayer.create(this, R.raw.music1);

final MediaPlayer sound2 = MediaPlayer.create(this, R.raw.music2);

final MediaPlayer sound3 = MediaPlayer.create(this, R.raw.music3);

//generate random number
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3) + 1;

//picking the right sound to play
switch (randomInt){
case 1:
if ( !sound1.isPlaying())
sound1.setLooping(true);
sound1.start();

break;
case 2: if ( !sound2.isPlaying() )

sound2.setLooping(true);
sound2.start();

break;
case 3:
if ( !sound3.isPlaying() )
sound3.setLooping(true);
sound3.start();

break;
}

mBtnChooseAndStart.setVisibility(View.GONE);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl_board_container,
GameFragment.newInstance(mBitmapBricks, GOAL_STATUS))
.commit();
}


the pobelem is that when I click on another button start again that generates a dialogue after confirmation I start the game but with another sound that works and the first does not stop
how to solve this problem thanks in advance



 public void restart(View view) {

if (mFullBitmap == null) {
// Not started, so cannot restart
UIUtils.toast(this, getString(R.string.not_started));
return;
}

new AlertDialog.Builder(this)
.setTitle(getString(R.string.restart))
.setMessage(getString(R.string.confirm_restart_msg))
.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

startNewGame();
}
})
.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}


and also how to stop sounds in onBackpressed ().










share|improve this question
















In my application I make a music background that works when click on a button start and the music changes loop sound1, sound2, sound3.



 public void startNewGame() {//initializing the sounds

final MediaPlayer sound1 = MediaPlayer.create(this, R.raw.music1);

final MediaPlayer sound2 = MediaPlayer.create(this, R.raw.music2);

final MediaPlayer sound3 = MediaPlayer.create(this, R.raw.music3);

//generate random number
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3) + 1;

//picking the right sound to play
switch (randomInt){
case 1:
if ( !sound1.isPlaying())
sound1.setLooping(true);
sound1.start();

break;
case 2: if ( !sound2.isPlaying() )

sound2.setLooping(true);
sound2.start();

break;
case 3:
if ( !sound3.isPlaying() )
sound3.setLooping(true);
sound3.start();

break;
}

mBtnChooseAndStart.setVisibility(View.GONE);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl_board_container,
GameFragment.newInstance(mBitmapBricks, GOAL_STATUS))
.commit();
}


the pobelem is that when I click on another button start again that generates a dialogue after confirmation I start the game but with another sound that works and the first does not stop
how to solve this problem thanks in advance



 public void restart(View view) {

if (mFullBitmap == null) {
// Not started, so cannot restart
UIUtils.toast(this, getString(R.string.not_started));
return;
}

new AlertDialog.Builder(this)
.setTitle(getString(R.string.restart))
.setMessage(getString(R.string.confirm_restart_msg))
.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

startNewGame();
}
})
.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}


and also how to stop sounds in onBackpressed ().







android android-studio android-mediaplayer soundpool






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 21:29







MusAit

















asked Nov 16 '18 at 18:14









MusAitMusAit

11




11













  • if (sound1 == null) ; Those ifs are not doing what you think they're doing (in fact they're doing nothing). I suggest you review how ifs work in Java.

    – m0skit0
    Nov 16 '18 at 18:54













  • thank you for targeting me the error i try if (sound1 == null) {} but application stops .if you can offer me a solution

    – MusAit
    Nov 16 '18 at 20:35











  • It seems that your problem is that you lack the basics of programming, you should try some simpler problems before attempting this.

    – m0skit0
    Nov 19 '18 at 10:13













  • I thought that the fact of trying to use this site is to have help if I encounter a difficulty to go ahead. not to orient me to redo java classes because I use one; and there are some examples of the same problem, and people try to give help, in my opinion the problem is not if it's the algorithm ... and instead of fading alone to solve my problem to find a solution I am gonna lose my time in no the base of the programming, no it is the algorithm. it is not you which will discourage me

    – MusAit
    Nov 20 '18 at 12:25











  • I'm definitely not trying to discourage you, however this site is a Q&A site for very specific questions with very specific answers. Your question is way too broad to answer here since it involves lack of very basic concepts which are too long to explain here and are already covered by a lot of other resources available on the Internet.

    – m0skit0
    Nov 20 '18 at 14:08





















  • if (sound1 == null) ; Those ifs are not doing what you think they're doing (in fact they're doing nothing). I suggest you review how ifs work in Java.

    – m0skit0
    Nov 16 '18 at 18:54













  • thank you for targeting me the error i try if (sound1 == null) {} but application stops .if you can offer me a solution

    – MusAit
    Nov 16 '18 at 20:35











  • It seems that your problem is that you lack the basics of programming, you should try some simpler problems before attempting this.

    – m0skit0
    Nov 19 '18 at 10:13













  • I thought that the fact of trying to use this site is to have help if I encounter a difficulty to go ahead. not to orient me to redo java classes because I use one; and there are some examples of the same problem, and people try to give help, in my opinion the problem is not if it's the algorithm ... and instead of fading alone to solve my problem to find a solution I am gonna lose my time in no the base of the programming, no it is the algorithm. it is not you which will discourage me

    – MusAit
    Nov 20 '18 at 12:25











  • I'm definitely not trying to discourage you, however this site is a Q&A site for very specific questions with very specific answers. Your question is way too broad to answer here since it involves lack of very basic concepts which are too long to explain here and are already covered by a lot of other resources available on the Internet.

    – m0skit0
    Nov 20 '18 at 14:08



















if (sound1 == null) ; Those ifs are not doing what you think they're doing (in fact they're doing nothing). I suggest you review how ifs work in Java.

– m0skit0
Nov 16 '18 at 18:54







if (sound1 == null) ; Those ifs are not doing what you think they're doing (in fact they're doing nothing). I suggest you review how ifs work in Java.

– m0skit0
Nov 16 '18 at 18:54















thank you for targeting me the error i try if (sound1 == null) {} but application stops .if you can offer me a solution

– MusAit
Nov 16 '18 at 20:35





thank you for targeting me the error i try if (sound1 == null) {} but application stops .if you can offer me a solution

– MusAit
Nov 16 '18 at 20:35













It seems that your problem is that you lack the basics of programming, you should try some simpler problems before attempting this.

– m0skit0
Nov 19 '18 at 10:13







It seems that your problem is that you lack the basics of programming, you should try some simpler problems before attempting this.

– m0skit0
Nov 19 '18 at 10:13















I thought that the fact of trying to use this site is to have help if I encounter a difficulty to go ahead. not to orient me to redo java classes because I use one; and there are some examples of the same problem, and people try to give help, in my opinion the problem is not if it's the algorithm ... and instead of fading alone to solve my problem to find a solution I am gonna lose my time in no the base of the programming, no it is the algorithm. it is not you which will discourage me

– MusAit
Nov 20 '18 at 12:25





I thought that the fact of trying to use this site is to have help if I encounter a difficulty to go ahead. not to orient me to redo java classes because I use one; and there are some examples of the same problem, and people try to give help, in my opinion the problem is not if it's the algorithm ... and instead of fading alone to solve my problem to find a solution I am gonna lose my time in no the base of the programming, no it is the algorithm. it is not you which will discourage me

– MusAit
Nov 20 '18 at 12:25













I'm definitely not trying to discourage you, however this site is a Q&A site for very specific questions with very specific answers. Your question is way too broad to answer here since it involves lack of very basic concepts which are too long to explain here and are already covered by a lot of other resources available on the Internet.

– m0skit0
Nov 20 '18 at 14:08







I'm definitely not trying to discourage you, however this site is a Q&A site for very specific questions with very specific answers. Your question is way too broad to answer here since it involves lack of very basic concepts which are too long to explain here and are already covered by a lot of other resources available on the Internet.

– m0skit0
Nov 20 '18 at 14:08














1 Answer
1






active

oldest

votes


















0














finally I found the solution and it works
firstly create a method to stop mediaplayer



public void cleanUpMediaPlayer(){
if(mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
}


then create a method of launching for each sound and within this method it is necessary to apply the functin to stop mediaplayer if it exist



public void sound1(){
cleanUpMediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.music1);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}

public void sound2(){
cleanUpMediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.music2);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}

public void sound3(){
cleanUpMediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.music3);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}


finally call the sounds in the desired moment
for me I want the sounds to change every time I play my game that's why I use random with switch loop like this.



 Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3) + 1;

//picking the right sound to play
switch (randomInt){
case 1:
sound1();

break;
case 2:
sound2();

break;
case 3:
sound3();

break;

}


the same for onBackPressed() you just have to call cleanUpMediaPlayer method



public void onBackPressed() {

cleanUpMediaPlayer();


.....
}



a good day for my friend mistek :)






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343321%2fhow-to-stop-several-sounds-when-i-click-on-a-button-start-again%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    finally I found the solution and it works
    firstly create a method to stop mediaplayer



    public void cleanUpMediaPlayer(){
    if(mediaPlayer != null) {
    if(mediaPlayer.isPlaying()) {
    mediaPlayer.stop();
    }
    mediaPlayer.release();
    }
    }


    then create a method of launching for each sound and within this method it is necessary to apply the functin to stop mediaplayer if it exist



    public void sound1(){
    cleanUpMediaPlayer();
    mediaPlayer = MediaPlayer.create(this, R.raw.music1);
    mediaPlayer.setLooping(true);
    mediaPlayer.start();
    }

    public void sound2(){
    cleanUpMediaPlayer();
    mediaPlayer = MediaPlayer.create(this, R.raw.music2);
    mediaPlayer.setLooping(true);
    mediaPlayer.start();
    }

    public void sound3(){
    cleanUpMediaPlayer();
    mediaPlayer = MediaPlayer.create(this, R.raw.music3);
    mediaPlayer.setLooping(true);
    mediaPlayer.start();
    }


    finally call the sounds in the desired moment
    for me I want the sounds to change every time I play my game that's why I use random with switch loop like this.



     Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(3) + 1;

    //picking the right sound to play
    switch (randomInt){
    case 1:
    sound1();

    break;
    case 2:
    sound2();

    break;
    case 3:
    sound3();

    break;

    }


    the same for onBackPressed() you just have to call cleanUpMediaPlayer method



    public void onBackPressed() {

    cleanUpMediaPlayer();


    .....
    }



    a good day for my friend mistek :)






    share|improve this answer




























      0














      finally I found the solution and it works
      firstly create a method to stop mediaplayer



      public void cleanUpMediaPlayer(){
      if(mediaPlayer != null) {
      if(mediaPlayer.isPlaying()) {
      mediaPlayer.stop();
      }
      mediaPlayer.release();
      }
      }


      then create a method of launching for each sound and within this method it is necessary to apply the functin to stop mediaplayer if it exist



      public void sound1(){
      cleanUpMediaPlayer();
      mediaPlayer = MediaPlayer.create(this, R.raw.music1);
      mediaPlayer.setLooping(true);
      mediaPlayer.start();
      }

      public void sound2(){
      cleanUpMediaPlayer();
      mediaPlayer = MediaPlayer.create(this, R.raw.music2);
      mediaPlayer.setLooping(true);
      mediaPlayer.start();
      }

      public void sound3(){
      cleanUpMediaPlayer();
      mediaPlayer = MediaPlayer.create(this, R.raw.music3);
      mediaPlayer.setLooping(true);
      mediaPlayer.start();
      }


      finally call the sounds in the desired moment
      for me I want the sounds to change every time I play my game that's why I use random with switch loop like this.



       Random randomGenerator = new Random();
      int randomInt = randomGenerator.nextInt(3) + 1;

      //picking the right sound to play
      switch (randomInt){
      case 1:
      sound1();

      break;
      case 2:
      sound2();

      break;
      case 3:
      sound3();

      break;

      }


      the same for onBackPressed() you just have to call cleanUpMediaPlayer method



      public void onBackPressed() {

      cleanUpMediaPlayer();


      .....
      }



      a good day for my friend mistek :)






      share|improve this answer


























        0












        0








        0







        finally I found the solution and it works
        firstly create a method to stop mediaplayer



        public void cleanUpMediaPlayer(){
        if(mediaPlayer != null) {
        if(mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
        }
        mediaPlayer.release();
        }
        }


        then create a method of launching for each sound and within this method it is necessary to apply the functin to stop mediaplayer if it exist



        public void sound1(){
        cleanUpMediaPlayer();
        mediaPlayer = MediaPlayer.create(this, R.raw.music1);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
        }

        public void sound2(){
        cleanUpMediaPlayer();
        mediaPlayer = MediaPlayer.create(this, R.raw.music2);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
        }

        public void sound3(){
        cleanUpMediaPlayer();
        mediaPlayer = MediaPlayer.create(this, R.raw.music3);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
        }


        finally call the sounds in the desired moment
        for me I want the sounds to change every time I play my game that's why I use random with switch loop like this.



         Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(3) + 1;

        //picking the right sound to play
        switch (randomInt){
        case 1:
        sound1();

        break;
        case 2:
        sound2();

        break;
        case 3:
        sound3();

        break;

        }


        the same for onBackPressed() you just have to call cleanUpMediaPlayer method



        public void onBackPressed() {

        cleanUpMediaPlayer();


        .....
        }



        a good day for my friend mistek :)






        share|improve this answer













        finally I found the solution and it works
        firstly create a method to stop mediaplayer



        public void cleanUpMediaPlayer(){
        if(mediaPlayer != null) {
        if(mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
        }
        mediaPlayer.release();
        }
        }


        then create a method of launching for each sound and within this method it is necessary to apply the functin to stop mediaplayer if it exist



        public void sound1(){
        cleanUpMediaPlayer();
        mediaPlayer = MediaPlayer.create(this, R.raw.music1);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
        }

        public void sound2(){
        cleanUpMediaPlayer();
        mediaPlayer = MediaPlayer.create(this, R.raw.music2);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
        }

        public void sound3(){
        cleanUpMediaPlayer();
        mediaPlayer = MediaPlayer.create(this, R.raw.music3);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
        }


        finally call the sounds in the desired moment
        for me I want the sounds to change every time I play my game that's why I use random with switch loop like this.



         Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(3) + 1;

        //picking the right sound to play
        switch (randomInt){
        case 1:
        sound1();

        break;
        case 2:
        sound2();

        break;
        case 3:
        sound3();

        break;

        }


        the same for onBackPressed() you just have to call cleanUpMediaPlayer method



        public void onBackPressed() {

        cleanUpMediaPlayer();


        .....
        }



        a good day for my friend mistek :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 18:12









        MusAitMusAit

        11




        11
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343321%2fhow-to-stop-several-sounds-when-i-click-on-a-button-start-again%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues