HTML5音频不会在Firefox中第二次播放

jef*_*eyj 6 javascript audio firefox html5

我正在开发一个网络应用程序,我需要播放单词和句子音频文件.我正在使用骨干来从服务器获取和呈现内容.音频文件存在于S3上.当渲染内容时,我播放单词音频文件,然后播放句子音频文件.我还有一个按钮,因此可以重放单词和句子音频.

我的问题是Firefox在初始播放后不会重播音频文件.当playWordplaySentence被调用时,没有任何反应.Safari和Chrome中的代码字很好.

  events: {
   "click a#play-sentence":     "playSentence",
   "click a#play-word":         "playWord"
  },


  // render markup when model has synced with server
  render: function() {
    this.$el.html(this.template({ exam: this.model }));
    this.cacheMedia();

    return this;
  },


  // save audio objects. play word then sentence
  cacheMedia: function() {
    this.audioWord = this.$el.find('#audioWord').get(0);
    this.audioSentence = this.$el.find('#audioSentence').get(0);

    this.audioWord.addEventListener('ended', function(){
      this.currentTime = 0;
    }, false);

    this.audioSentence.addEventListener('ended', function(){
      this.currentTime = 0;
    }, false);

    var view = this;
    var playSentence = function() {
      view.audioSentence.play();
      view.audioWord.removeEventListener('ended', playSentence);
    };

    this.audioWord.addEventListener('ended', playSentence);
    this.audioWord.play();
  },


  // play sentence audio
  playSentence: function(e) {
    e.preventDefault();

    this.audioWord.pause();
    this.audioWord.currentTime = 0;

    this.audioSentence.play();
  },


  // play word audio
  playWord: function(e) {
    e.preventDefault();

    this.audioSentence.pause();
    this.audioSentence.currentTime = 0;

    this.audioWord.play();
  }
Run Code Online (Sandbox Code Playgroud)

Ian*_*lin 0

您需要做的就是.load()在这两种情况下调用,即playWord()应该只需要this.audioWord.load();并且playSentence()应该只需要this.audioSentence.load();。