Example usage for android.media AudioManager registerMediaButtonEventReceiver

List of usage examples for android.media AudioManager registerMediaButtonEventReceiver

Introduction

In this page you can find the example usage for android.media AudioManager registerMediaButtonEventReceiver.

Prototype

@Deprecated
public void registerMediaButtonEventReceiver(PendingIntent eventReceiver) 

Source Link

Document

Register a component to be the sole receiver of MEDIA_BUTTON intents.

Usage

From source file:net.sourceforge.kalimbaradio.androidapp.util.Util.java

public static void registerMediaButtonEventReceiver(Context context) {

    // Only do it if enabled in the settings.
    SharedPreferences prefs = getPreferences(context);
    boolean enabled = prefs.getBoolean(Constants.PREFERENCES_KEY_MEDIA_BUTTONS, true);

    if (enabled) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        ComponentName mediaButtonIntentReceiver = new ComponentName(context.getPackageName(),
                MediaButtonIntentReceiver.class.getName());
        audioManager.registerMediaButtonEventReceiver(mediaButtonIntentReceiver);
    }// ww w  . jav a2  s  . c om
}

From source file:de.j4velin.headsetturnon.ListeningService.java

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
    if (BuildConfig.DEBUG)
        android.util.Log.d("HeadsetTurnOn", "service start");
    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    boolean failed = am.requestAudioFocus(focusChangeListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_FAILED;
    am.registerMediaButtonEventReceiver(new ComponentName(ListeningService.this, Receiver.class));

    if (BuildConfig.DEBUG)
        android.util.Log.d("HeadsetTurnOn", "request audiofocus failed? " + failed);

    NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    b.setOngoing(true).setContentText(getString(R.string.notification_text))
            .setContentTitle(getString(R.string.notification_title)).setSmallIcon(R.drawable.ic_notification)
            .setContentIntent(PendingIntent.getActivity(this, 0,
                    new Intent(this, Main.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0));

    startForeground(1, b.build());/* w w  w . jav a  2  s .c  o m*/

    return START_STICKY;
}

From source file:com.thejoshwa.ultrasonic.androidapp.util.Util.java

public static void registerMediaButtonEventReceiver(Context context) {
    if (getMediaButtonsPreference(context)) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.registerMediaButtonEventReceiver(
                new ComponentName(context.getPackageName(), MediaButtonIntentReceiver.class.getName()));

    }//from w  ww.j a  va 2s .c o  m
}

From source file:org.videolan.vlc.AudioService.java

/**
 * Set up the remote control and tell the system we want to be the default receiver for the MEDIA buttons
 * @see http://android-developers.blogspot.fr/2010/06/allowing-applications-to-play-nicer.html
 *//*w ww.  j  a  v  a2 s  .c  o m*/
@TargetApi(14)
public void setUpRemoteControlClient() {
    Context context = VLCApplication.getAppContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);

    if (Util.isICSOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);

        if (mRemoteControlClient == null) {
            Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
            PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);

            // create and register the remote control client
            mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
            audioManager.registerRemoteControlClient(mRemoteControlClient);
        }

        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    } else if (Util.isFroyoOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
    }
}

From source file:org.videolan.myvlc.core.mediaController.AudioService.java

/**
 * Set up the remote control and tell the system we want to be the default receiver for the MEDIA buttons
 * @see http://android-developers.blogspot.fr/2010/06/allowing-applications-to-play-nicer.html
 *//* ww  w .  ja va  2 s .c om*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void setUpRemoteControlClient() {
    Context context = MyVLCApp.getAppContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);

    if (Util.isICSOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);

        if (mRemoteControlClient == null) {
            Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
            PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);

            // create and register the remote control client
            mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
            audioManager.registerRemoteControlClient(mRemoteControlClient);
        }

        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    } else if (Util.isFroyoOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
    }
}

From source file:org.amahi.anywhere.service.AudioService.java

private void setUpAudioPlayerRemote() {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    ComponentName audioReceiver = new ComponentName(getPackageName(), AudioReceiver.class.getName());

    Intent audioIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    audioIntent.setComponent(audioReceiver);
    PendingIntent audioPendingIntent = PendingIntent.getBroadcast(this, 0, audioIntent, 0);

    audioPlayerRemote = new RemoteControlClient(audioPendingIntent);
    audioPlayerRemote.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS);
    audioPlayerRemote.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);

    audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    audioManager.registerMediaButtonEventReceiver(audioReceiver);
    audioManager.registerRemoteControlClient(audioPlayerRemote);
}

From source file:com.yamin.kk.service.AudioService.java

/**
 * Set up the remote control and tell the system we want to be the default receiver for the MEDIA buttons
 * @see http://android-developers.blogspot.fr/2010/06/allowing-applications-to-play-nicer.html
 *//*  w w w.  jav  a 2  s. c  o  m*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void setUpRemoteControlClient() {
    Context context = VLCApplication.getAppContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);

    if (Util.isICSOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);

        if (mRemoteControlClient == null) {
            Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
            PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);

            // create and register the remote control client
            mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
            audioManager.registerRemoteControlClient(mRemoteControlClient);
        }

        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    } else if (Util.isFroyoOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
    }
}

From source file:org.videolan.vlc.audio.AudioService.java

/**
 * Set up the remote control and tell the system we want to be the default receiver for the MEDIA buttons
 * @see http://android-developers.blogspot.fr/2010/06/allowing-applications-to-play-nicer.html
 *///from  ww w. jav a  2s . c  om
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void setUpRemoteControlClient() {
    Context context = VLCApplication.getAppContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);

    if (LibVlcUtil.isICSOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);

        if (mRemoteControlClient == null) {
            Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
            PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);

            // create and register the remote control client
            mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
            audioManager.registerRemoteControlClient(mRemoteControlClient);
        }

        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    } else if (LibVlcUtil.isFroyoOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
    }
}

From source file:org.videolan.vlc.PlaybackService.java

/**
 * Set up the remote control and tell the system we want to be the default receiver for the MEDIA buttons
 * @see http://android-developers.blogspot.fr/2010/06/allowing-applications-to-play-nicer.html
 *///w ww .j a va2  s .co  m
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void setUpRemoteControlClient() {
    Context context = VLCApplication.getAppContext();
    AudioManager audioManager = (AudioManager) VLCApplication.getAppContext().getSystemService(AUDIO_SERVICE);

    if (AndroidUtil.isICSOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);

        if (mRemoteControlClient == null) {
            Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
            PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);

            // create and register the remote control client
            mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
            audioManager.registerRemoteControlClient(mRemoteControlClient);
        }

        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    } else if (AndroidUtil.isFroyoOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
    }
}

From source file:com.dzt.musicplay.player.AudioService.java

/**
 * Set up the remote control and tell the system we want to be the default
 * receiver for the MEDIA buttons//www.j  a  v  a 2  s .co  m
 * 
 * @see http 
 *      ://android-developers.blogspot.fr/2010/06/allowing-applications-to
 *      -play-nicer.html
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void setUpRemoteControlClient() {
    Context context = getApplicationContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);

    if (LibVlcUtil.isICSOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);

        if (mRemoteControlClient == null) {
            Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
            PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);

            // create and register the remote control client
            mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
            audioManager.registerRemoteControlClient(mRemoteControlClient);
        }

        mRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
                | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
                | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    } else if (LibVlcUtil.isFroyoOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
    }
}