1

EDIT : trying to implement Adam B's answer now...

I have a crashSound.wav file that I have put in my Supporting Files folder of my xCode project

I'm now trying to make it play inside a while loop, but the documentation isn't very clear as to how exactly I can do that. I know I have to create a delegate object somewhere (I guess my Game class) and get notifications as to whether stopButtonPressed is false and whether the file has finsihed playing so that it can loop and play again while the stopButtonPressed condition is false.. and I know I shouldn't be doing that by calling the [crashSound play] method but I'm not sure how to do it.. Any help?

@interface Game()
{
    // code...

    AVAudioPlayer *crashSound;

    // code...
}
@end

@implementation Game

- (id) init 
{ 
    // code...

    NSURL *crashSoundFile = [[NSURL alloc] initWithString:@"crashSound" ];

    crashSound = [[AVAudioPlayer alloc] initWithContentsOfURL:crashSoundFile error:NULL];

    // code...
}
-(void) play // this is my "main" method that will be called once the playButton is pressed
{
   while(!self.stopButonPressed)
   {
      [crashSound play];
   }
}
@end

1 Answers1

0

You're structuring your code wrong for how most multimedia libraries would work (including AVPlayer, for example).

Instead of having a while loop, you would start the sound playing, and check if your condition is true each time it completes. You may need to trigger the "next" thing based on a timer or in that callback when the sound completes, instead of having the loop.

See this question and good answer for an example of how to setup AVPlayer and the notification function playerItemDidReachEnd:: Looping a video with AVFoundation AVPlayer?

Community
  • 1
  • 1
Adam B
  • 3,775
  • 3
  • 32
  • 42
  • You're right. I'm thinking of using the AudioToolbox framework with its System Sound Services interface. It has a this AudioServicesAddSystemSoundCompletion function that can let you know when the sound has finished playing..etc.. : https://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html I hope I'm on the right track.. –  Aug 09 '12 at 17:07
  • 1
    I'd probably recommend using a higher level construct, like [AVAudioPlayer](https://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html#//apple_ref/occ/cl/AVAudioPlayer). It has a lot of additional features that you may want to leverage later. In fact, it has a construct to loop forever. In that case you could wait until you want to stop it and call `stop`. – Adam B Aug 09 '12 at 17:18
  • Solution : http://stackoverflow.com/questions/11890576/how-can-i-play-the-same-sound-again-once-it-has-finished-playing-using-the-avaud –  Aug 11 '12 at 14:34