0

is there a way to stop mediacontroller from pausing when the screen display turns off either by itself or manually. My app uses the mediacontroller to stream audio files from the internet so I need it to keep playing even if the screen is off. Any help would be much appreciated.

here is the code for my mediacontroller class:

import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.widget.MediaController;

public class WillMediaController extends MediaController {

    public WillMediaController(Context context) {
        super(context);
    }

    @Override
    public void hide() {
        // Do Nothing to show the controller all times

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
        {
            ((Activity) getContext()).finish();

        }else{
            super.dispatchKeyEvent(event);
        }
        if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN ||
                event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
            // don't show the controls for volume adjustment
            return super.dispatchKeyEvent(event);
        }
        return true;


    }
}

Thanks

Will
  • 1,487
  • 1
  • 23
  • 35

1 Answers1

0

I'm not sure if there's a way to prevent the MediaController from pausing itself when the activity pauses (ie. when the screen goes off), but I do have a couple of ideas:

First, you could try to disable the screen from timing out in your activity via:

getWindow().setFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                     android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

However, this probably won't prevent the user from hitting the lock button and manually turning the screen off and ending the audio playback.

Second, you could create a Service with a MediaPlayer to play your audio stream in the background and respond to play / pause commands from your app: http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices

Hexar
  • 583
  • 2
  • 6