2016-09-30 11 views
2

私はreact-jsでサウンドを再生しようとしましたが、私はそれを始めることはできません。 私は、私のreactClassに初期化私のサウンドVARを持って前に初期状態を取得:react-jsでサウンドを再生

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ファイルで試しました。しかしそれは始まりません。私は間違って何をしていますか?

!!!! EDIT:

<audio id="beep" loop> 
     <source src="files/ring.wav" type="audio/wav" /> 
     </audio> 

そして、次のスタート/ストップと:: はまた、HTML項目を追加しようとした

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(). 

!!!! EDIT2:

また、私はボタンを設定すると、音を再生することに気づいた。私がファイルマネージャを開くと、私のindex.htmlに行き、それを開いて、それは完全に動作します。 webpack-server:localhost:8000/app/index.htmlからページを試しても動作しません。同じDOMExceptionを取得しています。どうしてこれなの?

+1

playSound()をどこから呼び出しているのかを聞くことができますか? – BlinkingCahill

+0

もう少し複雑です。しかし、私は、主なindex.htmlであるreact-app.jsクラスと、タブと、ページをレンダリングするビューを持っています。 コールバック関数を持つC++ライブラリを初期化します。ネイティブのものを変更するとコールバックが返され、そこからサウンドを再生していました。私は問題を解決しました、それはかなり簡単でした、私はそれについて考えて、私は今応答を追加します –

答えて

2

しばらくお待ちください。私は、index.htmlファイルに2つのボタンを作成しました.1つはサウンドを開始し、もう1つは停止します。だから私はそれを試みました、そして、私はファイルマネージャから動作することに気付きましたが、Webpackサーバから試してみるとそうではありません。 私は自分のパスをチェックしましたが、代わりに: "files/audio.mp3" "../files/audio.mp3"に変更しました。新人ミスですが、Androidデベロッパーを入れてjavascriptで作業するとどうなりますか)))

関連する問題