Android音乐控制接口RemoteController使用
2016-05-18 11:27
477 查看
转载出处:https://segmentfault.com/a/1190000000713535
原文链接
RemoteController在API 19 引进,用来给音乐控制提供标准接口。长久以来,音乐播放在Android平台没有一个标准的接口,所有播放器都使用自己的方式实现对音乐的控制,最常见的方式是在Service中进行音乐播放,通过PendingIntent进行播放事件的传递及控制,因此就带来了一个问题。任何一个第三方app无法通过标准方式获取到当前正在播放的音乐的信息,更无法进行控制。RemoteController的出现恰好解决了这个问题,RemoteController需要和RemoteControlClient配合使用,从命名上能够看出,RemoteController是控制及信息获取端的接口,用来进行音乐信息的获取以及音乐播放动作的发送。RemoteControlClient是播放器端的接口,用来获取并执行播放动作,同时讲当前播放状态信息进行同步。
然而相关文档介绍很少。
使用方式
1.继承 NotificationListenerService 并实现RemoteController.OnClientUpdateListener接口来创建remoteController对象并获取播放进度
NotificationListenerService的主要作用是用来获取和操作通知栏通知,由于很奇葩的原因,为了获取合法的remoteController对象,必须实现这样一个NotificationListenerService并作为onClientUpdateListener传入remoteController来实现。
RemoteController的初始化传入的两个参数分别是context和updateListener,而且此处的context必须是notificationListenerService
2.获取播放信息
合法register之后在回调中会接收到播放信息,包括播放/暂停等动作信息以及歌曲的meta信息
音乐的meta信息从onClientMetadataUpdate回调中获取,能够获取到的字段包括歌手、名称、专辑名称、专辑封面等。注意此处获取到的专辑封面bitmap的尺寸是由注册remoteController时setArtworkConfiguration (int, int)来决定的
API文档
3.音乐控制
音乐的控制主要通过remoteController的sendMediaKeyEvent来实现
需要注意的是音乐的控制在逻辑上是模拟按钮的点击动作来实现的,所以在send一个keyCode时需要先后send KEY_ACTION_DOWN和KEY_ACTION_UP两个event来实现,所以我的实现是这样的
前面一部分是刚才讲到的通过remoteController来sendMediaKeyEvent,后面一部分是当remoteClient发生变化时remoteController的传递会失效,此时可以通过AudioManager来传递事件,
注意事项
1.注册RemoteController时OnClientUpdateListener必须是NotificationListenerService
2.发送KeyEvent时先发送ACTION_DOWN再发送ACTION_UP才是一个完整的事件;
Android RemoteController使用
原文链接RemoteController在API 19 引进,用来给音乐控制提供标准接口。长久以来,音乐播放在Android平台没有一个标准的接口,所有播放器都使用自己的方式实现对音乐的控制,最常见的方式是在Service中进行音乐播放,通过PendingIntent进行播放事件的传递及控制,因此就带来了一个问题。任何一个第三方app无法通过标准方式获取到当前正在播放的音乐的信息,更无法进行控制。RemoteController的出现恰好解决了这个问题,RemoteController需要和RemoteControlClient配合使用,从命名上能够看出,RemoteController是控制及信息获取端的接口,用来进行音乐信息的获取以及音乐播放动作的发送。RemoteControlClient是播放器端的接口,用来获取并执行播放动作,同时讲当前播放状态信息进行同步。
然而相关文档介绍很少。
使用方式
1.继承 NotificationListenerService 并实现RemoteController.OnClientUpdateListener接口来创建remoteController对象并获取播放进度
@TargetApi(Build.VERSION_CODES.KITKAT) public class MusicStateListener extends NotificationListenerService implements RemoteController.OnClientUpdateListener { }
NotificationListenerService的主要作用是用来获取和操作通知栏通知,由于很奇葩的原因,为了获取合法的remoteController对象,必须实现这样一个NotificationListenerService并作为onClientUpdateListener传入remoteController来实现。
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的初始化传入的两个参数分别是context和updateListener,而且此处的context必须是notificationListenerService
2.获取播放信息
合法register之后在回调中会接收到播放信息,包括播放/暂停等动作信息以及歌曲的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); };
音乐的meta信息从onClientMetadataUpdate回调中获取,能够获取到的字段包括歌手、名称、专辑名称、专辑封面等。注意此处获取到的专辑封面bitmap的尺寸是由注册remoteController时setArtworkConfiguration (int, int)来决定的
API文档
3.音乐控制
音乐的控制主要通过remoteController的sendMediaKeyEvent来实现
需要注意的是音乐的控制在逻辑上是模拟按钮的点击动作来实现的,所以在send一个keyCode时需要先后send KEY_ACTION_DOWN和KEY_ACTION_UP两个event来实现,所以我的实现是这样的
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(); } } }
前面一部分是刚才讲到的通过remoteController来sendMediaKeyEvent,后面一部分是当remoteClient发生变化时remoteController的传递会失效,此时可以通过AudioManager来传递事件,
注意事项
1.注册RemoteController时OnClientUpdateListener必须是NotificationListenerService
2.发送KeyEvent时先发送ACTION_DOWN再发送ACTION_UP才是一个完整的事件;
相关文章推荐
- 十大Material Design开源项目
- Android WheelView 详解
- 推荐几款优秀的Android Studio插件
- android studio jni环境搭建
- Android 如何调用第三方.so (.a) 文件
- Android获取WIFI状态下的IP地址以及MAC地址
- Android引用方式
- android工程res/drawable目录适配的分辨率
- 项目分离
- Android线程池详解
- Android Studio如何设置代码自动提示
- 进击的Android注入术《五》
- Android之高德地图定位及附近搜索
- 谁来讲讲Rxjava、rxandroid中的操作符的作用?
- Android--通知之Notification
- android开发 drawable(hdpi,mdpi,ldpi)的区别
- 进击的Android注入术《四》
- 通过settings.db自定义Android系统默认设置:SHOW_IME_WITH_HARD_KEYBOARD 默认物理键盘与软键盘同时使用
- Android技术积累:开发规范
- Android Gradle配置Debug和release参数的方法