We are using Livekit for playing webRTC streams. B...
# ask-ai
s
We are using Livekit for playing webRTC streams. But when we try to increase or decrease the volume it is taking call controls and not media controls on android. How to switch to media control from call control on Android
t
Thanks for reaching out, Rajesh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Are you using the LiveKit React Native SDK or the native Android SDK for your implementation? Knowing this will really help me guide you to the best way to switch from call control to media control. I'm here to help you through this! Sources: livekit-android-camerax/src/main/java/livekit/org/webrtc/CameraXSession.kt | Android quickstart | LiveKit Docs | Android quickstart (Jetpack Compose) | LiveKit Docs | android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java | android/src/main/java/com/livekit/reactnative/LiveKitReactNative.kt
s
Android SDK
t
To switch from call control to media control in the LiveKit Android SDK, I’m here to guide you through the process step-by-step. You can easily configure the audio settings using the
LiveKitOverrides
class when creating a
Room
object. Let’s take a look at how you can do this: • Using Predefined Audio Types: The simplest and most recommended way is to use the
MediaAudioType
for media playback. Here’s how you can set it up:
Copy code
val room = LiveKit.create(
    appContext = application,
    overrides = LiveKitOverrides(
        audioOptions = AudioOptions(
            audioOutputType = AudioType.MediaAudioType()
        )
    )
)
• Customizing Audio Attributes: If you want more control over your audio settings, you can use
CustomAudioType
to define specific audio attributes tailored to your needs:
Copy code
val audioAttributes = AudioAttributes.Builder()
    .setUsage(AudioAttributes.USAGE_MEDIA)
    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
    .build()

val customAudioType = AudioType.CustomAudioType(
    audioMode = AudioManager.MODE_NORMAL,
    audioAttributes = audioAttributes,
    audioStreamType = AudioManager.STREAM_MUSIC
)

val room = LiveKit.create(
    appContext = application,
    overrides = LiveKitOverrides(
        audioOptions = AudioOptions(
            audioOutputType = customAudioType
        )
    )
)
• Optional Audio Focus Management: For even more advanced control, you might find
AudioFocusHandler
useful to manage audio focus effectively:
Copy code
val audioHandler = AudioFocusHandler(context).apply {
    focusMode = AudioManager.AUDIOFOCUS_GAIN
    audioAttributeUsageType = AudioAttributes.USAGE_MEDIA
    audioAttributeContentType = AudioAttributes.CONTENT_TYPE_MUSIC
}

val room = LiveKit.create(
    appContext = application,
    overrides = LiveKitOverrides(
        audioOptions = AudioOptions(
            audioHandler = audioHandler,
            audioOutputType = AudioType.MediaAudioType()
        )
    )
)
I hope this helps you make the switch smoothly! If you have any questions or need more help with any part of this, please don’t hesitate to ask. I’m here to support you! Sources: README.md | README.md
s
This solution doesnt seem to be working on older devices
t
For older Android devices, audio control can sometimes act a bit differently because of legacy routing systems. Don’t worry though—I’m here to help you make some adjustments that should improve compatibility: • Set Legacy Audio Stream Type: For devices before Android O, try using `AudioManager.STREAM_VOICE_CALL`:
Copy code
val customAudioType = AudioType.CustomAudioType(
    audioMode = AudioManager.MODE_IN_COMMUNICATION,
    audioAttributes = null, // Optional for older devices
    audioStreamType = AudioManager.STREAM_VOICE_CALL
)

val room = LiveKit.create(
    appContext = application,
    overrides = LiveKitOverrides(
        audioOptions = AudioOptions(
            audioOutputType = customAudioType
        )
    )
)
• Force Audio Routing: If you’re facing issues on certain devices, enabling forced routing can help:
Copy code
val audioHandler = AudioFocusHandler(context).apply {
    forceHandleAudioRouting = true
}

val room = LiveKit.create(
    appContext = application,
    overrides = LiveKitOverrides(
        audioOptions = AudioOptions(
            audioHandler = audioHandler
        )
    )
)
• Maintain Communication Mode: Using a silent
AudioTrack
can prevent Android from resetting
MODE_IN_COMMUNICATION
after some inactivity:
Copy code
val audioOptions = AudioOptions(
    disableCommunicationModeWorkaround = false // Default behavior
)
• Preferred Device List: You can customize the priority order for audio output devices like this:
Copy code
audioSwitchHandler.preferredDeviceList = listOf(
    AudioDevice.Speakerphone::class.java,
    AudioDevice.Earpiece::class.java,
    AudioDevice.BluetoothHeadset::class.java
)
I hope these tips help your setup work smoothly on older devices! If you have any more questions or need a hand with anything else, just let me know—I’m here for you. Sources: livekit-android-sdk/src/main/java/io/livekit/android/LiveKitOverrides.kt | android/src/main/java/com/livekit/reactnative/LivekitReactNativeModule.kt