我使用AVAudioplayer
到播放音頻文件,播放/暫停按鈕來播放之間切換和暫停和NSTimer
與audio player
陸續view controllers
一個同步視圖控制器。我總共有10個或11個view controllers
一個接一個地加載。暫停按鈕時,再次按下恢復重裝
當播放按鈕被按下啓動NSTimer
的第一次,開始播放的audio file
和基於NSTimer
,視圖控制器會加載一前一後。當再次按下暫停按鈕以恢復時,它將從該點音頻文件NSTimer
和view controllers
中恢復,從暫停的同一點開始,這很好。但是,在同一時間開始根據NSTimer
一個接一個地重新加載view controllers
,這是不好的。
據重裝視圖控制器這種說法
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:@selector(displayviewsAction:)
userInfo:nil
repeats:NO];
所以我的問題是,我需要從一開始就再次停止的view controllers
裝載暫停時再次按下按鈕恢復播放。
-(void)playpauseAction:(id)sender
{
if ([audioPlayer isPlaying]) {
[sender setImage:[UIImage imageNamed:@"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
} else {
[sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:@selector(displayviewsAction:)
userInfo:nil
repeats:NO];
}
}
-(void)pauseTimer{
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFireDate = [[timer fireDate] retain];
[timer setFireDate:[NSDate distantFuture]];
}
-(void)resumeTimer{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
[pauseStart release];
[previousFireDate release];
}
- (void)displayviewsAction:(id)sender
{
First *firstController = [[First alloc] init];
firstController.view.frame = CGRectMake(0, 0, 320, 480);
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setDuration:1];
[transitionAnimation setType:kCATransitionFade];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];
[self.view addSubview:firstController.view];
[self.view addSubview:toolbar];
[firstController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:@selector(Second) userInfo:nil repeats:NO];
}
任何人都可以就如何暫停時再次按下按鈕恢復播放再次從一開始就停止view controllers
裝載的想法。