再次按下暂停按钮以恢复重新加载视图控制器

时间:2012-06-14 13:48:58

标签: iphone

我正在使用AVAudioplayer 播放音频文件播放/暂停按钮在播放和暂停之间切换NSTimer {{1}一个接一个地同步audio player。我总共有10个或11个view controllers一个接一个地加载。

当第一次启动view controllers时按下播放按钮,开始播放NSTimer并基于audio file,视图控制器将一个接一个地加载。当再次按下暂停按钮以恢复时,它将从该点音频文件NSTimerNSTimer恢复从暂停的同一点恢复所有内容,这是好的。但是,同时它开始基于view controllers从一开始就一个接一个地加载view controllers,这是不好的。

使用此语句重新加载视图控制器

NSTimer

所以我的问题是,当再次按下暂停按钮以恢复播放时,我需要从头开始再次加载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]; }

1 个答案:

答案 0 :(得分:0)

我收集你试图在音频轨道中经过11秒后显示FirstViewController
您可以将计时器设置为轮询(每隔0.5到1.0秒),如果audioPlayer的经过时间大于11.0,则执行操作。

-(void)playpauseAction:(id)sender 
{ 
if ([audioPlayer isPlaying]){
   [sender setImage:[UIImage imageNamed:@"Play Icon.png"] forState:UIControlStateSelected];
   [audioPlayer pause];
   [self.timer invalidate]; // stop the polling timer
} else {
   [sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
   [audioPlayer play];
   // start/resume the timer
   self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 /* polling interval */
                                                      target:self
                                                   selector:@selector(tryToDisplayViews:)
                                                    userInfo:nil
                                                     repeats:YES]; /* REPEAT = YES */
}       
}

// the new polled method
-(void)tryToDisplayViews:(id)sender {
    // need last current time as a new variable to ensure that we only add the view once
    // and not every time the method fires with currentTime > 11.0
    if( audioPlayer.currentTime > 11.0 && lastCurrentTime < 11.0) {
        [self displayviewsAction:nil];
    } 
    // you can add the second view here in a similar fashion
    // if(audioPlayer.currentTime > (23.0+11.0) && lastCurrentTime < (23.0+11.0) ) {}

    lastCurrentTime = audioPlayer.currentTime;

}

这也可以摆脱您的resumeTimer / pauseTimer行为,您无需计算暂停的时间。