On the MainActivity, I play a background music using MediaPlayer.
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Music.playBGmusic(this, R.raw.main_music);
}
@Override
protected void onResume() {
super.onResume();
if ( ! Music.isPlaying(this) )
{
Music.playBGmusic(this, R.raw.main_music);
}
}
In Music.java:
public class Music {
private static MediaPlayer mMainSound = null;
public static boolean isPlaying(Context context)
{
if ( mMainSound != null )
{
return mMainSound.isPlaying();
}
return false;
}
public static void playBGmusic(Context context, int resource)
{
if ( mMainSound != null ) mMainSound.release();
mMainSound = MediaPlayer.create(context, resource);
mMainSound.setLooping(true);
mMainSound.start();
}
public static void stopBGmusic(Context context) {
if (mMainSound != null) {
mMainSound.stop();
mMainSound.release();
mMainSound = null;
}
}
}
where mMainSound is a MediaPlayer object. There is a button on my MainActivity, and when it’s clicked, it creates an intent, and startActivity that intent. This new activity calls playBGmusic with another sound. Now when that new activity calls stopBGmusic() and finish() to return to MainActivity, onResume() of MainActivity is called as expected, and it’s supposed to play main_music in the background. What actually happens is that I could only hear less than a second of the music, and it gets cut off. I tried with prepare() before starting MediaPlayer, but still that doesn’t solve the problem. It will be very much appreciated if anyone could clarify this issue for me..
Maybe I should also mention where finish() is called in the new activity, although I’m not sure if that’s of any importance..
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
showDialog(1);
break;
}
return true;
}
@Override
protected Dialog onCreateDialog(int id) { //id is ignored
Dialog dialog = null;
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.return_home)
.setCancelable(true)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton(R.string.cancel, null);
dialog = builder.create();
return dialog;
}
You should really bind a service to control the MediaPlayer, not have your main activity control it. Instead it should control the service via you binding the service it to your app.
To put it simply(assuming you want to continuously play music even upon exiting you app):
When your are playing music you want to run it in the background and have your app control (start/stop/pause) states of your mediaplayer. When you create the media player you play from the background instead of the foreground. This allows you to continuously play music while the user has your service running.
Communication could be setup using PendingIntent to communicate with your service, through capturing you specific intent filters. I’d recommend go to the Google dev site and do some of the tutorials and try to learn how Intents, intent-filter/actions, and services and also learn the lifecycle of activities and services.
To do this properly your should allocate some time to research/experiment and learn about android.