Javascript 设置一个<;音频>;标签不工作?

Javascript 设置一个<;音频>;标签不工作?,javascript,html,html5-audio,Javascript,Html,Html5 Audio,我在后台播放这个音频标签,并且我能够在几秒钟内将进度存储到cookie中。 但我无法从那块饼干开始播放音频。(在其他页面上继续) 对于.get[0],它也不起作用 有人能帮我把事情弄清楚吗?非常感谢 在设置当前时间之前,您需要等待音频源加载 $(function(){ $('audio').bind('canplay', function(){ $(this)[0].currentTime = $.cookie('audioTime'); }); }); 首先,

我在后台播放这个音频标签,并且我能够在几秒钟内将进度存储到cookie中。 但我无法从那块饼干开始播放音频。(在其他页面上继续)

对于.get[0],它也不起作用


有人能帮我把事情弄清楚吗?非常感谢

在设置当前时间之前,您需要等待
音频
源加载

$(function(){
    $('audio').bind('canplay', function(){
        $(this)[0].currentTime = $.cookie('audioTime');
    });
});

首先,代码中有一个错误,因为currentTime不是jQuery的一部分(但您已经知道这一点)

我发现音频标签有一些奇怪的东西,在不同的浏览器中可以进行不同的操作,例如在Chrome中

首先,您必须等待“
durationchange
”事件,以确保对象知道长度

在此之后,您必须使用“
play()
”(如果尚未启动)启动流,并使用“
pause()
”函数暂停流(有时经过短暂延迟)。然后可以使用该值更改“currentTime”属性。在此之后,您必须使用
'play()
'函数再次启动流

有时还需要使用“
load()
”函数自己加载流

大概是这样的:

$(document).ready( function()
{
var a = $('audio:first'),
    o = a[0];

a.on( 'durationchange', function(e)
{
  var o = e.target;
  if( o )
  {
   o.pause();
   o.currentTime = parseInt( $.cookie("audioTime"));
   o.play();
  } 
});

if( o )
{
  o.load();
  o.play();
}
});
你必须用它来确定在你的情况下什么是最好的,例如用resume(play-reach)方法来延迟一秒钟左右

当使用此方法时,您不必使用自动播放功能,因为大多数情况下它不起作用

希望有帮助,格里茨,
Erwinus

在我的案例中,我发现某个地方存在着上下文问题。我在窗口上下文下初始化音频,但当我尝试从XMLHttpRequest响应更改currentTime时,它不起作用。我还不知道答案,但我提供了一个线索,也许Javascript专家会知道如何让它工作

