dod_moshe
dod_moshe

Reputation: 35

Trying to make a pause/resume music activity using service

I'm trying to create an activity with one button in its layout which will pause or resume the music when it's pressed. the problem is that when it's pressed to resume the music it starts the audio file all over again. Any idea how to make it play from the last point it stopped rather than restarting it?

Main activity

public class MainActivity extends Activity implements OnClickListener {

    private Boolean isMusicPlaying;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View musicButton = findViewById(R.id.music_button);
        musicButton.setOnClickListener(this);
        startService(new Intent(getBaseContext(), MusicService.class));
        isMusicPlaying = true;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.music_button)
            if (isMusicPlaying) stopService(new Intent(getBaseContext(),
                    MusicService.class));
            else startService(new Intent(getBaseContext(), MusicService.class));
        isMusicPlaying = !isMusicPlaying;
    }
}

Service class

public class MusicService extends Service {

    MediaPlayer player;

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

    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.song);
        player.setLooping(true); // Set looping
        player.setVolume(100, 100);
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        player.start();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        player.pause();
    }
}

Upvotes: 2

Views: 6014

Answers (2)

droid-zilla
droid-zilla

Reputation: 555

Seeing that the activity is re-started why don't you save the states to Shared Preferences

Upvotes: 0

sam
sam

Reputation: 2820

It seems like MediaPlayer object in your service is not kept when you stop the service. You can get the current position of your song using getCurrentPosition() method before you stop your service,

player.pause();
currentPos = player.getCurrentPosition();

and send this value back to activity before it is stopped.

Then, you can use seekTo(int) method to move to specified time position before playing.

player.seekTo(currentPos);
player.start();

For communication between your service and activity, you might register broadcastreceiver and communicate using intent or use a bound service.

UPDATE: I found following tutorial and you'd better to follow this. It's about using a service with MediaPlayer. Especially, you are not using prepare method at all when you initialize your MediaPlayer. put these lines:

player.setOnPreparedListener(this);
player.prepareAsync();

and add listener:

public void onPrepared(MediaPlayer player) {
    player.start();
}

http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices

Upvotes: 1

Related Questions