|
| 1 | +// import { PauseIcon, PlayIcon, SpeakerMuteIcon, VolumeDownIcon, VolumeUpIcon } from "@fluentui/react-northstar"; |
| 2 | +import { initializeIcons } from '@uifabric/icons'; |
| 3 | +initializeIcons(); |
| 4 | +import * as React from "react"; |
| 5 | +import styles from './ModernAudio.module.scss'; |
| 6 | +import * as strings from 'ControlStrings'; |
| 7 | +import { IModernAudioProps } from "./IModernAudioProps"; |
| 8 | +import { IconButton } from "office-ui-fabric-react"; |
| 9 | +import { ModernAudioLabelPosition } from './ModernAudioLabelPosition'; |
| 10 | + |
| 11 | +export const ModernAudio: React.FC<IModernAudioProps> = (props: IModernAudioProps) => { |
| 12 | + const audioComp = React.useRef<HTMLAudioElement>(new Audio(props.audioUrl)); |
| 13 | + const [muted, setMuted] = React.useState<boolean>(false); |
| 14 | + const [playing, setPlaying] = React.useState<boolean>(false); |
| 15 | + |
| 16 | + React.useEffect(() => { |
| 17 | + audioComp.current.onended = () => { setPlaying(false); }; |
| 18 | + }, []); |
| 19 | + |
| 20 | + const playAudio = () => { |
| 21 | + setPlaying(true); |
| 22 | + audioComp.current.play(); |
| 23 | + }; |
| 24 | + const pauseAudio = () => { |
| 25 | + setPlaying(false); |
| 26 | + audioComp.current.pause(); |
| 27 | + }; |
| 28 | + const incVolume = () => { |
| 29 | + if (audioComp.current.volume <= 0.9) { |
| 30 | + audioComp.current.volume += 0.1; |
| 31 | + } |
| 32 | + else { |
| 33 | + audioComp.current.volume = 1; |
| 34 | + } |
| 35 | + if (audioComp.current.muted) { |
| 36 | + audioComp.current.muted = false; |
| 37 | + setMuted(false); |
| 38 | + } |
| 39 | + }; |
| 40 | + const decVolume = () => { |
| 41 | + audioComp.current.volume -= 0.1; |
| 42 | + if (audioComp.current.volume < 0.1) { |
| 43 | + audioComp.current.volume = 0; |
| 44 | + audioComp.current.muted = true; |
| 45 | + setMuted(true); |
| 46 | + } |
| 47 | + }; |
| 48 | + const muteAudio = () => { |
| 49 | + audioComp.current.muted = !muted; |
| 50 | + setMuted(!muted); |
| 51 | + }; |
| 52 | + return ( |
| 53 | + <div className={styles.modernAudio}> |
| 54 | + {props.labelPosition === ModernAudioLabelPosition.TopLeft && |
| 55 | + props.label !== "" && |
| 56 | + <div><label>{props.label}</label></div>} |
| 57 | + {props.labelPosition === ModernAudioLabelPosition.TopCenter && |
| 58 | + props.label !== "" && |
| 59 | + <div style={{textAlign: "center"}}><label>{props.label}</label></div>} |
| 60 | + <div className={styles.audioPanel}> |
| 61 | + {props.audioUrl !== "" && <audio ref={audioComp} src={props.audioUrl}></audio>} |
| 62 | + {/* <PlayIcon className={styles.audioIcon} size={props.size} disabled={playing} onClick={playAudio} /> */} |
| 63 | + <IconButton iconProps={{ iconName: 'Play' }} className={styles.audioIcon} title={strings.ModernAudioPlay} disabled={playing} onClick={playAudio} /> |
| 64 | + {/* <PauseIcon className={styles.audioIcon} size={props.size} disabled={!playing} onClick={pauseAudio} /> */} |
| 65 | + <IconButton iconProps={{ iconName: 'Pause' }} className={styles.audioIcon} title={strings.ModernAudioPause} disabled={!playing} onClick={pauseAudio} /> |
| 66 | + {/* <VolumeUpIcon className={styles.audioIcon} size={props.size} title="Increase volume" onClick={incVolume} /> */} |
| 67 | + <IconButton iconProps={{ iconName: 'Volume2' }} className={styles.audioIcon} title={strings.ModernAudioIncVol} onClick={incVolume} /> |
| 68 | + {/* <VolumeDownIcon className={styles.audioIcon} size={props.size} title="Decrease volume" disabled={muted} onClick={decVolume} /> */} |
| 69 | + <IconButton iconProps={{ iconName: 'Volume0' }} className={styles.audioIcon} title={strings.ModernAudioDecVol} disabled={muted} onClick={decVolume} /> |
| 70 | + {/* <SpeakerMuteIcon className={styles.audioIcon} size={props.size} title="Mute" disabled={muted} onClick={muteAudio} /> */} |
| 71 | + <IconButton iconProps={{ iconName: 'VolumeDisabled' }} className={styles.audioIcon} title={strings.ModernAudioMute} disabled={muted} onClick={muteAudio} /> |
| 72 | + </div> |
| 73 | + {props.labelPosition === ModernAudioLabelPosition.BottomLeft && |
| 74 | + props.label !== "" && |
| 75 | + <div><label>{props.label}</label></div>} |
| 76 | + {props.labelPosition === ModernAudioLabelPosition.BottomCenter && |
| 77 | + props.label !== "" && |
| 78 | + <div style={{textAlign: "center"}}><label>{props.label}</label></div>} |
| 79 | + </div> |
| 80 | + ); |
| 81 | +}; |
| 82 | + |
| 83 | +ModernAudio.defaultProps = { |
| 84 | + label: "", |
| 85 | + labelPosition: ModernAudioLabelPosition.TopLeft |
| 86 | +}; |
0 commit comments