我得到的音频持续时间是 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>
这是我的标签示例
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/