Engineer Llfe Dogear

エンジニアリング活動におけるメモやTipsを書くブログ

RemoteControlClientの使い方(RemoteControllerとの連携)

前回の記事で、API 19から登場したRemoteControllerの使い方について説明をしました。 今回は、RemoteControlClientのどのような動作がRemoteControllerに関連しているか、studyしましたので、そのメモを残します。

RemoteControlClientの生成

まず、以下の用にRemoteControlClientのインスタンスの生成を行います。 このとき、AudioMangerのrequestAudioFocusを利用して、Focusを取得しないとRemoteControlClienは正常に動作しません。

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
ComponentName component = 
    new ComponentName(this, RemoteControlEventReceiver.class);
am.requestAudioFocus(new OnAudioFocusChangeListener() {
    @Override
    public void onAudioFocusChange(int foucusChange) {
        Log.d(TAG, "onAudioFocusChange");
    }
}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

am.registerMediaButtonEventReceiver(component);

Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(component);
PendingIntent mediaPendingIntent = 
    PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);

mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
am.registerRemoteControlClient(mRemoteControlClient);

RemoteControlClientの生成とRemoteControllerの関係に関してですが、requetAudioFocusでfocusがあたっとときに、 RemoteContoller.OnClientUpdateListener#onClientChange が呼び出されているようです。

RemoteControlClientがどの制御をサポートするかの設定

RemoteControlClientがサポートするPlayerの制御の設定をflagにより行います。 このサポート状況を見て、RemoteControllerはどのようにRemoteControlClientを制御すればよいかを判断することができるのだと思います。

設定は以下のように行います。設定するflagは任意です。

mRemoteControlClient.setTransportControlFlags(
    RemoteControlClient.FLAG_KEY_MEDIA_PLAY |
    RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | 
    RemoteControlClient.FLAG_KEY_MEDIA_STOP |
    RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE);

RemoteControlClientのsetTransportControlFlagsの呼び出た場合に、RemoteContoller.OnClientUpdateListener#onClientTransportControlUpdate が呼び出されます。

RemoteControlClientでのメタデータのupdate

RemoteControlClientでのメタデータのupdateは、以下のように editMetadataでRemoteControlClient.MetadataEditorを生成し、編集したいメタデータ項目をputStringなどで指定することで行います。

mRemoteControlClient.editMetadata(true)
    .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, "a")
    .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, "b")
    .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, "c")
    .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, getDuration())
    .apply();

RemoteControlClientのeditMetadataでメタデータを更新した場合に、 RemoteContoller.OnClientUpdateListener#onClientMetadataUpdate が呼び出されます。

RemoteControlClientでの再生状態の設定

RemoteControlClientで、再生状態の設定は以下のように行います。設定には、再生状態だけを設定するもの、再生位置と再生速度も設定するものの2つが存在します。

// 再生状態のみ
mRemoteControlClient.setPlaybackState(playbackState);

// 再生状態 + 再生位置、再生速度
mRemoteControlClient.setPlaybackState(playbackState, timeInMs, playbackSpeed);

playbackStateにはRemoteControlClient.PLAYSTATE_PLAYINGなどを指定します。 RemoteContolClientのそれぞれのsetPlaybackStateが呼ばれた時に、それぞれに対応した RemoteContoller.OnClientUpdateListener#onClientPlaybackStateUpdate が呼び出されます。