Skip to content

Commit

Permalink
fix AudioSessionId 获取不到问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lizixian committed Mar 29, 2021
1 parent 7fb8f14 commit fb407a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class PlayerControl(
isRunningTimeTask = true
val position = getPlayingPosition()
val duration = getDuration()
val effectSwitch = StarrySkyConstant.keyEffectSwitch
if (effectSwitch) {
StarrySky.effect().attachAudioEffect(getAudioSessionId())
}
progressListener.forEach {
it.value.onPlayProgress(position, duration)
}
Expand Down Expand Up @@ -572,10 +576,6 @@ class PlayerControl(
when (playbackStage.stage) {
PlaybackStage.PLAYING -> {
timerTaskManager?.startToUpdateProgress()
val effectSwitch = StarrySkyConstant.keyEffectSwitch
if (effectSwitch) {
StarrySky.effect().attachAudioEffect(getAudioSessionId())
}
}
PlaybackStage.PAUSE,
PlaybackStage.ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.lzx.starrysky.control
import android.media.audiofx.BassBoost
import android.media.audiofx.Equalizer
import android.media.audiofx.Virtualizer
import com.lzx.starrysky.StarrySky
import com.lzx.starrysky.utils.StarrySkyConstant

/**
Expand Down Expand Up @@ -31,6 +32,7 @@ class VoiceEffect {

releaseAudioEffect()

StarrySky.log("audioSessionId = $audioSessionId")
if (audioSessionId == 0) return

currSessionId = audioSessionId
Expand Down
23 changes: 19 additions & 4 deletions starrysky/src/main/java/com/lzx/starrysky/playback/ExoPlayback.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.PlaybackParameters
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.analytics.AnalyticsListener
import com.google.android.exoplayer2.analytics.AnalyticsListener.EventTime
import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory
import com.google.android.exoplayer2.source.MediaSource
Expand Down Expand Up @@ -39,6 +41,7 @@ import com.lzx.starrysky.utils.isFLAC
import com.lzx.starrysky.utils.isRTMP
import com.lzx.starrysky.utils.orDef


/**
* isAutoManagerFocus 是否让播放器自动管理焦点
*/
Expand All @@ -60,10 +63,13 @@ class ExoPlayback(val context: Context,

private var currSongInfo: SongInfo? = null
private var callback: Playback.Callback? = null
private val mEventListener by lazy { ExoPlayerEventListener() }
private val eventListener by lazy { ExoPlayerEventListener() }
private val analyticsListener by lazy { ExoAnalyticsListener() }
private var sourceTypeErrorInfo: SourceTypeErrorInfo = SourceTypeErrorInfo()
private var focusManager = FocusManager(context)
private var hasError = false
private var playerAudioSessionId = 0


init {
focusManager.listener = this
Expand Down Expand Up @@ -110,7 +116,7 @@ class ExoPlayback(val context: Context,

override fun getCurrPlayInfo(): SongInfo? = currSongInfo

override fun getAudioSessionId(): Int = player?.audioSessionId.orDef()
override fun getAudioSessionId(): Int = playerAudioSessionId

private fun getPlayWhenReady() = player?.playWhenReady ?: false

Expand Down Expand Up @@ -145,6 +151,7 @@ class ExoPlayback(val context: Context,
if (mediaHasChanged || player == null) {
//创建播放器实例
createExoPlayer()

player?.setMediaSource(mediaSource!!)
player?.prepare()
if (!isAutoManagerFocus) {
Expand Down Expand Up @@ -275,8 +282,9 @@ class ExoPlayback(val context: Context,
.setTrackSelector(trackSelector!!)
.build()

player?.addListener(mEventListener)
player?.addListener(eventListener)
player?.setAudioAttributes(AudioAttributes.DEFAULT, isAutoManagerFocus)
player?.addAnalyticsListener(analyticsListener)
if (!isAutoManagerFocus) {
player?.playbackState?.let { focusManager.updateAudioFocus(getPlayWhenReady(), it) }
}
Expand Down Expand Up @@ -315,7 +323,8 @@ class ExoPlayback(val context: Context,
override fun stop() {
player?.stop(true)
player?.release()
player?.removeListener(mEventListener)
player?.removeListener(eventListener)
player?.removeAnalyticsListener(analyticsListener)
player = null
if (!isAutoManagerFocus) {
focusManager.release()
Expand Down Expand Up @@ -385,6 +394,12 @@ class ExoPlayback(val context: Context,
this.callback = callback
}

private inner class ExoAnalyticsListener : AnalyticsListener {
override fun onAudioSessionId(eventTime: EventTime, audioSessionId: Int) {
playerAudioSessionId = audioSessionId
}
}

private inner class ExoPlayerEventListener : Player.EventListener {

override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
Expand Down

0 comments on commit fb407a2

Please sign in to comment.