Skip to content

Commit 6d0082f

Browse files
authored
fix: fix useIOSAudioManagement calculation for track counts (#128)
* fix: fix useIOSAudioManagement calculation for track counts * docs: add useIOSAudioManagement to readme
1 parent e3a985d commit 6d0082f

3 files changed

Lines changed: 30 additions & 15 deletions

File tree

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,13 @@ Additional documentation for the LiveKit SDK can be found at https://docs.liveki
159159
160160
## Audio sessions
161161
162-
As seen in the above example, we've introduced a new class `AudioSession` that helps
162+
As seen in the above example, we've introduced a class `AudioSession` that helps
163163
to manage the audio session on native platforms. This class wraps either [AudioManager](https://developer.android.com/reference/android/media/AudioManager) on Android, or [AVAudioSession](https://developer.apple.com/documentation/avfaudio/avaudiosession) on iOS.
164164
165165
You can customize the configuration of the audio session with `configureAudio`.
166166
167-
### Media playback
167+
### Android
168+
#### Media playback
168169
169170
By default, the audio session is set up for bidirectional communication. In this mode, the audio framework exhibits the following behaviors:
170171
@@ -174,8 +175,6 @@ By default, the audio session is set up for bidirectional communication. In this
174175
175176
If you're leveraging LiveKit primarily for media playback, you have the option to reconfigure the audio session to better suit media playback. Here's how:
176177
177-
Note: iOS audio session customization is in development, and will be documented here when released.
178-
179178
```js
180179
useEffect(() => {
181180
let connect = async () => {
@@ -197,7 +196,7 @@ useEffect(() => {
197196
}, [url, token, room]);
198197
```
199198
200-
### Customizing audio session
199+
#### Customizing audio session
201200
202201
Instead of using our presets, you can further customize the audio session to suit your specific needs.
203202
@@ -220,6 +219,12 @@ await AudioSession.configureAudio({
220219
await AudioSession.startAudioSession();
221220
```
222221
222+
### iOS
223+
224+
For iOS, the most appropriate audio configuration may change over time when local/remote
225+
audio tracks publish and unpublish from the room. To adapt to this, the [`useIOSAudioManagement`](https://htmlpreview.github.io/?https://raw.githubusercontent.com/livekit/client-sdk-react-native/main/docs/functions/useIOSAudioManagement.html)
226+
hook is advised over just configuring the audio session once for the entire audio session.
227+
223228
## Screenshare
224229
225230
Enabling screenshare requires extra installation steps:

src/audio/AudioManager.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState, useEffect, useMemo } from 'react';
22
import { Platform } from 'react-native';
3-
import { RoomEvent, type Room } from 'livekit-client';
3+
import { RoomEvent, Room } from 'livekit-client';
44
import AudioSession, {
55
getDefaultAppleAudioConfigurationForMode,
66
type AppleAudioConfiguration,
@@ -31,14 +31,25 @@ export function useIOSAudioManagement(
3131
[localTrackCount, remoteTrackCount]
3232
);
3333

34+
useEffect(() => {
35+
let recalculateTrackCounts = () => {
36+
setLocalTrackCount(getLocalAudioTrackCount(room));
37+
setRemoteTrackCount(getRemoteAudioTrackCount(room));
38+
};
39+
40+
recalculateTrackCounts();
41+
42+
room.on(RoomEvent.Connected, recalculateTrackCounts);
43+
44+
return () => {
45+
room.off(RoomEvent.Connected, recalculateTrackCounts);
46+
};
47+
}, [room]);
3448
useEffect(() => {
3549
if (Platform.OS !== 'ios') {
3650
return () => {};
3751
}
3852

39-
setLocalTrackCount(getLocalAudioTrackCount(room));
40-
setRemoteTrackCount(getRemoteAudioTrackCount(room));
41-
4253
let onLocalPublished = () => {
4354
setLocalTrackCount(localTrackCount + 1);
4455
};
@@ -85,7 +96,6 @@ export function useIOSAudioManagement(
8596
let configFunc =
8697
onConfigureNativeAudio ?? getDefaultAppleAudioConfigurationForMode;
8798
let audioConfig = configFunc(trackState, preferSpeakerOutput);
88-
8999
AudioSession.setAppleAudioConfiguration(audioConfig);
90100
}, [trackState, onConfigureNativeAudio, preferSpeakerOutput]);
91101
}
@@ -106,13 +116,13 @@ function computeAudioTrackState(
106116
}
107117

108118
function getLocalAudioTrackCount(room: Room): number {
109-
return room.localParticipant.audioTracks.entries.length;
119+
return room.localParticipant.audioTracks.size;
110120
}
111121

112122
function getRemoteAudioTrackCount(room: Room): number {
113123
var audioTracks = 0;
114124
room.participants.forEach((participant) => {
115-
audioTracks += participant.audioTracks.entries.length;
125+
audioTracks += participant.audioTracks.size;
116126
});
117127

118128
return audioTracks;

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7507,9 +7507,9 @@ lines-and-columns@^1.1.6:
75077507
integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==
75087508

75097509
livekit-client@^1.15.0:
7510-
version "1.15.0"
7511-
resolved "https://registry.yarnpkg.com/livekit-client/-/livekit-client-1.15.0.tgz#ab3ea8687eaf920ecace72537b6307b676f5819f"
7512-
integrity sha512-uyp1sIFeOdezYBn9J0FnrMuG8uPgR4fwWPENodNisJC999xJwTSV3xVLX76AAnfky3IJoJehU4gY2UqoyXjm2w==
7510+
version "1.15.10"
7511+
resolved "https://registry.yarnpkg.com/livekit-client/-/livekit-client-1.15.10.tgz#8772927a55ee9ee57616b90d1d8bcbc9152e9ce7"
7512+
integrity sha512-fNiXakeJyoaInt8Yo2i7kzsEG5YtlvqVqgXX1OPv7U7Y6D5TVwJc7dR1sIPpMxsAGMEIwbvsIweRXlObpp2mxA==
75137513
dependencies:
75147514
"@bufbuild/protobuf" "^1.3.0"
75157515
events "^3.3.0"

0 commit comments

Comments
 (0)