content: package com.faceunity.fulivedemo.utils;
  
  import android.arch.lifecycle.Lifecycle;
  import android.arch.lifecycle.LifecycleObserver;
  import android.arch.lifecycle.OnLifecycleEvent;
  import android.content.Context;
  import android.media.AudioAttributes;
  import android.media.AudioFocusRequest;
  import android.media.AudioManager;
  import android.os.Build;
  
  /**
   * 声音焦点管理类，监听 Activity 生命周期
   *
   * @author Richie on 2019.03.23
   */
  public final class AudioObserver implements LifecycleObserver {
  
      private Context mContext;
      private AudioFocusRequest mAudioFocusRequest;
  
      public AudioObserver(Context context) {
          mContext = context;
      }
  
      @OnLifecycleEvent(Lifecycle.Event.ON_START)
      public void onStart() {
          AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              AudioAttributes audioAttributes = new AudioAttributes.Builder()
                      .setUsage(AudioAttributes.USAGE_MEDIA)
                      .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                      .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                      .build();
              mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                      .setAudioAttributes(audioAttributes)
                      .build();
              audioManager.requestAudioFocus(mAudioFocusRequest);
          } else {
              audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
          }
      }
  
      @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
      public void oStop() {
          AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              audioManager.abandonAudioFocusRequest(mAudioFocusRequest);
          } else {
              audioManager.abandonAudioFocus(null);
          }
      }
  
  }