Skip to content

Commit

Permalink
Merge pull request #1097 from iscle/dev
Browse files Browse the repository at this point in the history
Fix audio effect control intent broadcast
  • Loading branch information
z-huang authored Jan 11, 2024
2 parents 5799ecf + 9d1330c commit 1ee7fc1
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/src/main/java/com/zionhuang/music/playback/MusicService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ class MusicService : MediaLibraryService(),
lateinit var player: ExoPlayer
private lateinit var mediaSession: MediaLibrarySession

private var isAudioEffectSessionOpened = false

override fun onCreate() {
super.onCreate()
setMediaNotificationProvider(
Expand Down Expand Up @@ -469,6 +471,8 @@ class MusicService : MediaLibraryService(),
}

private fun openAudioEffectSession() {
if (isAudioEffectSessionOpened) return
isAudioEffectSessionOpened = true
sendBroadcast(
Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION).apply {
putExtra(AudioEffect.EXTRA_AUDIO_SESSION, player.audioSessionId)
Expand All @@ -479,9 +483,12 @@ class MusicService : MediaLibraryService(),
}

private fun closeAudioEffectSession() {
if (!isAudioEffectSessionOpened) return
isAudioEffectSessionOpened = false
sendBroadcast(
Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION).apply {
putExtra(AudioEffect.EXTRA_AUDIO_SESSION, player.audioSessionId)
putExtra(AudioEffect.EXTRA_PACKAGE_NAME, packageName)
}
)
}
Expand Down Expand Up @@ -511,8 +518,9 @@ class MusicService : MediaLibraryService(),
}

override fun onEvents(player: Player, events: Player.Events) {
if (events.containsAny(EVENT_PLAYBACK_STATE_CHANGED, EVENT_PLAY_WHEN_READY_CHANGED, EVENT_IS_PLAYING_CHANGED, EVENT_POSITION_DISCONTINUITY)) {
if (player.playbackState != STATE_ENDED && player.playWhenReady) {
if (events.containsAny(Player.EVENT_PLAYBACK_STATE_CHANGED, Player.EVENT_PLAY_WHEN_READY_CHANGED)) {
val isBufferingOrReady = player.playbackState == Player.STATE_BUFFERING || player.playbackState == Player.STATE_READY
if (isBufferingOrReady && player.playWhenReady) {
openAudioEffectSession()
} else {
closeAudioEffectSession()
Expand Down

0 comments on commit 1ee7fc1

Please sign in to comment.