• 在Activity不可见时暂停WebView的语音播放,可见时继续播放之前的语音


    private AudioManager mAudioManager;
    private AudioManager.OnAudioFocusChangeListener mFocusChangeListener;

    @Override
    protected void onPause() {
      super.onPause();
      stopPlayVoice();
    }

    @Override
    protected void onResume() {
      super.onResume();
      startPlayVoice();
    }

    public void stopPlayVoice() {
      if (mAudioManager == null) {
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        mFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
          @Override
          public void onAudioFocusChange(int focusChange) {
          }
        };
      }
      for (int i = 0; i < 10; i++) {  //尝试10次请求AudioFocus,一般一两次可以请求到,一旦请求到,语音将暂停播放
        if (mAudioManager.requestAudioFocus(mFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
          break;
        }
        SystemClock.sleep(100);
      }
    }

    private void startPlayVoice() {
      if (mAudioManager != null) {
        mAudioManager.abandonAudioFocus(mFocusChangeListener);//放弃并还回拿到的AudioFocus,语音将继续播放
      }
    }

  • 相关阅读:
    JVM理论:(一/2)OutOfMemoryError异常
    JVM理论:(一/1)对象的创建过程
    JVM理论:(一)JVM内存模型
    MySQL优化(6):Mysql锁机制
    MySQL优化(5):索引失效分析、in与exists使用场合
    MySQL优化(4):explain分析
    MySQL优化(3):慢SQL分析
    MySQL优化(2):索引简述
    MySQL优化(1):Mysql简述
    MySQL基础(4):事务控制
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/8399406.html
Copyright © 2020-2023  润新知