当前位置: 首页 > 面试题库 >

如何为结束播放声音做出承诺?

滕鸿畴
2023-03-14
问题内容

我有一个简单的JavaScript可以加载声音:

  prefix = 'modules/sounds/';
 _sounds = ['nameOfSound','nameOfSound','nameOfSound'];


 for (var sound in _sounds) {
                if (_sounds.hasOwnProperty(sound)) {
                    var cached = _sounds[sound];
                    cached.audio = new Audio(prefix + cached.src);
                    }
                }
            }

我有一项执行以下功能的服务:

 this.play = function(trackName) {
        _sounds[trackName].audio.play();
    };

我想要的是对声音的简单承诺,所以当我调用它时,我会遇到类似以下情况:

soundService.play('boom').then(function(){
    do something here
});

那有可能吗?


问题答案:

您可以实施设置:

function play(url) {
     return new Promise(function(resolve, reject) {   // return a promise
         var audio = new Audio();                     // create audio wo/ src
         audio.preload = "auto";                      // intend to play through
         audio.autoplay = true;                       // autoplay when loaded
         audio.onerror = reject;                      // on error, reject
         audio.onended = resolve;                     // when done, resolve

         audio.src = path + url + suffix; // just for example
     });
}

现在您可以致电:

play("boom").then(function() {

});

由于音频元素的缓冲行为,因此没有意义。指示预加载将告诉浏览器您要播放音频文件。在我看来,这对于客户端和系统也更好。

function play(url) {
  return new Promise(function(resolve, reject) { // return a promise
    var audio = new Audio();                     // create audio wo/ src
    audio.preload = "auto";                      // intend to play through
    audio.autoplay = true;                       // autoplay when loaded
    audio.onerror = reject;                      // on error, reject
    audio.onended = resolve;                     // when done, resolve

    audio.src = url
  });
}

play(sample).then(function() {
  alert("Done!");
})


<script>
var sample = "data:audio/wav;base64,UklGRiAOAABXQVZFZm10IBAAAAABAAEA71YAAO9WAAABAAgAZGF0YfsNAACCgoKAgYKCgoKCgoGCgoGBgIB/foCAf35+fn19fX1+foCCgYGAgICAfn5+fnx8fn5/f3+AgICBgYKCg4OCgIB/fn5+gIB/gIGBgoGAf4CAgYB+fn5+fXx8fHx9fn59fX1+foB/f39+gIGAgIGBgIB+f4CAgICAgH+AgoKBgIGCgoKChYWBfHp5eHh5enx/goSGiYqMi4mHhoaEhIOCgH58fH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Afn19fn19fHx9f4B+f39+fn6AgH59fXx9gIB+fn9/gICBgoKCgoGBgYKCgoKBgICAgYGBgICAgICBgYCBgoKBgICAgoKCgoGBgYKCgYGCg4KBgICAgYCBgoGAgIGCgoGAgICBg4KAgICBgoKCgH5+foCAgH5+foCCgYCAgH9/gYGAf358fH6Afn18foAA"
</script>


 类似资料:
  • 播放(播放音效/播放录音)

  • 问题内容: 我希望能够在程序中播放声音文件。我应该去哪里看? 问题答案: 我写了下面的代码,效果很好。但我认为它仅适用于格式。

  • 我刚刚开始使用JavaFX。我知道它的基本原理。我尝试使用media和mediaplayer类播放一个声音,叫做“sound.mp3”。我正在eclipse中编程,我在src文件夹中有声音文件,与“(默认包)”相同的文件夹。下面是我的代码: 请告诉我我做错了什么。 下面是来自控制台的错误消息:

  • 我希望能够在我的程序中播放声音文件。我应该去哪里?

  • 问题内容: 我想用Swift播放声音。 我的代码在Swift 1.0中可用,但现在在Swift 2或更高版本中不再起作用。 问题答案: 最可取的是,您可能想使用 AVFoundation。它提供了使用视听媒体的所有必要条件。 更新: 某些评论中建议与 Swift 2 , Swift 3 和 Swift 4 兼容。 斯威夫特2.3 迅捷3 Swift 4(与iOS 13兼容) 确保更改乐曲的名称以及

  • 问题内容: Octave似乎假设某个特定的声音播放实用程序将在系统上可用,但似乎没有提供指定备用声音的功能。在以下错误中,Octave正在寻找,这不是在所有系统上都可用的实用程序。 八度:38>声音(beamformed_20) sh:ofsndplay:找不到命令 是否可以使用Octave配置设置或代码片段来指定适合系统的实用程序? 问题答案: 在我的一台Linux机器上,我创建了以下ofsnd