1

我的音频持续时间是 5 分钟 = 5:00,但是听整个音频太长了,所以我有一个标签,例如从 2:00 分钟到 3:00 分钟,所以我想做的是播放HTML5 音频中的标签,表示从 2:00 分钟到 3:00 分钟播放。

这是我发现的如何播放开始时间

$('#audio').bind('canplay', function() {
  this.currentTime = 29; // jumps to 29th secs
});

更新

这是我的代码

 <audio id="audio" controls="controls" class="span4">
  <source src="file/test.mp3" type="audio/ogg">
  <source src="file/test.mp3" type="audio/mpeg">
  <source src="file/test.mp3" type="audio/wav">
  Your browser does not support the audio element. Please try other browser
</audio>

这是我的标签示例

在此处输入图像描述

AJAX

$('.start_tag').click(function() {
    var sec =  $(this).attr('data-id');
    var file =  $(this).attr('data-file');
    show_tag(sec,file);
});

function show_tag(sec,file)
{
    $.ajax({
        type: "POST",
        url: config.base_url + 'index.php/qm/show_tag',
        data : {
            sec : sec,
            file : file,
        },
        success : function(data) {
            $('.show_tag').empty().append(data);
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest + " : " + textStatus + " : " + errorThrown);
        }
    });

}

阿贾克斯发布的数据

  function show_tag()
    {
        $sec = $this->input->post('sec');
        $record_filename = $this->input->post('file');
        $table = '<h4 style="margin-left:31px;">Tagged File</h4>';
        $table .= '<audio id="audio" controls="controls" class="span4 video1">';
        $table .= '<source src="'.base_url().'file/'.$record_filename.'" type="audio/ogg">';
        $table .= '<source src="'.base_url().'file/'.$record_filename.'" type="audio/wav">';
        $table .= '<source src="'.base_url().'file/'.$record_filename.'" type="audio/mpeg">';
        $table .= 'Your browser does not support the audio element. Please try other browser';
        $table .= '</audio>';
        $table .= '<script>';
        $table .= "$('#audio').bind('canplay', function() {";
        $table .= 'this.currentTime = '.$sec.';';
        $table .= '}); </script>';

/*      $table .= '<script>';
        $table .= "$('#audio').bind('timeupdate', function() {";
        $table .= ' if (this.currentTime >= '.$sec.') this.pause();';
        $table .= '}); </script>';
         */

        echo $table;
    }

我所做的是单击开始标签时,它将转到当前音频时间

4

1 回答 1

3

您可以收听该timeupdate事件并在视频达到 3 分钟的 currentTime 时暂停该视频。

$('#audio').bind('timeupdate', function () {
    if (this.currentTime >= 180) this.pause();
}

以下是您可以在媒体元素上侦听的事件列表: MDN

于 2013-11-06T06:33:01.980 回答