Ali Hatefi
Ali Hatefi

Reputation: 1

How to play different musics in different activities?

I've a problem in android and i test every methods but i cant solve it!! I want to play some musics in some activities it means I have a music for home Activity and another one for second Activity and another music for another activity and etc ...! when my app runs i want to play background music activity and when the user goes to another activity i want at first home music should be stop! and then second activity music should be play! it's important when I go back to home activity, home music should play again!

is there any person can help me ?! thanks a lot to any one that reply to me!

Upvotes: 0

Views: 35

Answers (1)

itsMike
itsMike

Reputation: 74

You can create MediaPlayer object in every Activity. Then it's just a switch between your music files location.

MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mp= MediaPlayer.create(getApplicationContext(), \your song location\);

    mp.start();

}

@Override
protected void onPause() {
    super.onPause();
    mp.stop();
    mp.release();
}

Upvotes: 1

Related Questions