0

I am using a Service to play background Music. The problem is that the music continues playing when i have finished the activity. when i press home button the music stop i want to make music continue playing while application is use and stop when home button press help me please. can some tell me how to do this

private static final String TAG = null;
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;

}
@Override
public void onCreate() {
    super.onCreate();


    player = MediaPlayer.create(this, R.raw.sample);
    player.setLooping(true); // Set looping
    player.setVolume(100,100);

}
public int onStartCommand(Intent intent, int flags, int startId) {


    player.start();

    return 1;


}

public void onStart(Intent intent, int startId) {
    // TODO



}
public IBinder onUnBind(Intent arg0) {
    // TODO Auto-generated method stub

    return null;
}

public void onStop() {

}
public void onPause() {



    }


@Override
public void onDestroy() {

    player.stop();
    player.release();
}

@Override
public void onLowMemory() {




}

}

user3153271
  • 33
  • 1
  • 4

1 Answers1

0

You should use a service and play the music.

http://developer.android.com/guide/components/services.html

Service runs in the background so even when you switch activities it still runs and you can play your music there.

You can start a service and then stop it when you need. Also you can bind the service to the activity. Look at the docs in the above link for more info.

Example :

https://thenewcircle.com/s/post/60/servicesdemo_using_android_services

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    private static final String TAG = "MyService";
    MediaPlayer player;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");

        player = MediaPlayer.create(this, R.raw.braincandy);
            // song is res/raw/yourmusicfile.mp3
        player.setLooping(false); // Set looping
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
        player.stop();
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
        player.start();
    }
}

Now just start the service and you can stop the service when you need. You can switch between activities and the service still runs playing the music.

Since the code i huge i uploaded to DropBox. Here's the sample i made

https://www.dropbox.com/s/fjc4krpcwsg7qm6/ServicesDemo.zip

Raghunandan
  • 132,755
  • 26
  • 225
  • 256