javascript - 如何在 HTML5 音频中播放特定的开始和结束持续时间?

标签 javascript html audio

我得到的音频持续时间是 5 分钟 = 5:00,但是听完整个音频太长了,所以我得到了一个标签,从示例 2:00 分钟到 3:00 分钟,所以我想要做的是在 HTML5 音频中播放标签,这意味着从 2:00 分钟播放到 3:00 分钟。

Here is what I found how to play the start duration

$('#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>

这是我的标签示例

enter image description here

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);
        }
    });

}

Ajax 发布的数据

  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;
    }

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

最佳答案

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

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

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

关于javascript - 如何在 HTML5 音频中播放特定的开始和结束持续时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19805301/

相关文章:

javascript - vue.js cli 命令 "npm run serve"是如何工作的

javascript - 在 Electron 中使用 ipc 从渲染器设置全局变量(再次)

html - 当位置是相对时子 div 的动态高度

html - 如何在水平线上显示导航栏元素以及网站名称?

python - 如何在不改变音高的情况下改变音频速度?

audio - 如何知道 mp3 文件的位深?

javascript - 如何正确等待 JavaScript 应用内联 Css

html - 使用响应式设计将元素大小从 PSD 转换为 HTML

iphone - 如何在iPhone上播放3gpp文件?

javascript - 与 SVG 矩形内联的 Div