Android RemoteController使用
RemoteController在API 19 引進(jìn),用來(lái)給音樂(lè)控制提供標(biāo)準(zhǔn)接口。長(zhǎng)久以來(lái),音樂(lè)播放在Android平臺(tái)沒有一個(gè)標(biāo)準(zhǔn)的接口,所有播放器都使用自己的方式實(shí)現(xiàn)對(duì)音樂(lè)的控制,最常見的方式是在Service中進(jìn)行音樂(lè)播放,通過(guò)PendingIntent進(jìn)行播放事件的傳遞及控制,因此就帶來(lái)了一個(gè)問(wèn)題。任何一個(gè)第三方app無(wú)法通過(guò)標(biāo)準(zhǔn)方式獲取到當(dāng)前正在播放的音樂(lè)的信息,更無(wú)法進(jìn)行控制。RemoteController的出現(xiàn)恰好解決了這個(gè)問(wèn)題,RemoteController需要和RemoteControlClient配合使用,從命名上能夠看出,RemoteController是控制及信息獲取端的接口,用來(lái)進(jìn)行音樂(lè)信息的獲取以及音樂(lè)播放動(dòng)作的發(fā)送。RemoteControlClient是播放器端的接口,用來(lái)獲取并執(zhí)行播放動(dòng)作,同時(shí)講當(dāng)前播放狀態(tài)信息進(jìn)行同步。
1.繼承 NotificationListenerService 并實(shí)現(xiàn)RemoteController.OnClientUpdateListener接口來(lái)創(chuàng)建remoteController對(duì)象并獲取播放進(jìn)度
@TargetApi(Build.VERSION_CODES.KITKAT) public class MusicStateListener extends NotificationListenerServiceimplements RemoteController.OnClientUpdateListener { }NotificationListenerService的主要作用是用來(lái)獲取和操作通知欄通知,由于很奇葩的原因,為了獲取合法的remoteController對(duì)象,必須實(shí)現(xiàn)這樣一個(gè)NotificationListenerService并作為onClientUpdateListener傳入remoteController來(lái)實(shí)現(xiàn)。
public void registerRemoteController() {remoteController = new RemoteController(this, this);boolean registered;try {registered = ((AudioManager) getSystemService(AUDIO_SERVICE)).registerRemoteController(remoteController);} catch (NullPointerException e) {registered = false;}if (registered) {try {remoteController.setArtworkConfiguration(getResources().getDimensionPixelSize(R.dimen.remote_artwork_bitmap_width),getResources().getDimensionPixelSize(R.dimen.remote_artwork_bitmap_height));remoteController.setSynchronizationMode(RemoteController.POSITION_SYNCHRONIZATION_CHECK);} catch (IllegalArgumentException e) {e.printStackTrace();}}}RemoteController的初始化傳入的兩個(gè)參數(shù)分別是context和updateListener,而且此處的context必須是notificationListenerService
2.獲取播放信息
合法register之后在回調(diào)中會(huì)接收到播放信息,包括播放/暫停等動(dòng)作信息以及歌曲的meta信息
/*** Interface definition for the callbacks to be invoked whenever media events, metadata* and playback status are available.*/ public interface OnClientUpdateListener {/*** Called whenever all information, previously received through the other* methods of the listener, is no longer valid and is about to be refreshed.* This is typically called whenever a new {@link RemoteControlClient} has been selected* by the system to have its media information published.* @param clearing true if there is no selected RemoteControlClient and no information* is available.*/public void onClientChange(boolean clearing);/*** Called whenever the playback state has changed.* It is called when no information is known about the playback progress in the media and* the playback speed.* @param state one of the playback states authorized* in {@link RemoteControlClient#setPlaybackState(int)}.*/public void onClientPlaybackStateUpdate(int state);/*** Called whenever the playback state has changed, and playback position* and speed are known.* @param state one of the playback states authorized* in {@link RemoteControlClient#setPlaybackState(int)}.* @param stateChangeTimeMs the system time at which the state change was reported,* expressed in ms. Based on {@link android.os.SystemClock#elapsedRealtime()}.* @param currentPosMs a positive value for the current media playback position expressed* in ms, a negative value if the position is temporarily unknown.* @param speed a value expressed as a ratio of 1x playback: 1.0f is normal playback,* 2.0f is 2x, 0.5f is half-speed, -2.0f is rewind at 2x speed. 0.0f means nothing is* playing (e.g. when state is {@link RemoteControlClient#PLAYSTATE_ERROR}).*/public void onClientPlaybackStateUpdate(int state, long stateChangeTimeMs,long currentPosMs, float speed);/*** Called whenever the transport control flags have changed.* @param transportControlFlags one of the flags authorized* in {@link RemoteControlClient#setTransportControlFlags(int)}.*/public void onClientTransportControlUpdate(int transportControlFlags);/*** Called whenever new metadata is available.* See the {@link MediaMetadataEditor#putLong(int, long)},* {@link MediaMetadataEditor#putString(int, String)},* {@link MediaMetadataEditor#putBitmap(int, Bitmap)}, and* {@link MediaMetadataEditor#putObject(int, Object)} methods for the various keys that* can be queried.* @param metadataEditor the container of the new metadata.*/public void onClientMetadataUpdate(MetadataEditor metadataEditor); };音樂(lè)的meta信息從onClientMetadataUpdate回調(diào)中獲取,能夠獲取到的字段包括歌手、名稱、專輯名稱、專輯封面等。注意此處獲取到的專輯封面bitmap的尺寸是由注冊(cè)remoteController時(shí)setArtworkConfiguration (int, int)來(lái)決定的
API文檔
3.音樂(lè)控制
音樂(lè)的控制主要通過(guò)remoteController的sendMediaKeyEvent來(lái)實(shí)現(xiàn)?需要注意的是音樂(lè)的控制在邏輯上是模擬按鈕的點(diǎn)擊動(dòng)作來(lái)實(shí)現(xiàn)的,所以在send一個(gè)keyCode時(shí)需要先后send KEY_ACTION_DOWN和KEY_ACTION_UP兩個(gè)event來(lái)實(shí)現(xiàn),所以我的實(shí)現(xiàn)是這樣的
public boolean sendMusicKeyEvent(int keyCode) {if (!clientIdLost && remoteController != null) {// send "down" and "up" key events.KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);boolean down = remoteController.sendMediaKeyEvent(keyEvent);keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);boolean up = remoteController.sendMediaKeyEvent(keyEvent);return down && up;} else {long eventTime = SystemClock.uptimeMillis();KeyEvent key = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, keyCode, 0);dispatchMediaKeyToAudioService(key);dispatchMediaKeyToAudioService(KeyEvent.changeAction(key, KeyEvent.ACTION_UP));}return false;}private void dispatchMediaKeyToAudioService(KeyEvent event) {AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);if (audioManager != null) {try {audioManager.dispatchMediaKeyEvent(event);} catch (Exception e) {e.printStackTrace();}}}前面一部分是剛才講到的通過(guò)remoteController來(lái)sendMediaKeyEvent,后面一部分是當(dāng)remoteClient發(fā)生變化時(shí)remoteController的傳遞會(huì)失效,此時(shí)可以通過(guò)AudioManager來(lái)傳遞事件,
注意事項(xiàng)
1.注冊(cè)RemoteController時(shí)OnClientUpdateListener必須是NotificationListenerService?2.發(fā)送KeyEvent時(shí)先發(fā)送ACTION_DOWN再發(fā)送ACTION_UP才是一個(gè)完整的事件;
- 2014年10月08日發(fā)布?
- 更多
總結(jié)
以上是生活随笔為你收集整理的Android RemoteController使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于ViewTreeObserver的理
- 下一篇: Android虚拟机-Dalvik VM