client-sdk-android icon indicating copy to clipboard operation
client-sdk-android copied to clipboard

when playing audio before connecting a room, play audio frozen for about a second

Open wzJun1 opened this issue 2 years ago • 6 comments

As the title, when we connect the room before, we played the music in advance. then call the connect room, the concert frozen for about a second.

need to use speaker for testing, since LiveKit defaults to earpiece, this will be very obvious.

the process of reproducing the problem is to use the speaker to play music in advance, and then connect the room to feel the problem.

eg:

//Suppose this is a code to play music
MediaPlayer.play();

//then we connect the rooms
room.connect(wsURL, token)
  
//use audioHandler.selectDevice to enable speaker

I feel like room.connect turned off the audio device during the connection process, and then suddenly turned it on again.

wzJun1 avatar Jun 29 '23 12:06 wzJun1

This is really a problem, my current solution is to use while to turn on the speaker before connect, but occasionally there are still problems.

  val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
  var timer = true
  var timerClockTime = System.currentTimeMillis()
  launch {
      withContext(Dispatchers.IO) {
          while (timer) {
              if (System.currentTimeMillis() - timerClockTime > 3000) {
                  connected = true
                  timer = false
              }
              audioManager.isSpeakerphoneOn = true
              audioManager.mode = AudioManager.MODE_NORMAL
          }
      }
  }

  launch {
      room.connect(
          wsURL,
          token,
      ) 
  }

I suspect that there is an abnormality in the switching of audio during the connection process of LiveKit Android SDK.

wzJun1 avatar Jul 27 '23 02:07 wzJun1

Because even if the speaker is turned on before connect, it will be turned off when the code executes to connect until the connection is successful.

wzJun1 avatar Jul 27 '23 02:07 wzJun1

You can change the default priority of the audio devices by passing in an AudioSwitchHandler with the preferredDeviceList set to have Speakerphone prioritized over Earpiece.

davidliu avatar Aug 03 '23 14:08 davidliu

@davidliu

still not working.

I have done the following configuration:


// Suppose this is a code to play music
MediaPlayer.play();

// eg:
var audioHandler = AudioSwitchHandler(application)
audioHandler.loggingEnabled = true
audioHandler.preferredDeviceList = listOf(
      AudioDevice.Speakerphone::class.java,
      AudioDevice.Earpiece::class.java,
      AudioDevice.BluetoothHeadset::class.java,
      AudioDevice.WiredHeadset::class.java,
)

var room = LiveKit.create(
    applicationContext, overrides = LiveKitOverrides(
        audioHandler = audioHandler
    ), options = options
)

// As long as the following connect is executed, the music will pause for 1-2s, and then continue
// If I remove the connect code, the playback will not be affected
room.connect(
    wsURL,
    token,
)

wzJun1 avatar Aug 04 '23 05:08 wzJun1

I think this might be an audio focus issue? Are you managing audio focus on your own? If that's the case, AudioSwitchHandler now has a manageAudioFocus flag to turn off the internal focus handling.

davidliu avatar Sep 13 '23 09:09 davidliu

I'm not sure because after I updated to version 1.2.2, this problem disappeared.

I will observe it for a while before commenting.

wzJun1 avatar Sep 13 '23 13:09 wzJun1