我在我的应用中使用avaudioplayer。在viewwilldisappear我想暂停/停止声音,并在viewwill出现我想再次播放声音。我这样做吗?我在viewWillAppear上使用此代码: -
if(self.TickPlayer)
{
[self.TickPlayer play];
}
if(self.TickPlayer.volume<0)
{
self.TickPlayer.volume=1.0;
}
,这在viewWillDisAppear
上 if(self.TickPlayer)
{
[self.TickPlayer stop];
}
这是播放声音的方法
-(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];
}
}
我正在使用定时器每秒触发的方法调用它
在viewDidLoad -
中 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES];
-(void) updateCountdown {
[self PlayTickTickSound:@"tick.caf"];
}
当警报出现时,我也会在特定条件下使用avaudioplayer发出哔哔声。这样可以正常工作但是这没有按预期工作
答案 0 :(得分:1)
看起来像大写问题 - viewWillDisAppear永远不会被调用,因为签名不正确,正确的签名是:
- (void)viewWillDisappear:(BOOL)animated