Oscar
Oscar

Reputation: 1103

How to play specific start and end duration in HTML5 audio?

I got a audio duration which is 5 minutes = 5:00, but it's too long to listen whole audio, so I got a tag which is from example 2:00 minutes to 3:00 minutes, So what I want to do is play the tag in the HTML5 audio which mean play from 2:00 minutes to 3:00 minutes.

Here is what I found how to play the start duration

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

Update

Here is my code

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

Here is my tag sample

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 Posted Data

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

What I did was when click the start tag, it's will go to the current audio time

Upvotes: 1

Views: 4093

Answers (1)

Sacho
Sacho

Reputation: 2179

You can listen to the timeupdate event and pause the video once it has reached a currentTime of 3 minutes.

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

Here's a list of events you can listen for on a media element: MDN

Upvotes: 3

Related Questions