Audio Share
These are Android only APIs. This feature is the analog of screen capture, but for audio. There may be cases where the application needs to stream music which is either stored in the device locally or from some other app present on the device in the room where the peer is joined.
Examples of such use cases can be a FM like application where the host would want to stream music while also interacting with others in the room or a host in a gaming app who would want to stream music from their device in the room along with their regular audio track.
How does audio share work
Note: The Audio share option only works in Android 10 and above.
100ms SDK uses the MediaProjection APIs of Android to capture the device audio and stream it along with the user's regular audio track. To achieve this SDK starts a foreground service and starts capturing the device audio and mixes the bytes with the data collected from mic, so that the stream contains both system music and mic data.
This API gives apps the ability to copy the audio being played by other apps which have set its usage to USAGE_MEDIA, USAGE_GAME, or USAGE_UNKNOWN. (Audio from apps like youtube etc can be captured)
How to stream device audio from the app
Add permission for FOREGROUND_SERVICE
& HMSAudioshareActivity
to manifest located at android/app/src/main/AndroidManifest.xml
.
... <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> ... ... <activity android:name="com.reactnativehmssdk.HMSAudioshareActivity" android:label="@string/app_name" /> ...
To start streaming device audio , app needs to call the startAudioshare
async method of HMSSDK
, which takes in one parameter which is one of the modes of type HMSAudioMixingMode
in which the user wants to stream. This can be one out of the three available types - TALK_ONLY : only data captured by mic will be streamed in the room TALK_AND_MUSIC: data captured by mic as well as playback audio being captured from device will be streamed in the room MUSIC_ONLY: only the playback audio being captured from device will be streamed in the room.
Following is the snippet on how to use this:
import { HMSAudioMixingMode } from '@100mslive/react-native-hms'; await hmsInstance?.startAudioshare(HMSAudioMixingMode.MUSIC_ONLY)
How to stop audio sharing
To stop capturing device audio and streaming into the room, call the stopAudioshare
API.
await hmsInstance?.stopAudioshare()
How to change mode
To change the mode while the user is streaming audio, call the setAudioMixingMode
API and pass one of the modes out of TALK_ONLY
or TALK_AND_MUSIC
or MUSIC_ONLY
enum available in HMSAudioMixingMode
class.
Note that TALK_ONLY
mode is equivalent to regular mode, that is without starting this API.
import { HMSAudioMixingMode } from '@100mslive/react-native-hms'; await hmsInstance?.setAudioMixingMode(HMSAudioMixingMode.MUSIC_ONLY)
How to get Audio Share Status
Application needs to call the isAudioShared
method of HMSSDK
. This method returns a boolean which will be true
incase Audioshare is currently active and being used, and false for inactive state.
await hmsInstance?.isAudioShared()