Android API Guide for Media Apps(六)——媒体会话的回调(Media Session Callbacks)

Media Session Callbacks

媒体会话回调调用不同的接口方法来控制播放器,管理音频焦点,并实现媒体会话和媒体浏览器服务间的通信。下面的列表总结这些任务如何分布在回调中。

_onPlay()onPause()onStop()
Audio FocusrequestFocus() passing in your OnAudioFocusChangeListener. Always call requestFocus() first, proceed only if focus is granted.abandonAudioFocus()
ServicestartService()stopSelf()
Media SessionsetActive(true) - Update metadata and state-Update metadata and statesetActive(false) - Update metadata and state
Player ImplementationStart the playerPause the playerStop the player
Becoming NoisyRegister your BroadcastReceiverUnregister your BroadcastReceiver
NotificationsstartForeground(notification)stopForeground(false)stopForeground(true)

回调的示例架构:

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 = new
MediaSessionCompat.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值