[Android] AudioFocus: Implement new API
Geoffrey Métais
git at videolan.org
Fri Jan 18 13:59:18 CET 2019
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Jan 18 11:53:40 2019 +0100| [8e9ec3785720b7e0ed369f5578fab834f48a1209] | committer: Geoffrey Métais
AudioFocus: Implement new API
> https://code.videolan.org/videolan/vlc-android/commit/8e9ec3785720b7e0ed369f5578fab834f48a1209
---
.../org/videolan/vlc/util/VLCAudioFocusHelper.kt | 38 ++++++++++++++++++++--
1 file changed, 35 insertions(+), 3 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/util/VLCAudioFocusHelper.kt b/vlc-android/src/org/videolan/vlc/util/VLCAudioFocusHelper.kt
index e71a1e276..f70e4c236 100644
--- a/vlc-android/src/org/videolan/vlc/util/VLCAudioFocusHelper.kt
+++ b/vlc-android/src/org/videolan/vlc/util/VLCAudioFocusHelper.kt
@@ -23,17 +23,28 @@
*/
package org.videolan.vlc.util
+import android.annotation.TargetApi
import android.content.Context
+import android.media.AudioAttributes
+import android.media.AudioFocusRequest
import android.media.AudioManager
+import android.os.Build
import android.util.Log
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.ObsoleteCoroutinesApi
+import org.videolan.libvlc.util.AndroidUtil
import org.videolan.vlc.BuildConfig
import org.videolan.vlc.PlaybackService
private const val TAG = "VLCAudioFocusHelper"
+ at ObsoleteCoroutinesApi
+ at ExperimentalCoroutinesApi
+ at Suppress("DEPRECATION")
class VLCAudioFocusHelper(private val service: PlaybackService) {
private lateinit var audioManager: AudioManager
+ private lateinit var audioFocusRequest : AudioFocusRequest
private var hasAudioFocus = false
@Volatile
internal var lossTransient = false
@@ -45,20 +56,41 @@ class VLCAudioFocusHelper(private val service: PlaybackService) {
if (acquire && !service.hasRenderer()) {
if (!hasAudioFocus) {
- val result = audioManager.requestAudioFocus(audioFocusListener,
- AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN)
+ val result = requestAudioFocus()
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
audioManager.setParameters("bgm_state=true")
hasAudioFocus = true
}
}
} else if (hasAudioFocus) {
- audioManager.abandonAudioFocus(audioFocusListener)
+ abandonAudioFocus()
audioManager.setParameters("bgm_state=false")
hasAudioFocus = false
}
}
+ @TargetApi(Build.VERSION_CODES.O)
+ private fun abandonAudioFocus() = if (AndroidUtil.isOOrLater) {
+ audioManager.abandonAudioFocusRequest(audioFocusRequest)
+ } else {
+ audioManager.abandonAudioFocus(audioFocusListener)
+ }
+
+ @TargetApi(Build.VERSION_CODES.O)
+ private fun requestAudioFocus() = if (AndroidUtil.isOOrLater) {
+ val attributes = AudioAttributes.Builder()
+ .setContentType(if (service.isVideoPlaying) AudioAttributes.CONTENT_TYPE_MOVIE else AudioAttributes.CONTENT_TYPE_MUSIC)
+ .build()
+ audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
+ .setOnAudioFocusChangeListener(audioFocusListener)
+ .setAudioAttributes(attributes)
+ .build()
+ audioManager.requestAudioFocus(audioFocusRequest)
+ } else {
+ audioManager.requestAudioFocus(audioFocusListener,
+ AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN)
+ }
+
private fun createOnAudioFocusChangeListener(): AudioManager.OnAudioFocusChangeListener {
return object : AudioManager.OnAudioFocusChangeListener {
private var lossTransientVolume = -1
More information about the Android
mailing list