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