Android API Guide for Media Apps(六)——媒体会话的回调(Media Session Callbacks)
来源:互联网 发布:开发聊天软件 编辑:程序博客网 时间:2024/05/07 01:59
Media Session Callbacks
媒体会话回调调用不同的接口方法来控制播放器,管理音频焦点,并实现媒体会话和媒体浏览器服务间的通信。下面的列表总结这些任务如何分布在回调中。
回调的示例架构:
private IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);// Defined elsewhere...private AudioManager.OnAudioFocusChangeListener afChangeListener;private BecomingNoisyReceiver myNoisyAudioStreamReceiver = new BecomingNoisyReceiver();private MediaStyleNotification myPlayerNotification;private MediaSessionCompat mediaSession;private MediaBrowserService service;private SomeKindOfPlayer player;MediaSessionCompat.Callback callback = newMediaSessionCompat.Callback() { @Override public void onPlay() { AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE); // Request audio focus for playback, this registers the afChangeListener int result = am.requestAudioFocus(afChangeListener, // Use the music stream. AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { // Start the service service.start(); // Set the session active (and update metadata and state) mediaSession.setActive(true); // start the player (custom call) player.start(); // Register BECOME_NOISY BroadcastReceiver registerReceiver(myNoisyAudioStreamReceiver, intentFilter); // Put the service in the foreground, post notification service.startForeground(myPlayerNotification); } } @Override public void onStop() { AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE); // Abandon audio focus am.abandonAudioFocus(afChangeListener); unregisterReceiver(myNoisyAudioStreamReceiver); // Start the service service.stop(self); // Set the session inactive (and update metadata and state) mediaSession.setActive(false); // stop the player (custom call) player.stop(); // Take the service out of the foreground, remove notification service.stopForeground(true); } @Override public void onPause() { AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE); // Update metadata and state // pause the player (custom call) player.pause(); // unregister BECOME_NOISY BroadcastReceiver unregisterReceiver(myNoisyAudioStreamReceiver, intentFilter); // Take the service out of the foreground, retain the notification service.stopForeground(false); }}
0 0
- Android API Guide for Media Apps(六)——媒体会话的回调(Media Session Callbacks)
- Android API Guide for Media Apps(九)——媒体会话的回调(Media Session Callbacks)
- Android API Guide for Media Apps(二)——媒体会话的使用(Working with a Mession)
- Android API Guide for Media Apps —— 媒体应用
- Android API Guide for Media Apps(四)—— 构建媒体浏览器服务(Building a Media Browser Service)
- Android API Guide for Media Apps(五)——构建媒体浏览客户端(Building a Media Browser Client)
- Android API Guide for Media Apps(一)——概览(Overview)
- Android API Guide for Media and Camera(一)—— 媒体与相机(媒体播放器)
- Android API Guide for Media and Camera(三)—— 媒体与相机(ExoPlayer)
- Android API Guide for Media and Camera(二)—— 媒体与相机(媒体记录器)
- Android API Guide for Media and Camera(四)—— 媒体与相机(媒体路由)
- Android API Guide for Media and Camera(五)—— 媒体与相机(Camera API)
- Android API Guide for Media Apps(三)—— 构建音频应用(Building an Audio App)
- Android API Guide for Media Apps(七)——构建一个视频应用(Building a Video App)
- Android API Guide for Media Apps(八)——构建一个视频播放器Activity(Building a Video Player Activity)
- PhoneGap API中文帮助文档——Media(媒体)
- PhoneGap API帮助文档翻译—Media(媒体)
- PhoneGap API帮助文档翻译—Media(媒体)
- JAVA枚举类
- 用 TensorFlow 创建自己的 Speech Recognizer
- css绝对定位和相对定位的理解
- ppp与pppoe 交叉编译移植
- 论人工智能技术在推荐系统中的应用
- Android API Guide for Media Apps(六)——媒体会话的回调(Media Session Callbacks)
- 大公司高级Android工程师技能要求
- 欢迎使用CSDN-markdown编辑器
- KNN与K-Means的区别
- Windows下PCL及其第三方库的安装编译过程
- Node.js 给前端带来了什么?
- Spring Boot Actuator监控端点
- Android-Activity的四种启动方式
- .net core中加载lua脚本的类库- MoonSharp