1

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

Community
  • 1
  • 1
Oscar
  • 1,093
  • 4
  • 21
  • 31

1 Answers1

3

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

Sacho
  • 2,169
  • 1
  • 14
  • 13
  • Probably better to use `>= 180` as the number is a float which means it's not sure it will be exactly 180 due to rounding errors. –  Nov 06 '13 at 06:37
  • Hi Sacho, After I tried your code, it's did not pause after 3:00, do you know why ? – Oscar Nov 06 '13 at 06:39
  • 1
    Check out the comment Ken made and the modified code. Past that, you need to post a sample of your code. – Sacho Nov 06 '13 at 06:59