再次按下暂停按钮以恢复重新加载视图控制器 [英] pause button when pressed again to resume reloading view controllers

查看:89
本文介绍了再次按下暂停按钮以恢复重新加载视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 AVAudioplayer 播放音频文件播放/暂停按钮在播放和暂停之间切换 NSTimer 音频播放器一个接一个地同步查看控制器 。我总共有10或11 视图控制器一个接一个地加载。

I am using AVAudioplayer to play audio files, play/pause button to toggle between play and pause and NSTimer with audio player to synchronize view controllers one after another. I have a total of 10 or 11 view controllers to load one after the other.

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

When play button is pressed the very first time it starts NSTimer, starts playing an audio file and based on NSTimer, view controllers will load one after the other. When pause button is pressed again to resume it resumes everything from that point audio file, NSTimer and view controllers from the same point where it was paused which is good. However, at the same time it starts loading the view controllers again one after the other from the beginning based on NSTimer which is not good.

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

It is reloading view controllers with this statement

self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
                                              target:self
                                            selector:@selector(displayviewsAction:)
                                            userInfo:nil
                                             repeats:NO];

所以我的问题是我需要停止加载视图控制器再次从按下暂停按钮开始再次播放。

So my issue is that I need to stop the loading of view controllers again from the beginning when pause button is pressed again to resume playback.

-(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];     
 }

任何人都可以知道如何停止加载<$ c $再次按下暂停按钮以重新开始播放时,从头开始再次查看控制器。

Can anyone give an idea on how to stop the loading of view controllers again from the beginning when pause button is pressed again to resume playback.

推荐答案

我收集你试图在音频轨道中经过11秒后显示 FirstViewController

您可以将计时器设置为轮询(每0.5至1.0秒),如果audioPlayer的经过时间大于11.0,则执行您的操作。

I gather you're trying to display FirstViewController after 11 seconds have elapsed in the audio track.
You can set you timer up to poll (every .5 - 1.0 seconds) and if the audioPlayer's elapsed time is greater than 11.0, perform your action.

-(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 操作,您无需计算暂停时间。

This also get's rid of your resumeTimer/pauseTimer actions, you don't need to count the time paused.

这篇关于再次按下暂停按钮以恢复重新加载视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