2012-06-14 80 views
1

我使用AVAudioplayer播放音頻文件播放/暫停按鈕來播放之間切換和暫停NSTimeraudio player陸續view controllers一個同步視圖控制器。我總共有10個或11個view controllers一個接一個地加載。暫停按鈕時,再次按下恢復重裝

當播放按鈕被按下啓動NSTimer的第一次,開始播放的audio file和基於NSTimer,視圖控制器會加載一前一後。當再次按下暫停按鈕以恢復時,它將從該點音頻文件NSTimerview 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裝載的想法。

回答

0

我收集你想在音軌中經過11秒後顯示FirstViewController
您可以將您的計時器設置爲輪詢(每個.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動作,你不需要算暫停時間。

相關問題