Skip to content

Commit a34792d

Browse files
authored
Merge pull request nexmo-se#98 from nexmo-se/adding-audio-output
Adding audio output
2 parents ea1ee2f + 4c989df commit a34792d

File tree

9 files changed

+290
-83
lines changed

9 files changed

+290
-83
lines changed

package-lock.json

Lines changed: 36 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"@testing-library/react": "^11.1.0",
1414
"@testing-library/user-event": "^12.1.10",
1515
"@vonage/video-effects": "^0.1.0",
16-
"@vonage/video-express": "^1.1.4",
16+
"@vonage/video-express": "^1.2.0",
1717
"axios": "^0.21.1",
1818
"body-parser": "^1.19.0",
1919
"concurrently": "^6.2.0",

src/App.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
BrowserRouter as Router,
55
Switch,
66
Route,
7-
Redirect
7+
Redirect,
88
} from 'react-router-dom';
99
import { UserContext } from './context/UserContext';
1010
import VideoRoom from './components/VideoRoom';
@@ -27,40 +27,41 @@ const theme = () => {
2727
palette: {
2828
type: 'light',
2929
primary: {
30-
main: primary
30+
main: primary,
3131
},
3232
secondary: {
33-
main: secondary
33+
main: secondary,
3434
},
3535
bodyBackground: {
36-
black: '#131415'
36+
black: '#131415',
3737
},
3838
callBackground: {
39-
main: '#20262D'
39+
main: '#20262D',
4040
},
4141
toolbarBackground: {
42-
main: '#41464D'
42+
main: '#41464D',
4343
},
4444
activeButtons: {
4545
green: '#1C8731',
46-
red: '#D50F2C'
47-
}
48-
}
46+
red: '#D50F2C',
47+
},
48+
},
4949
});
5050
};
5151

