我想播放多个音频文件。因此,我为此创建了一个函数。但是它只能播放一个音频..我该怎么做?
我正在尝试使VoiceAudio作为模板,以便我可以播放多个音频。但是目前我只能播放一个音频。如果我像下面的示例一样带两个voiceaudio,其中一个不会玩。
我该如何工作?
loginphone.ts
import {voiceAudio} from '../../src/common/utils/voice';
const LoginPhone = () => {
const domNavigate = useDomNavigate();
const {openKioskAlert} = useContext(KioskAlertContext);
const [phoneNumber, setPhoneNumber] = useState([]);
const {playAudio, stopAudio} = voiceAudio('login-phone');
useEffect(() => {
playAudio();
return () => stopAudio();
}, []);
utils> poice.ts
export const voiceAudio = (title: string) => {
const audioFile = new Audio(`/sounds/${title}.mp3`);
const playAudio = () => audioFile.play();
const stopAudio = () => {
audioFile.pause();
audioFile.currentTime = 0;
};
return {
playAudio,
stopAudio,
};
};
我想做的事情:示例
import {voiceAudio} from '../../src/common/utils/voice';
const LoginPhone = () => {
const domNavigate = useDomNavigate();
const {openKioskAlert} = useContext(KioskAlertContext);
const [phoneNumber, setPhoneNumber] = useState([]);
// if I bring bring?!two voiceAudio, one of them wouldn't work..
const {playAudio, stopAudio} = voiceAudio('login-phone);
const {playAudio, stopAudio} = voiceAudio('login-pin);
useEffect(() => {
playAudio();
return () => stopAudio();
}, []);
const play =
I'm trying to make voiceAudio as a template so I can play multiple audios if I want. But currently I can play only one audio. If I bring two voiceAudio like I did in example below, one of them wouldn't play.
How can I make it work?!
loginPhone.ts
import {voiceAudio} from '../../src/common/utils/voice';
const LoginPhone = () => {
const domNavigate = useDomNavigate();
const {openKioskAlert} = useContext(KioskAlertContext);
const [phoneNumber, setPhoneNumber] = useState([]);
const {playAudio, stopAudio} = voiceAudio('login-phone');
useEffect(() => {
playAudio();
return () => stopAudio();
}, []);
utils>voice.ts
export const voiceAudio = (title: string) => {
const audioFile = new Audio(`/sounds/${title}.mp3`);
const playAudio = () => audioFile.play();
const stopAudio = () => {
audioFile.pause();
audioFile.currentTime = 0;
};
return {
playAudio,
stopAudio,
};
};
what I would like to do : example
import {voiceAudio} from '../../src/common/utils/voice';
const LoginPhone = () => {
const domNavigate = useDomNavigate();
const {openKioskAlert} = useContext(KioskAlertContext);
const [phoneNumber, setPhoneNumber] = useState([]);
// if I bring bring?!two voiceAudio, one of them wouldn't work..
const {playAudio, stopAudio} = voiceAudio('login-phone);
const {playAudio, stopAudio} = voiceAudio('login-pin);
useEffect(() => {
playAudio();
return () => stopAudio();
}, []);
const play =
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能是您正在尝试多次使用相同的变量名称。请注意
Playaudio
和potaudio
正在使用两次。这类似于编写const a = 8; const a = 5
。尝试使用第二个
voiceaudio
使用不同的名称:这将允许您在第二行上引用
playaudio
,为playaudio2
,并参考stopaudio
在第二行上,stopaudio2
。在您的
useeffect
中,您应该能够以这样的方式自由调用两个功能:The problem may be that you are trying to use the same variable names multiple times. Notice how
playAudio
andstopAudio
are being used twice. This is similar to writingconst a = 8; const a = 5
.Try using different names for the second
voiceAudio
like this:That will allow you to refer to the
playAudio
on the second line asplayAudio2
and refer to thestopAudio
on the second line asstopAudio2
.In your
useEffect
you should then be able to freely call both functions like this: