1

I have created an application and with some background music.

Result = SUCCESS

Problem is.. -.- if app receives a call, music is still playing and plays instead of the user's original ringtone. Plus if "Home" button is selected on the phone. Activity exits but the music is still running?

I managed to quit the music if the "Back" button on the phone is selected.

other than that music runs 24/7.

What I have used is.

A menu option in the application that when the user selected "Menu" a list of options pop up and an option to choose "Settings..." If the user selects "Settings..." a preference dialogue pops up to check or uncheck music (ON/OFF) Result = SUCCESS

The music plays throughout the application, from activity to activity.

This is my media player code in my application.

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


@Override
protected void onResume() {
super.onResume();
Music.play(this, R.raw.bgmusic);
}



public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        finish(); 
        Music.stop(this);
        return true; 
    }
    return super.onKeyDown(keyCode, event);
}

This is my music.java

public class Music {
   private static MediaPlayer mp = null;

   /** Stop old song and start new one */

   public static void play(Context context, int resource) {
      stop(context);

      // Start music only if not disabled in preferences
      if (Prefs.getMusic(context)) {
         mp = MediaPlayer.create(context, resource);
         mp.setLooping(true);
         mp.start();
      }
   }



   /** Stop the music */
   public static void stop(Context context) { 
      if (mp != null) {
         mp.stop();
         mp.pause();
         mp.release();
         mp = null;
      }
   }
}

anyone have a clue?

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Wahid
  • 11
  • 2

1 Answers1

1

You need to call

Music.stop(this);

in the onPause method. This may have the effect of an instant glitch, when switching from one of yours activities to another.

Read also this question and the accepted answer.

Community
  • 1
  • 1
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • Sorry I have not made myself clear, what you suggested worked BUT i need my music to run through activities... this suggestion stops the music from running on the next activity...? thanks wahid – Wahid Jun 09 '11 at 07:36
  • hi kgiannakakis can you solve my problem http://stackoverflow.com/questions/14544848/some-time-music-does-not-stop-android – Siddhpura Amit Jan 27 '13 at 06:21