5252
function App() {
5353
const [user, setUser] = useState({
5454
videoEffects: {
5555
backgroundBlur: false,
56-
virtualBackground: false
56+
virtualBackground: false,
5757
},
5858
defaultSettings: {
5959
publishAudio: true,
6060
publishVideo: true,
6161
audioSource: undefined,
62-
videoSource: undefined
63-
}
62+
videoSource: undefined,
63+
audioOutput: undefined,
64+
},
6465
});
6566
const userValue = useMemo(() => ({ user, setUser }), [user, setUser]);
6667
return (
@@ -78,7 +79,7 @@ function App() {
7879
<Route path="*">
7980
<Redirect
8081
to={{
81-
pathname: '/'
82+
pathname: '/',
8283
}}
8384
/>
8485
</Route>

src/components/MuteAudioButton/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function MuteAudioButton({
2222
classes,
2323
getAudioSource,
2424
cameraPublishing,
25-
changeAudioSource
25+
changeAudioSource,
2626
}) {
2727
const title = hasAudio ? 'Disable Microphone' : 'Enable Microphone';
2828
const localClasses = styles();
@@ -52,7 +52,7 @@ export default function MuteAudioButton({
5252
getAudioSource,
5353
deviceInfo,
5454
audioDeviceId,
55-
devicesAvailable
55+
devicesAvailable,
5656
]);
5757

5858
React.useEffect(() => {
@@ -66,7 +66,6 @@ export default function MuteAudioButton({
6666

6767
const handleChangeAudioSource = (event, index) => {
6868
setSelectedIndex(index);
69-
console.log(index);
7069
setOpen(false);
7170
const audioDeviceId = devicesAvailable.find(
7271
(device) => device.label === event.target.textContent
@@ -132,14 +131,13 @@ export default function MuteAudioButton({
132131
role={undefined}
133132
transition
134133
disablePortal
135-
style={{ zIndex: 101 }} // todo temporary fix for a bug in MP 0.1.5
136134
>
137135
{({ TransitionProps, placement }) => (
138136
<Grow
139137
{...TransitionProps}
140138
style={{
141139
transformOrigin:
142-
placement === 'bottom' ? 'center top' : 'center bottom'
140+
placement === 'bottom' ? 'center top' : 'center bottom',
143141
}}
144142
>
145143
<Paper>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import SpeakerIcon from '@material-ui/icons/Speaker';
2+
import { IconButton } from '@material-ui/core';
3+
import Tooltip from '@material-ui/core/Tooltip';
4+
5+
import useDevices from '../../hooks/useDevices';
6+
7+
import Menu from '@material-ui/core/Menu';
8+
import MenuItem from '@material-ui/core/MenuItem';
9+
10+
import React from 'react';
11+
import styles from './styles.js';
12+
13+
export default function SpeakerSelector({
14+
classes,
15+
cameraPublishing,
16+
changeAudioOutput,
17+
getCurrentAudioOutput,
18+
}) {
19+
const [anchorEl, setAnchorEl] = React.useState(null);
20+
const localClasses = styles();
21+
const ITEM_HEIGHT = 48;
22+
23+
const { deviceInfo } = useDevices();
24+
const [devicesAvailable, setDevicesAvailable] = React.useState(null);
25+
const [options, setOptions] = React.useState([]);
26+
27+
const open = Boolean(anchorEl);
28+
const [selectedIndex, setSelectedIndex] = React.useState(0);
29+
const [audioOutputId, setAudioOutputId] = React.useState('');
30+
31+
React.useEffect(() => {
32+
setDevicesAvailable(deviceInfo.audioOutputDevices);
33+
34+
if (cameraPublishing && devicesAvailable) {
35+
getCurrentAudioOutput().then((id) => setAudioOutputId(id));
36+
37+
const indexOfSelectedElement = devicesAvailable.indexOf(
38+
devicesAvailable.find((e) => e.deviceId === audioOutputId)
39+
);
40+
41+
setSelectedIndex(indexOfSelectedElement);
42+
}
43+
}, [
44+
cameraPublishing,
45+
deviceInfo,
46+
audioOutputId,
47+
devicesAvailable,
48+
getCurrentAudioOutput,
49+
]);
50+
51+
React.useEffect(() => {
52+
if (devicesAvailable) {
53+
const audioOutputsAvailable = devicesAvailable.map((e) => {
54+
return e.label;
55+
});
56+
setOptions(audioOutputsAvailable);
57+
}
58+
}, [devicesAvailable]);
59+
60+
const handleChangeAudioOutput = (event, index) => {
61+
setSelectedIndex(index);
62+
const audioOutputId = devicesAvailable.find(
63+
(device) => device.label === event.target.textContent
64+
).deviceId;
65+
changeAudioOutput(audioOutputId);
66+
};
67+
68+
const handleClose = () => {
69+
setAnchorEl(null);
70+
};
71+
72+
const handleClick = (event) => {
73+
setAnchorEl(event.currentTarget);
74+
};
75+
76+
return (
77+
<div>
78+
<Tooltip title="Change Audio Output">
79+
<IconButton
80+
aria-label="more"
81+
aria-controls="long-menu"
82+
aria-haspopup="true"
83+
className={classes.toolbarButtons}
84+
onClick={handleClick}
85+
>
86+
<SpeakerIcon />
87+
</IconButton>
88+
</Tooltip>
89+
<Menu
90+
id="long-menu"
91+
anchorEl={anchorEl}
92+
keepMounted
93+
open={open}
94+
onClose={handleClose}
95+
PaperProps={{
96+
style: {
97+
maxHeight: ITEM_HEIGHT * 4.5,
98+
width: '40ch',
99+
},
100+
}}
101+
>
102+
{options.map((option, index) => (
103+
<MenuItem
104+
key={option}
105+
selected={index === selectedIndex}
106+
onClick={(event) => handleChangeAudioOutput(event, index)}
107+
classes={{ selected: localClasses.selected }}
108+
>
109+
{option}
110+
</MenuItem>
111+
))}
112+
</Menu>
113+
</div>
114+
);
115+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { makeStyles } from '@material-ui/core/styles';
2+
// export default makeStyles((theme)=>{
3+
export default makeStyles({
4+
selected: {
5+
color: '#b779ff'
6+
}
7+
});

0 commit comments

Comments
 (0)