Android音乐控制接口RemoteController使用
来源:互联网 发布:淘宝潮男店铺粉丝排行 编辑:程序博客网 时间:2024/06/05 07:09
转载出处:https://segmentfault.com/a/1190000000713535
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才是一个完整的事件;
- Android音乐控制接口RemoteController使用
- Android音乐控制接口RemoteController使用
- Android音乐控制接口RemoteController使用
- Android RemoteController使用
- Android:音乐特效控制
- android下使用xstream解析百度音乐接口
- android,锁屏控制音乐
- android 锁屏音乐控制
- 使用MCI接口播放音乐
- 使用MCI接口播放音乐
- Android音乐播放器---实现Notification控制音乐播放
- Android音乐播放器---实现Notification控制音乐播放
- Android音乐播放器---实现Notification控制音乐播放
- Android音乐播放器---实现Notification控制音乐播放
- Android接口调用控制
- android 音乐、铃声播放控制相关问题
- Android中音乐开关的控制
- android通过耳机控制音乐播放器
- JavaScript小技巧
- HTML中让边框变成圆形并且带有阴影
- poj 1390 Blocks(记忆化搜索/动态规划)
- IOS开发之简单音频播放器
- as下删除无用的图片
- Android音乐控制接口RemoteController使用
- NYOJ 题目711 最舒适的路线
- IOS 多线程编程指南_GCD
- 大数据入门:各种大数据技术介绍
- [Book Note] Haskell: Monoid
- solution Of 1098. Insertion or Heap Sort (25)
- APP事件表通用字段视图的实现
- 安装HPL、GotoBLAS2、openmpi
- gradle dsl method not found : 'android()'