7

I'm having a bit of trouble rewinding audio in Javascript. I basically have a countdown that beeps each second as it gets towards the end of the countdown.

I tried using;

var bip = new Audio("http://www.soundjay.com/button/beep-7.wav");
bip.play();

but it didn't beep every second which I'm guessing has something to do withit having to load a new sound every second. I then tried loading the sound externally and triggering it with the following code.

bip.pause();
bip.currentTime = 0;
console.log(bip.currentTime);
bip.play();

but this only plays the sound once then completely fails to rewind it (this is shown by the console logging a time of 0.19 seconds after the first playthrough).

Is there something I'm missing here?

4

4 に答える 4

12

Google Chrome では、同じドメインにオーディオ ファイルがある場合にのみ機能することに気付きました。オーディオ ファイルが同じドメインにある場合は問題ありません。イベント設定.currentTimeが機能します。

クロスドメイン:

var bip = new Audio("http://www.soundjay.com/button/beep-7.wav");
bip.play();
bip.currentTime; //0.10950099676847458
bip.currentTime = 0;
bip.currentTime; //0.10950099676847458

同一ドメイン:

var bip = new Audio("beep-7.wav");
bip.play();
bip.currentTime; //0.10950099676847458
bip.currentTime = 0;
bip.currentTime; //0

しばらくグーグルを試してみましたが、これに関する仕様や議論さえ何も見つかりませんでした。

于 2012-06-12T20:49:16.517 に答える
3

巻き戻したいときは、オーディオを再度ロードするだけです。

my_audio.load()

*ところで、「canplay」のイベントリスナーも使用して、my_audio.play() をトリガーします。これはAndroidで必要なようで、おそらく他のデバイスでも必要です*

于 2013-06-09T20:56:43.933 に答える
0

「シバン全体」の少しの数字によるペイントでdsdsdsdsdの答えをさらに進めるには

注: 私のアプリでloc1は、曲の保存場所を参照するダミーです

// ... code before ...

tune = new Audio(loc1); // First, create audio event using js

// set up "song's over' event listener & action
tune.addEventListener('ended', function(){
tune.load();    //reload audio event (and reset currentTime!)
tune.play();    //play audio event for subsequent 'ended' events
},false);

tune.play();        // plays it the first time thru

// ... code after ...

何日も何日もかけて何が間違っていたのかを理解しようとしましたが、今ではすべて正常に動作します...少なくともデスクトップブラウザでは...

于 2014-02-28T21:08:25.763 に答える