/* initialize this.audio player */
Audio = function() {
    var that = this;
    // keep track of playback status
    var AudioStatus = {
            isPlaying : false
    };

    // define references to this.audio, pulldown menu, play-button, slider and time display
    that.audio = document.querySelector("AUDIO");

    /* load track by menu-index */
    var loadTrack = function() {

        if(that.audio == null){
            log("audio null"); return;
        }
        that.audio.src = '../sounds/400.mp3';
        that.audio.load();
    };

    /* callback to play or pause */
    that._play = function() {
        if(that.audio == null){
            log("audio null"); return;
        }  
        that.audio.play();
        AudioStatus.isPlaying = true;
    };

    that._pause = function() {

        if(that.audio == null){
            log("audio null"); return;
        }
        that.audio.pause();
        AudioStatus.isPlaying = false;
    };

    that.playPause = function() {
        if (that.audio.paused) {
            self._play();
        }
        else {
            self._pause();
        }
    };

    /* callback to set or update playback position */
    that.updateProgress = function(value) {
        if(that.audio == null){
            log("audio null"); return;
        }
        that.audio.currentTime = value;    // <<<--- it does NOT work if I call from XMLHttpRequest response but it DOES if it is called from a timer expired call back
    };

    that.isAudioPlaying = function(){
        return AudioStatus.isPlaying;
    };
};
/*初始化此音频播放器*/
音频=函数(){
var=这个;
//跟踪播放状态
var音频状态={
isPlaying:错误
};
//定义对此的引用。音频、下拉菜单、播放按钮、滑块和时间显示
that.audio=document.querySelector(“音频”);
/*按菜单索引加载曲目*/
var loadTrack=函数(){
如果(that.audio==null){
日志(“音频空”);返回;
}
that.audio.src='../sounds/400.mp3';
that.audio.load();
};
/*回调以播放或暂停*/
那._play=function(){
如果(that.audio==null){
日志(“音频空”);返回;
}  
那个。音频。播放();
AudioStatus.isplay=true;
};
那._pause=function(){
如果(that.audio==null){
日志(“音频空”);返回;
}
那。音频。暂停();
AudioStatus.isplay=false;
};
that.playPause=函数(){
如果(that.audio.paused){
赛尔夫;
}
否则{
自我;
}
};
/*回调以设置或更新播放位置*/
that.updateProgress=函数(值){
如果(that.audio==null){
日志(“音频空”);返回;
}

that.audio.currentTime=value;//您可以通过向URL添加
t=
来设置开始时间,如下所述:

例如,
这对我很有用

if (!isNaN(audio.duration)) {
    audio.currentTime = 0;
}
希望有帮助!

这为我解决了问题:


$(“p#声音音频”)。在('loadedmetadata',()=>{
$(“p#声音音频”).get(0.load();
});

因此,我可以稍后设置
currentTime
,而不必担心音频是否已加载。

我也遇到了同样的问题。我已通过将回调绑定到Brian Hadaway提到的
canplay
事件来确保音频已加载。即使如此,
currentTime
也不会更改。还有这个问题问题。不知道在哪里进行调试。一切似乎都是正确的,但currentTime无法设置…这并没有解决我的问题,但+1用于尝试并说明HTML中对声音的支持有多差。在代码的第一行观察中,我看到您分配了一个函数(非protyped用户类)对于HTML5音频类定义。音频是HTML5 API的一部分,请不要尝试使用这些词。音频是HTML中音频标记的Javascript版本。如果要创建类,请使用例如音频这样的名称,以避免API冲突。此外,您的查询选择器可能错误,音频必须是音频。另请参阅:
$(document).ready( function()
{
var a = $('audio:first'),
    o = a[0];

a.on( 'durationchange', function(e)
{
  var o = e.target;
  if( o )
  {
   o.pause();
   o.currentTime = parseInt( $.cookie("audioTime"));
   o.play();
  } 
});

if( o )
{
  o.load();
  o.play();
}
});
/* initialize this.audio player */
Audio = function() {
    var that = this;
    // keep track of playback status
    var AudioStatus = {
            isPlaying : false
    };

    // define references to this.audio, pulldown menu, play-button, slider and time display
    that.audio = document.querySelector("AUDIO");

    /* load track by menu-index */
    var loadTrack = function() {

        if(that.audio == null){
            log("audio null"); return;
        }
        that.audio.src = '../sounds/400.mp3';
        that.audio.load();
    };

    /* callback to play or pause */
    that._play = function() {
        if(that.audio == null){
            log("audio null"); return;
        }  
        that.audio.play();
        AudioStatus.isPlaying = true;
    };

    that._pause = function() {

        if(that.audio == null){
            log("audio null"); return;
        }
        that.audio.pause();
        AudioStatus.isPlaying = false;
    };

    that.playPause = function() {
        if (that.audio.paused) {
            self._play();
        }
        else {
            self._pause();
        }
    };

    /* callback to set or update playback position */
    that.updateProgress = function(value) {
        if(that.audio == null){
            log("audio null"); return;
        }
        that.audio.currentTime = value;    // <<<--- it does NOT work if I call from XMLHttpRequest response but it DOES if it is called from a timer expired call back
    };

    that.isAudioPlaying = function(){
        return AudioStatus.isPlaying;
    };
};
if (!isNaN(audio.duration)) {
    audio.currentTime = 0;
}