Commit 28b544c821ea5dd412ca5d56fe44be18800edfbb
1 parent
b0af9535
优化视频拍摄音频焦点
Showing
2 changed files
with
36 additions
and
1 deletions
config.gradle
lib_media/src/main/java/com/cnlive/media/ui/fragment/RecordFragment.java
| 1 | package com.cnlive.media.ui.fragment; | 1 | package com.cnlive.media.ui.fragment; |
| 2 | 2 | ||
| 3 | import android.Manifest; | 3 | import android.Manifest; |
| 4 | +import android.content.Context; | ||
| 4 | import android.content.pm.PackageManager; | 5 | import android.content.pm.PackageManager; |
| 5 | import android.graphics.Bitmap; | 6 | import android.graphics.Bitmap; |
| 7 | +import android.media.AudioManager; | ||
| 6 | import android.os.Bundle; | 8 | import android.os.Bundle; |
| 7 | import android.text.TextUtils; | 9 | import android.text.TextUtils; |
| 8 | import android.util.Log; | 10 | import android.util.Log; |
| @@ -14,6 +16,7 @@ import androidx.core.app.ActivityCompat; | @@ -14,6 +16,7 @@ import androidx.core.app.ActivityCompat; | ||
| 14 | 16 | ||
| 15 | import com.cnlive.libs.base.frame.fragment.MvpBindingFragment; | 17 | import com.cnlive.libs.base.frame.fragment.MvpBindingFragment; |
| 16 | import com.cnlive.libs.base.util.DoublePressed; | 18 | import com.cnlive.libs.base.util.DoublePressed; |
| 19 | +import com.cnlive.libs.util.Config; | ||
| 17 | import com.cnlive.media.PickerConfig; | 20 | import com.cnlive.media.PickerConfig; |
| 18 | import com.cnlive.media.R; | 21 | import com.cnlive.media.R; |
| 19 | import com.cnlive.media.databinding.FragmentRecordBinding; | 22 | import com.cnlive.media.databinding.FragmentRecordBinding; |
| @@ -201,6 +204,7 @@ public class RecordFragment extends MvpBindingFragment<RecordFragmentView, Recor | @@ -201,6 +204,7 @@ public class RecordFragment extends MvpBindingFragment<RecordFragmentView, Recor | ||
| 201 | } else if (state == RecordCameraViewObservable.CLICK_RECODE_START) { | 204 | } else if (state == RecordCameraViewObservable.CLICK_RECODE_START) { |
| 202 | //视频录制开始 | 205 | //视频录制开始 |
| 203 | if (isReady) { | 206 | if (isReady) { |
| 207 | + requestAudioFocus(); | ||
| 204 | isRecordIng = true; | 208 | isRecordIng = true; |
| 205 | plShortVideoRecorder.deleteAllSections(); | 209 | plShortVideoRecorder.deleteAllSections(); |
| 206 | plShortVideoRecorder.beginSection(); | 210 | plShortVideoRecorder.beginSection(); |
| @@ -210,6 +214,7 @@ public class RecordFragment extends MvpBindingFragment<RecordFragmentView, Recor | @@ -210,6 +214,7 @@ public class RecordFragment extends MvpBindingFragment<RecordFragmentView, Recor | ||
| 210 | } | 214 | } |
| 211 | } else if (state == RecordCameraViewObservable.CLICK_RECODE_FINISH) { | 215 | } else if (state == RecordCameraViewObservable.CLICK_RECODE_FINISH) { |
| 212 | //录制完成 | 216 | //录制完成 |
| 217 | + abandonAudioFocus(); | ||
| 213 | plShortVideoRecorder.endSection(); | 218 | plShortVideoRecorder.endSection(); |
| 214 | plShortVideoRecorder.concatSections(new PLVideoSaveListener() { | 219 | plShortVideoRecorder.concatSections(new PLVideoSaveListener() { |
| 215 | //视频保存完成 | 220 | //视频保存完成 |
| @@ -247,4 +252,34 @@ public class RecordFragment extends MvpBindingFragment<RecordFragmentView, Recor | @@ -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 | } |