在 react-js 中播放声音
Play sound in react-js
我尝试在 react-js 上播放声音,但无法启动。
在获取 InitialState 之前,我在我的 reactClass 中初始化了我的声音变量:
var App = React.createClass({
audio: new Audio('files/audio.mp3'),
getInitialState: function () {
return {
playSound: false,
........
}
}
这是我用于启动和停止的函数:
playSound: function() {
if(!this.state.playSound){
this.setState({
playSound: true,
}, function(){
console.log("play sound 1");
this.audio.play();
console.log("play sound 2");
});
}
},
stopSound: function() {
if(this.state.playSound){
this.setState({
playSound: false,
}, function(){
console.log("stop sound 1");
this.audio.pause();
console.log("stop sound 2");
});
}
},
但我得到了这个答案:
react-app.js:346 Uncaught (in promise) DOMException: The element has no supported sources.
我已经尝试过使用 .mp3 和 .wav 文件。但它不会启动。我做错了什么?
!!!!编辑:
还尝试添加 HTML 项:
<audio id="beep" loop>
<source src="files/ring.wav" type="audio/wav" />
</audio>
以及以下 start/stop:
playSound: function() {
if(!this.state.playSound){
this.setState({
playSound: true,
}, function(){
console.log("play sound 1");
var audioElement = document.getElementById('beep');
audioElement.setAttribute("preload", "auto");
audioElement.autobuffer = true;
audioElement.load();
audioElement.play();
console.log("play sound 2");
});
}
},
stopSound: function() {
if(this.state.playSound){
this.setState({
playSound: false,
}, function(){
console.log("stop sound 1");
var audioElement = document.getElementById('beep');
audioElement.pause();
console.log("stop sound 2");
});
}
},
然后我在启动时没有错误,但它没有启动。当我按下暂停时,我得到:
Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().
!!!!编辑 2:
我还注意到,如果我设置按钮,播放声音。如果我打开文件管理器,转到我的 index.html 并打开它,它运行良好。如果我尝试来自 webpack-server 的页面:localhost:8000/app/index.html,它不会工作。获得相同的 DOMException。这是为什么?
尝试了一段时间后,我在 index.html 文件中制作了 2 个按钮,1 个用于启动声音,1 个用于停止声音。所以我试了一下,我注意到它可以在我的文件管理器中运行,但如果我在 webpack 服务器上尝试则不行。
所以我检查了我的路径,而不是:
"files/audio.mp3" 我把它改成了“../files/audio.mp3”。菜鸟犯的错误,但当你让 Android 开发人员在 javascript 工作时就会发生这种情况 :)))
我尝试在 react-js 上播放声音,但无法启动。 在获取 InitialState 之前,我在我的 reactClass 中初始化了我的声音变量:
var App = React.createClass({
audio: new Audio('files/audio.mp3'),
getInitialState: function () {
return {
playSound: false,
........
}
}
这是我用于启动和停止的函数:
playSound: function() {
if(!this.state.playSound){
this.setState({
playSound: true,
}, function(){
console.log("play sound 1");
this.audio.play();
console.log("play sound 2");
});
}
},
stopSound: function() {
if(this.state.playSound){
this.setState({
playSound: false,
}, function(){
console.log("stop sound 1");
this.audio.pause();
console.log("stop sound 2");
});
}
},
但我得到了这个答案:
react-app.js:346 Uncaught (in promise) DOMException: The element has no supported sources.
我已经尝试过使用 .mp3 和 .wav 文件。但它不会启动。我做错了什么?
!!!!编辑: 还尝试添加 HTML 项:
<audio id="beep" loop>
<source src="files/ring.wav" type="audio/wav" />
</audio>
以及以下 start/stop:
playSound: function() {
if(!this.state.playSound){
this.setState({
playSound: true,
}, function(){
console.log("play sound 1");
var audioElement = document.getElementById('beep');
audioElement.setAttribute("preload", "auto");
audioElement.autobuffer = true;
audioElement.load();
audioElement.play();
console.log("play sound 2");
});
}
},
stopSound: function() {
if(this.state.playSound){
this.setState({
playSound: false,
}, function(){
console.log("stop sound 1");
var audioElement = document.getElementById('beep');
audioElement.pause();
console.log("stop sound 2");
});
}
},
然后我在启动时没有错误,但它没有启动。当我按下暂停时,我得到:
Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().
!!!!编辑 2:
我还注意到,如果我设置按钮,播放声音。如果我打开文件管理器,转到我的 index.html 并打开它,它运行良好。如果我尝试来自 webpack-server 的页面:localhost:8000/app/index.html,它不会工作。获得相同的 DOMException。这是为什么?
尝试了一段时间后,我在 index.html 文件中制作了 2 个按钮,1 个用于启动声音,1 个用于停止声音。所以我试了一下,我注意到它可以在我的文件管理器中运行,但如果我在 webpack 服务器上尝试则不行。 所以我检查了我的路径,而不是: "files/audio.mp3" 我把它改成了“../files/audio.mp3”。菜鸟犯的错误,但当你让 Android 开发人员在 javascript 工作时就会发生这种情况 :)))