HTML 5中的音频播放器在chrome中无法正常工作.(向前和向后播放)

Audio Player in HTML 5 not working properly in chrome.(Forward & Rewind play)

本文关键字:工作 播放 常工作 音频 播放器 chrome HTML      更新时间:2023-09-26

我在chrome中运行HTML5音频播放器时遇到问题。它在IE 9+和Firefox中运行良好。我已经在F7&按F8键,它在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));
        }
    }
}

我怀疑currentTime没有改变。是否存在语法错误或浏览器相关问题?请帮帮我。谢谢。

以下代码适用于我测试过的所有浏览器-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键进行特定设计。

顺便说一句,为了检测HTML5音频支持,我通常使用这里描述的函数。