Search code examples
androidaudiotransition

Play/Stop sound from another activity


I have 3 activities. I want to play one background music to all this activity. I made this possible by. Doing this.

In activity 1:

bgmp = MediaPlayer.create(this, R.raw.menu);
bgmp.setLooping(true);
bgmp.start();

This will make my music play up to the 3rd acitivity. At activity three. I need to stop this background music because another background music will be played when I go to the 4th activity. How can I stop the music at the 3rd acitivity that was created at the 1st activity. Any ideas? Thanks!


Solution

  • Define Method in common class with require parameters and use that Method in your activities.

    public class CommonMethod {
    public static MediaPlayer player;
        public static void SoundPlayer(Context ctx,int raw_id){
                player = MediaPlayer.create(ctx, raw_id);
                player.setLooping(false); // Set looping
                player.setVolume(100, 100);
    
                //player.release();
                 player.start();
            }
    }
    

    Within in your third activity, code for stop media.

    CommonMethod.player.stop();