开发者

why AVAudioplayer doesn't stop/pause when viewWillDisappear?

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 b开发者_JAVA百科ut this is not working as expected


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

- (void)viewWillDisappear:(BOOL)animated
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