Commit 28b544c821ea5dd412ca5d56fe44be18800edfbb

Authored by 王琳
1 parent b0af9535

优化视频拍摄音频焦点

config.gradle
... ... @@ -21,7 +21,7 @@ ext {
21 21 //编译方式
22 22 debug : false,
23 23 //版本号
24   - version: "1.1.4.9",
  24 + version: "1.1.5.0",
25 25 //Lib库前缀
26 26 packeg : "com.cnlive.media",
27 27 //Lib库名称
... ...
lib_media/src/main/java/com/cnlive/media/ui/fragment/RecordFragment.java
1 1 package com.cnlive.media.ui.fragment;
2 2  
3 3 import android.Manifest;
  4 +import android.content.Context;
4 5 import android.content.pm.PackageManager;
5 6 import android.graphics.Bitmap;
  7 +import android.media.AudioManager;
6 8 import android.os.Bundle;
7 9 import android.text.TextUtils;
8 10 import android.util.Log;
... ... @@ -14,6 +16,7 @@ import androidx.core.app.ActivityCompat;
14 16  
15 17 import com.cnlive.libs.base.frame.fragment.MvpBindingFragment;
16 18 import com.cnlive.libs.base.util.DoublePressed;
  19 +import com.cnlive.libs.util.Config;
17 20 import com.cnlive.media.PickerConfig;
18 21 import com.cnlive.media.R;
19 22 import com.cnlive.media.databinding.FragmentRecordBinding;
... ... @@ -201,6 +204,7 @@ public class RecordFragment extends MvpBindingFragment<RecordFragmentView, Recor
201 204 } else if (state == RecordCameraViewObservable.CLICK_RECODE_START) {
202 205 //视频录制开始
203 206 if (isReady) {
  207 + requestAudioFocus();
204 208 isRecordIng = true;
205 209 plShortVideoRecorder.deleteAllSections();
206 210 plShortVideoRecorder.beginSection();
... ... @@ -210,6 +214,7 @@ public class RecordFragment extends MvpBindingFragment<RecordFragmentView, Recor
210 214 }
211 215 } else if (state == RecordCameraViewObservable.CLICK_RECODE_FINISH) {
212 216 //录制完成
  217 + abandonAudioFocus();
213 218 plShortVideoRecorder.endSection();
214 219 plShortVideoRecorder.concatSections(new PLVideoSaveListener() {
215 220 //视频保存完成
... ... @@ -247,4 +252,34 @@ public class RecordFragment extends MvpBindingFragment<RecordFragmentView, Recor
247 252 }
248 253 }
249 254  
  255 + private boolean mAudioFocus;
  256 +
  257 + /**
  258 + * 录音时获取音频焦点(关闭第三方音乐)
  259 + */
  260 + private void requestAudioFocus() {
  261 + if (mAudioFocus) return;
  262 + AudioManager audioManager = (AudioManager) Config.getContext().getSystemService(Context.AUDIO_SERVICE);
  263 + if (audioManager == null) return;
  264 +
  265 + int result = audioManager.requestAudioFocus(null,
  266 + AudioManager.STREAM_MUSIC, // Use the music stream.
  267 + AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
  268 + if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
  269 + mAudioFocus = true;
  270 + } else {
  271 + Log.e("media picker record", "AudioManager request Audio Focus result = " + result);
  272 + }
  273 + }
  274 +
  275 + /**
  276 + * 录音结束恢复音频焦点
  277 + */
  278 + private void abandonAudioFocus() {
  279 + if (!mAudioFocus) return;
  280 + AudioManager audioManager = (AudioManager) Config.getContext().getSystemService(Context.AUDIO_SERVICE);
  281 + if (audioManager == null) return;
  282 + audioManager.abandonAudioFocus(null);
  283 + mAudioFocus = false;
  284 + }
250 285 }
... ...