0
votes

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

1
I'd like to see more code. I do the same in my apps and have no problems starting the sounds when my view appears and stopping when the view disappears. - mahboudz
i've edited my question and posted more code - Rahul Vyas
or if you tell me a way that i could play one sound for countdown and second for alert.means i want both sounds countdown is permanent and alert beeps on conditions.also is there a way to repeat system sound and stop whenever i want to stop. - Rahul Vyas
by the way system sound is also not working.however tick.caf runs and also beep as i want just want to disable tick sound when view disappears,Because I'm using audio recording also.And when i use video recording feature it hangs my app don't know why? - Rahul Vyas
What behavior is missing? Does the tick sound: (1) not play at all (2) plays but does not stop when the view closes (3) Keeps playing after the view closes? - TechZen

1 Answers

1
votes

Looks like a capitalization problem - viewWillDisAppear will never get called since the signature is incorrect, the correct signature is:

- (void)viewWillDisappear:(BOOL)animated