11import * as React from 'react' ;
22import { 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
78function 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}
0 commit comments