Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML audio: currentTime in IE

I'm trying to play a sound (mp3) starting at a certain point in the sound file. I want it to work in most mobile devices, and it does, but it seems there is a problem with Windows Phone 8. It works fine on a Samsung Galaxy and on the iPhone, but on WP the sound always starts from the beginning, ignoring the currentTime set. Any ideas? Thanks!

var sfile = "test.mp3';
    var myobj;
    function init()
    {

         myobj=document.createElement('audio');
        myobj.style.position="relative"; 
        myobj.style.bottom=0;
        myobj.src = sfile;
        myobj.id='sound';

        document.body.insertBefore(myobj,document.body.lastChild);
        myobj.load();
    }
    //called when pressing a button
    function test()
    {
        myobj.currentTime=30.0;
        myobj.play()
    }
like image 449
user1987412 Avatar asked Nov 12 '22 13:11

user1987412


1 Answers

Got some more information on this. Turns out you cannot set currentTime before making the play() call. At least not on the Windows phone. So I have to call play first and then (after x ms) I can set the currentTime.

My idea now is to have some silence in my sound file and loop that silence continuously. When I want to play a sound I can then just set the currentTime. When the sound part has finished I just start the silence loop again. It would be much easier to use multi tracks but I don't have that option unfortunately.

So, it is possible to use currentTime on Windows phone 8. However I bumped into another problem. Since I'm now playing the sound all the time, when the phone goes into sleep mode it seems the javascript freezes (causing the silence loop to stop) but to sound plays anyway. So the whole mp3 file would run from beginning to the end. I tried using the onfocusout event to stop the loop and pause the sound but it doesn't work. Will continue to investigate this rather rare and buggy behaviour.

like image 67
user1987412 Avatar answered Dec 14 '22 14:12

user1987412