Example usage for android.media RemoteControlClient RemoteControlClient

List of usage examples for android.media RemoteControlClient RemoteControlClient

Introduction

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

Prototype

public RemoteControlClient(PendingIntent mediaButtonIntent) 

Source Link

Document

Class constructor.

Usage

From source file:com.andrew.apolloMod.service.ApolloService.java

@SuppressLint({ "WorldWriteableFiles", "WorldReadableFiles" })
@Override/*from ww w. ja va2 s  . co m*/
public void onCreate() {
    super.onCreate();

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    ComponentName rec = new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName());
    mAudioManager.registerMediaButtonEventReceiver(rec);
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(rec);
    PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    if (Constants.isApi14Supported()) {
        mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
        mAudioManager.registerRemoteControlClient(mRemoteControlClient);

        int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
                | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP;
        mRemoteControlClient.setTransportControlFlags(flags);
    }

    mPreferences = getSharedPreferences(APOLLO_PREFERENCES, MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
    mCardId = MusicUtils.getCardId(this);

    registerExternalStorageListener();

    // Needs to be done in this thread, since otherwise
    // ApplicationContext.getPowerManager() crashes.
    mPlayer = new MultiPlayer();
    mPlayer.setHandler(mMediaplayerHandler);

    reloadQueue();
    notifyChange(QUEUE_CHANGED);
    notifyChange(META_CHANGED);

    IntentFilter commandFilter = new IntentFilter();
    commandFilter.addAction(SERVICECMD);
    commandFilter.addAction(TOGGLEPAUSE_ACTION);
    commandFilter.addAction(PAUSE_ACTION);
    commandFilter.addAction(NEXT_ACTION);
    commandFilter.addAction(PREVIOUS_ACTION);
    commandFilter.addAction(CYCLEREPEAT_ACTION);
    commandFilter.addAction(TOGGLESHUFFLE_ACTION);
    registerReceiver(mIntentReceiver, commandFilter);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());
    mWakeLock.setReferenceCounted(false);

    // If the service was idle, but got killed before it stopped itself, the
    // system will relaunch it. Make sure it gets stopped again in that
    // case.
    Message msg = mDelayedStopHandler.obtainMessage();
    mDelayedStopHandler.sendMessageDelayed(msg, IDLE_DELAY);
}

From source file:com.andrew.apollo.MusicPlaybackService.java

/**
 * Initializes the remote control client
 *///from   ww w.ja v a2 s .c  om
private void setUpRemoteControlClient() {
    final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(mMediaButtonReceiverComponent);
    mRemoteControlClient = new RemoteControlClient(PendingIntent.getBroadcast(getApplicationContext(), 0,
            mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    try {
        if (mAudioManager != null) {
            mAudioManager.registerRemoteControlClient(mRemoteControlClient);
        }
    } catch (Throwable t) {
        // seems like this doesn't work on some devices where it requires MODIFY_PHONE_STATE
        // which is a permission only given to system apps, not third party apps.
    }

    // Flags for the media transport control that this client supports.
    int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP;

    flags |= RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE;
    mRemoteControlClient.setOnGetPlaybackPositionListener(this::position);
    mRemoteControlClient.setPlaybackPositionUpdateListener(this::seek);

    mRemoteControlClient.setTransportControlFlags(flags);
}

From source file:net.nightwhistler.pageturner.fragment.ReadingFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void registerRemoteControlClient(ComponentName componentName) {

    audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    Intent remoteControlIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    remoteControlIntent.setComponent(componentName);

    RemoteControlClient localRemoteControlClient = new RemoteControlClient(
            PendingIntent.getBroadcast(context, 0, remoteControlIntent, 0));

    localRemoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE);

    audioManager.registerRemoteControlClient(localRemoteControlClient);

    this.remoteControlClient = localRemoteControlClient;
}

From source file:org.moire.ultrasonic.service.DownloadServiceImpl.java

@SuppressLint("NewApi")
public void setUpRemoteControlClient() {
    if (!Util.isLockScreenEnabled(this))
        return;/*w ww .j a  va2s .  c o m*/

    ComponentName componentName = new ComponentName(getPackageName(),
            MediaButtonIntentReceiver.class.getName());

    if (remoteControlClient == null) {
        final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setComponent(componentName);
        PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, mediaButtonIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteControlClient = new RemoteControlClient(broadcast);
        audioManager.registerRemoteControlClient(remoteControlClient);

        // Flags for the media transport control that this client supports.
        int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
                | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            flags |= RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE;

            remoteControlClient
                    .setOnGetPlaybackPositionListener(new RemoteControlClient.OnGetPlaybackPositionListener() {
                        @Override
                        public long onGetPlaybackPosition() {
                            return mediaPlayer.getCurrentPosition();
                        }
                    });

            remoteControlClient.setPlaybackPositionUpdateListener(
                    new RemoteControlClient.OnPlaybackPositionUpdateListener() {
                        @Override
                        public void onPlaybackPositionUpdate(long newPositionMs) {
                            seekTo((int) newPositionMs);
                        }
                    });
        }

        remoteControlClient.setTransportControlFlags(flags);
    }
}