views:

394

answers:

2

i want to play a sound file when alertview appears and plays continuously till user clicks on ok or cancel.how do i do this?

+1  A: 

As you already call the show method to display the dialog, why don’t you simply start playing the sound there and stop in the alert view callback? For the sound itself you can use AVAudioPlayer.

zoul
how do i continuously play that sound? and manually stop the sound from playing...also tell me that if another sound is already running will it work?
Rahul Vyas
can you post a sample code
Rahul Vyas
+3  A: 

As Zoul says, you set-up and play your sound as you call [myAlert show] and cancel the sound in the alert view callback. Your code will look something like this:

  AVAudioPlayer *myPlayer;

  // ...

  // create an alert...

  NSError *error;
  myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:mySoundFileURL error:&error];
  // handle errors here.
  [myPlayer setNumberOfLoops:-1];  // repeat forever
  [myPlayer play];
  [myAlert show];

  // ...

  // in alert callback.
  [myPlayer stop];
  [myPlayer release];
Olie