why AVAudioplayer doesn't stop/pause when viewWillDisappear?

Posted by Rahul Vyas on Stack Overflow See other posts from Stack Overflow or by Rahul Vyas
Published on 2009-11-07T08:30:15Z Indexed on 2010/04/09 9:03 UTC
Read the original article Hit count: 592

I am using avaudioplayer in my app. on viewwilldisappear i want to pause/stop the sound and on viewwill appear i want to play sound again.how do i do this? i'm using this code on viewWillAppear:-

 if(self.TickPlayer)
 {
  [self.TickPlayer play];
 }
 if(self.TickPlayer.volume<0)
 {
  self.TickPlayer.volume=1.0;
 }

and this on viewWillDisAppear

 if(self.TickPlayer)
 {
        [self.TickPlayer stop];
 }

here is the method which plays the sound

-(void)PlayTickTickSound:(NSString*)SoundFileName
{

//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],[NSString stringWithFormat:@"/%@",SoundFileName]];
//Get a URL for the sound file
 NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
 NSError *error;
if(self.TickPlayer==nil)
{
 self.TickPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:&error];
  // handle errors here.
  self.TickPlayer.delegate=self;
  [self.TickPlayer setNumberOfLoops:-1];  // repeat forever
  [self.TickPlayer play];
 }
if(self.TickPlayer)
{
 [self.TickPlayer play];
}

}

and i'm calling it in method which fires every second using a timer

in viewDidLoad-

 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES]; 

-(void) updateCountdown {
   [self PlayTickTickSound:@"tick.caf"];
}

i'm also playing a beep sound using avaudioplayer at a specific condition when alert appears.that works fine but this is not working as expected

© Stack Overflow or respective owner

Related posts about iphone-sdk-3.0

Related posts about objective-c