user2875404
user2875404

Reputation: 3218

iOS AVAudioPlayer play sound again

Hey I have a couple of AVAudioPlayers containing one sound each. If I press the same button a couple of times, it should repeat the sound from the beginning. If I press another button afterwards, the running sound shall be stopped in order to "make room" for the new one.

The code I am using for that:

-(void) plays:(int)p{  // p is the index of the sound being triggered

    if([players[p] isPlaying])
      {                               // setting the time back to 0 makes
        players[p].currentTime = 0.0; // the player automatically play again
      } 
    else 
      {
        [players[p] play]; // if not playing, start playing
      }

    if(last!=p)
      {                       // if the last sound is different from the current
        [players[last] stop]; // stop the last one
        players[last].currentTime = 0.0;} // put its position back to 0

    last=p; // set the 'last' variable 

} 

However, hitting the same button again ends up in a little delay (maybe 20ms) in which no sound is heard. This is the time, the AVAudioPlayer seems to need to "rewind" the track in order to play it again. One Idea to get around this would be to create multiple objects of AVAudioPlayer for each sound but that'd make some awful code! Any ideas on how to make this process quicker?

Thanks, Alex

EDIT: playing 2 different sounds works perfectly fine, I can't hear any delay in between them as I prepareToPlay all the sounds beforehand.

Upvotes: 0

Views: 615

Answers (2)

Curmudgeonlybumbly
Curmudgeonlybumbly

Reputation: 639

I know how to eliminate the 20ms gap, but first consider if you want to.

Imagine if you jumped immediately from the mid-point of the sound file to the beginning with no gap at all. Better yet, download Audacity and hear how it sounds. Because of the discontinuity, you are going to get an unpleasant crackling or pop sound. Perhaps that 1 fiftieth of a second of silence actually sounds better than immediately restarting.

If you want an uninterrupted audio stream, you're going to have to put away the easy AVAudioPlayer interface and build an AUGraph. Of course, this means learning a complex audio interface and learning all about audio formats. And then you have to figure out what data you're going to stuff into your audio stream.

How would you make your loop sounds nice? You might try fading out at the touch point, and then fading back in. Or you could search for the zero crossings at the beginning and end of your loop (zero crossings are the place where the value of your sound wave is 0. They happen all the time in a mono output, but might be harder to find if your output is stereo.) In the end will this sound nicer than the 20 ms of silence? Get out Audacity and experiment with looping before you enter the long road of learning the AUGraph interace.

Upvotes: 1

dorayo
dorayo

Reputation: 47

To the same song, how about stopping it, then prepareToPlay and play?

Upvotes: 0

Related Questions