0

i have this function to play music with web audio API:

function playMusic(){

 if(countPre<count ){
    audio0.play();      
    audio0.src = '0.mp3';
    audio0.controls = true;
    audio0.autoplay = true;
    audio0.loop = true;
    source = context.createBufferSource();
    source.connect(analyser);
    analyser.connect(context.destination);
    }
 else{audio0.pause();}

 }

However, the value of count and countPre are generated in a loop that runs 10 times per second.

I have to put the function playMusic inside that loop in order to update the values.

And here comes the problem:

I call playMusic 10 times per second! Every time, the music resumes.

I don't want it resumes, i want it plays continuously as long as the play condition is matched.

So is there any solution?

Oskar Eriksson
  • 2,591
  • 18
  • 32
kikkpunk
  • 1,287
  • 5
  • 22
  • 34
  • @Oskar Eriksson, what's the difference between web-audio and html5-audio? – kikkpunk Oct 29 '12 at 11:56
  • The – Oskar Eriksson Oct 29 '12 at 13:17
  • Actually, your example does use Web Audio. Sorry. – Oskar Eriksson Oct 29 '12 at 14:36

1 Answers1

0

You have to check that music is not already playing in your condition.

Don't think there's an audio method for that. You can do it by many ways, example :

function playMusic(){

if(countPre<count && !this.is_playing ){
    this.is_playing = true;    
    audio0.play();      
    audio0.src = '0.mp3';
    audio0.controls = true;
    audio0.autoplay = true;
    audio0.loop = true;
    source = context.createBufferSource();
    source.connect(analyser);
    analyser.connect(context.destination);
    }
 else{audio0.pause();
    this.is_playing = false}
 }
theredled
  • 998
  • 9
  • 20