【问题标题】:Audio Player in HTML 5 not working properly in chrome.(Forward & Rewind play)HTML 5 中的音频播放器在 chrome 中无法正常工作。(前进和后退播放)
【发布时间】:2014-04-22 14:46:00
【问题描述】:

我在 chrome 中运行 HTML5 音频播放器时遇到问题。它在 IE 9+ 和 Firefox 中运行良好。我已经编写了用于在 F7 和 F8 按键上转发和倒带音频播放器的 javascript 函数,它在 IE 和 FF 中运行良好,但由于某些原因它在 Chrome 中不起作用。以下是代码。

$(document).keydown(function (e) {

    if (e.keyCode == 118) { rewindAudio(); return false; }
    else if (e.keyCode == 119) { forwardAudio(); return false; }
} 

  // Rewinds the audio file by 30 seconds.        
    function rewindAudio() {            

        // Check for audio element support.
        if (window.HTMLAudioElement)
            {
               try {
                      var oAudio = audioPlayerInFocus[0]; 
                      oAudio.currentTime -= 1.0;
                }
               catch (e) {
                   // Fail silently but show in F12 developer tools console
                   if (window.console && console.error("Error:" + e));
                }
            }
      }

// Fast forwards the audio file by 1 seconds.
function forwardAudio() {

    // Check for audio element support.
    if (window.HTMLAudioElement) {
        try {
            var oAudio = audioPlayerInFocus[0
            oAudio.currentTime += 1.0;
        }
        catch (e) {
            // Fail silently but show in F12 developer tools console
            if (window.console && console.error("Error:" + e));
        }
    }
}

我怀疑当前时间在 chrome 中没有改变。是否有任何语法错误或浏览器相关问题?请帮助我。谢谢。

【问题讨论】:

  • var oAudio = audioPlayerInFocus[0 是一个语法难题。另外,请告诉我们audioPlayerInFocus是什么@

标签: javascript jquery asp.net-mvc google-chrome html5-audio


【解决方案1】:

以下代码适用于我为我测试过的所有浏览器 - fiddle

<audio controls>
    <source src="sample/one.mp3" type="audio/mpeg">
</audio>

<script type="text/javascript">
$(document).on('keydown',function(e) {
if (e.keyCode == 82) { 
    console.log('r key = rewind');
    rewindAudio();
}
else if(e.keyCode == 70){
    console.log('f key = forward');
    forwardAudio();
}
});

function rewindAudio() {            
    console.log('rewindAudio');
    oAudio = $('audio')[0];
    oAudio.currentTime -= 1.0;   
}
function forwardAudio() {            
    console.log('forwardAudio');
    oAudio = $('audio')[0];
    oAudio.currentTime += 1.0;   
}
</script>

我读到有人使用F7 key 进行特定设计。

在检测 HTML5 音频支持方面,我通常使用 here 中描述的函数。

【讨论】:

  • 感谢您的帮助,但仍然无法正常工作...我在 Chrome 中调试了代码...当前时间正在更改,但它没有反映在页面的音频播放器中...。
  • 我认为您应该为您的问题提供一个实际的小提琴,以便我们可以尝试重现您遇到的问题。
猜你喜欢
  • 2017-12-28
  • 1970-01-01
  • 2021-09-23
  • 2021-08-02
  • 2018-03-25
  • 2011-12-06
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
相关资源
最近更新 更多