Skip to content

Commit

Permalink
fix(webapp): player audio
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Feb 10, 2024
1 parent 6af8e73 commit 64a4987
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 19 additions & 10 deletions webapp/components/player/player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function AudioWave(props: { stream: MediaStream }) {
})

const record = wavesurfer.registerPlugin(RecordPlugin.create())
const { onDestroy, onEnd } = record.renderMicStream(props.stream)
const { onDestroy, onEnd } = record.renderMicStream(new MediaStream(props.stream.getAudioTracks()))

return () => {
onDestroy()
Expand All @@ -32,19 +32,28 @@ function AudioWave(props: { stream: MediaStream }) {
export default function Player(props: { stream: MediaStream, muted: boolean, audio: boolean, video: boolean, width: string }) {
const refVideo = useRef<HTMLVideoElement>(null)
const [showAudio, setShowAudio] = useState(false)
const audioTrack = props.stream.getAudioTracks()[0]
const videoTrack = props.stream.getVideoTracks()[0]

useEffect(() => {
if (props.stream?.getAudioTracks().length !== 0 && props.stream?.getVideoTracks().length === 0) {
setShowAudio(true)
} else {
setShowAudio(false)
}
audioTrack && !videoTrack ? setShowAudio(true) : setShowAudio(false)
if (audioTrack && props.audio) {
const el = document.createElement("audio")
el.srcObject = new MediaStream([audioTrack])
el.autoplay
el.play()

}, [props.stream])
return () => {
el.pause()
el.srcObject = null
el.remove()
}
}
}, [audioTrack, videoTrack])

useEffect(() => {
if (refVideo.current) {
refVideo.current.srcObject = props.stream
if (refVideo.current && videoTrack) {
refVideo.current.srcObject = new MediaStream([videoTrack])

// NOTE: About Autoplay
// Reference: https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide
Expand All @@ -54,7 +63,7 @@ export default function Player(props: { stream: MediaStream, muted: boolean, aud
// https://developers.weixin.qq.com/community/develop/doc/0006a61de48ab0165f99e1dcd51c00
if (isWechat()) refVideo.current.play()
}
}, [refVideo, props.stream])
}, [refVideo, videoTrack])

// NOTE: iOS can't display video
// https://webkit.org/blog/6784/new-video-policies-for-ios/
Expand Down
2 changes: 1 addition & 1 deletion webapp/components/player/whep-player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function WhepPlayer(props: { streamId: string, userStatus: Stream

return (
<center className='flex flex-col'>
<Player stream={stream} muted={false} width={props.width} audio={false} video={props.userStatus.video} />
<Player stream={stream} muted={false} width={props.width} audio={true} video={props.userStatus.video} />
<Detail streamId={props.streamId} userStatus={props.userStatus} restart={restart} />
</center>
)
Expand Down

0 comments on commit 64a4987

Please sign in to comment.