Skip to content

Commit f99e0de

Browse files
committed
[decrease-volume-on-speak] - upgrades as suggested by users
1 parent 0da12e0 commit f99e0de

3 files changed

Lines changed: 63 additions & 13 deletions

File tree

src/decrease-volume-on-talk/component.tsx

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as React from 'react';
22
import { useEffect } from 'react';
33

4-
import { BbbPluginSdk, PluginApi } from 'bigbluebutton-html-plugin-sdk';
5-
import { DecreaseVolumeOnSpeakProps } from './types';
4+
import { BbbPluginSdk, ExternalVideoVolumeEventsNames, PluginApi } from 'bigbluebutton-html-plugin-sdk';
5+
import { DecreaseVolumeOnSpeakProps, ExternalVideoMeetingSubscription } from './types';
6+
import MEETING_SUBSCRIPTION from './subscription';
67

78
function DecreaseVolumeOnSpeak(
89
{ pluginUuid: uuid }: DecreaseVolumeOnSpeakProps,
@@ -11,34 +12,62 @@ function DecreaseVolumeOnSpeak(
1112

1213
const pluginApi: PluginApi = BbbPluginSdk.getPluginApi(uuid);
1314
const intervalId = React.useRef<NodeJS.Timeout>(null);
15+
const volumeSetByUser = React.useRef(1);
16+
const volumeSetByMachine = React.useRef(1);
17+
const externalVideoData = pluginApi.useCustomSubscription<ExternalVideoMeetingSubscription>(
18+
MEETING_SUBSCRIPTION,
19+
);
20+
21+
const setVolume = (volume: number) => {
22+
pluginApi.uiCommands.externalVideo.volume.set({
23+
volume,
24+
});
25+
volumeSetByMachine.current = volume;
26+
};
27+
28+
const [isSomeoneTalking, setIsSomeoneTalking] = React.useState(false);
1429

1530
const talkingIndicatorResult = pluginApi.useTalkingIndicator();
1631

1732
const talkingIndicatorList = talkingIndicatorResult.data;
1833

34+
pluginApi.useUiEvent(
35+
ExternalVideoVolumeEventsNames.VOLUME_VALUE_CHANGED,
36+
(value) => {
37+
if (value.detail.value * 100 !== volumeSetByMachine.current * 100) {
38+
volumeSetByUser.current = value.detail.value;
39+
}
40+
},
41+
);
42+
43+
useEffect(() => {
44+
if (talkingIndicatorList?.some((userVoice) => userVoice.talking) !== isSomeoneTalking) {
45+
setIsSomeoneTalking(talkingIndicatorList?.some((userVoice) => userVoice.talking));
46+
}
47+
}, [talkingIndicatorResult]);
48+
1949
useEffect(() => {
20-
if (talkingIndicatorList?.some((userVoice) => userVoice.talking) && !intervalId.current) {
50+
if (isSomeoneTalking && !intervalId.current
51+
&& externalVideoData.data?.meeting[0]?.externalVideo?.playerPlaying) {
2152
let counter = 1;
2253
const total = 10;
2354
const finalVolume = 0.2;
24-
const percentage = (1 - 1 * finalVolume) / total;
55+
const partToSubtract = (
56+
volumeSetByUser.current - volumeSetByUser.current * finalVolume) / total;
2557
intervalId.current = setInterval(() => {
26-
pluginApi.uiCommands.externalVideo.volume.set({
27-
volume: 1 - counter * percentage,
28-
});
58+
const volumeToSet = Math.floor((volumeSetByUser.current - counter * partToSubtract) * 100);
59+
setVolume(volumeToSet / 100);
2960
if (counter === total) {
3061
clearInterval(intervalId.current);
3162
intervalId.current = null;
3263
}
3364
counter += 1;
3465
}, 100);
3566
} else if (
36-
!talkingIndicatorList?.some((userVoice) => userVoice.talking) && !intervalId.current) {
37-
pluginApi.uiCommands.externalVideo.volume.set({
38-
volume: 1,
39-
});
67+
!isSomeoneTalking && !intervalId.current) {
68+
setVolume(volumeSetByUser.current);
4069
}
41-
}, [talkingIndicatorResult]);
70+
}, [isSomeoneTalking]);
4271

4372
return null;
4473
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const MEETING_SUBSCRIPTION = `
2+
subscription MeetingSubscription {
3+
meeting {
4+
externalVideo {
5+
playerPlaying
6+
externalVideoUrl
7+
}
8+
}
9+
}
10+
`;
11+
12+
export default MEETING_SUBSCRIPTION;

src/decrease-volume-on-talk/types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,13 @@ interface DecreaseVolumeOnSpeakProps {
33
pluginUuid: string,
44
}
55

6-
export { DecreaseVolumeOnSpeakProps };
6+
interface ExternalVideoMeetingSubscription {
7+
meeting: {
8+
externalVideo: {
9+
playerPlaying: boolean
10+
externalVideoUrl: string
11+
}
12+
}[]
13+
}
14+
15+
export { DecreaseVolumeOnSpeakProps, ExternalVideoMeetingSubscription };

0 commit comments

Comments
 (0)