Markus
Markus

Reputation: 2564

Android MediaPlayer stops after half an hour of playback

I'm writing an media playing app. It works fine, but after playing for around 30 minutes the playback stops, and as far as I know it only happens when I'm out and running. I don't think (but I don't know for sure) it happens when I'm connected to the debugger, but since it takes so long for the bug to occur it's quite annoying to debug.

Even though the MediaPlayer is running in it's own (bound) service I have a feeling the bug might be related to android pausing/stopping my app after the screen has been off for some time (which does not happen when I'm debugging). This is only a guess however.

My question is not what the exact problem is but rather how to debug this kind of problem. I have attached BugSense to my application, but it doesn't throw any exception. Any idea for a practical debugging strategy?

EDIT: The service can be found here: https://github.com/bottiger/SoundWaves/blob/5820196924ce9bc731f50c5def991351ed9b3a3b/src/info/bottiger/podcast/service/PlayerService.java

I'm using the following code to bind it.

public ServiceConnection playerServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        mPlayerServiceBinder = ((PlayerService.PlayerBinder) service)
                .getService();
        // log.debug("onServiceConnected");
    }

    @Override
    public void onServiceDisconnected(ComponentName className) {
        mPlayerServiceBinder = null;
        // log.debug("onServiceDisconnected");
    }
};

and in onCreate()

       getActivity().bindService(bindIntent, playerServiceConnection,
            Context.BIND_AUTO_CREATE);

Upvotes: 1

Views: 554

Answers (1)

Alexander Mikhaylov
Alexander Mikhaylov

Reputation: 1800

You doesn't call startForeground and stopForeground.

A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

Android kills your service when system needs some more resources. This case is very hard to debug. It's OS specific behavior.

Call startForeground in your service, when you start playing and stopForeground when stop.

More about service lifecycle you can read here: http://developer.android.com/reference/android/app/Service.html

Upvotes: 2

Related Questions