HTML5 audio - get time played of a sound object (howler.js)
Asked Answered
J

1

8

Im playing some HTML5 audio using the Howler.js library.

Currently im able to determine the total length of the audio file with sound.duration(); however im not sure how to create a timer to show how much time that has been played.

I create a simple sound object like so:

var sound = new Howl({
    src: ['sound.ogg', 'sound.mp3', 'sound.wav'],
    autoplay: true,
    loop: false,
    volume: 1,
    onload: function() {
        var totalSoundDuration = sound.duration();
    },
    onplay: function(getSoundId) {
        //sound playing
    },
    onend: function() {
        //sound play finished
    }
});

I can't seem to find any method within the library (?) to check for currentTime so I can update my timer function.

An alternative route could simply be to trigger/toggle a setInterval upon onplay: like:

var currentTimeTracker=setInterval(function () {myTimer()}, 1000);

function myTimer() {
    timePlayed++;

    $("#time" ).html(timePlayed);
}

Not sure if that would be a good approach? Any suggestions?

Howler.js ref: https://github.com/goldfire/howler.js/tree/2.0#global-core-properties

Jeffry answered 15/12, 2014 at 10:50 Comment(0)
C
2

According to http://goldfirestudios.com/blog/104/howler.js-Modern-Web-Audio-Javascript-Library sound.pos() should get the current position. The posmethod can get or set, depending on whether you pass through the optional position parameter.

Confiscatory answered 15/12, 2014 at 11:26 Comment(2)
Since you are using howler.js 2.0, the seek method will work the same as pos in this instance: github.com/goldfire/howler.js/tree/2.0#seekseek-id.Perfumery
Thanks @JamesSimpson that's correct for the v2.0 branch. I think the v2.0 docs need updating as references are still made to pos as being the playback position rather than the 3D spatial position: github.com/goldfire/howler.js/tree/2.0#pauseidConfiscatory

© 2022 - 2024 — McMap. All rights reserved.