Set AudioAttributes in LocalMediaPlayer

This commit is contained in:
tzugen 2021-04-22 10:59:05 +02:00
parent e63f24242b
commit f27212f263
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
1 changed files with 17 additions and 2 deletions

View File

@ -360,7 +360,7 @@ class LocalMediaPlayer(
secondaryProgress = -1 // Ensure seeking in non StreamProxy playback works secondaryProgress = -1 // Ensure seeking in non StreamProxy playback works
setPlayerState(PlayerState.IDLE) setPlayerState(PlayerState.IDLE)
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC) setAudioAttributes(mediaPlayer)
var dataSource = file.path var dataSource = file.path
if (partial) { if (partial) {
@ -433,6 +433,15 @@ class LocalMediaPlayer(
} }
} }
private fun setAudioAttributes(player: MediaPlayer) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
player.setAudioAttributes(AudioFocusHandler.getAudioAttributes())
} else {
@Suppress("DEPRECATION")
player.setAudioStreamType(AudioManager.STREAM_MUSIC)
}
}
@Synchronized @Synchronized
private fun setupNext(downloadFile: DownloadFile) { private fun setupNext(downloadFile: DownloadFile) {
try { try {
@ -446,11 +455,17 @@ class LocalMediaPlayer(
} }
nextMediaPlayer = MediaPlayer() nextMediaPlayer = MediaPlayer()
nextMediaPlayer!!.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK) nextMediaPlayer!!.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK)
setAudioAttributes(nextMediaPlayer!!)
// This has nothing to do with the MediaSession, it is used to associate
// the equalizer or visualizer with the player
try { try {
nextMediaPlayer!!.audioSessionId = mediaPlayer.audioSessionId nextMediaPlayer!!.audioSessionId = mediaPlayer.audioSessionId
} catch (e: Throwable) { } catch (e: Throwable) {
nextMediaPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC)
} }
nextMediaPlayer!!.setDataSource(file.path) nextMediaPlayer!!.setDataSource(file.path)
setNextPlayerState(PlayerState.PREPARING) setNextPlayerState(PlayerState.PREPARING)
nextMediaPlayer!!.setOnPreparedListener { nextMediaPlayer!!.setOnPreparedListener {