如何在iphone中启动过去停止的同一个计时器

时间:2012-05-24 12:53:07

标签: iphone

我是iphone的新手。我正在研究音频播放器,因为我在编写此代码时在音频播放器中点击播放按钮时涉及到定时器

- (IBAction)playButtonClicked:(id)sender {
    bookTitleString=[[selectBook titleLabel]text];
    startChapterString =[[startChapter titleLabel]text];
    NSString *string = [[NSBundle mainBundle]pathForResource:startChapterString ofType:@"mp3" inDirectory:bookTitleString];
    NSLog(@"string is %@",string);
    NSURL *url = [NSURL fileURLWithPath:string isDirectory:YES];
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    AVURLAsset *audioAsset = [AVURLAsset assetWithURL:url];
    CMTime audioDuration = audioAsset.duration;
    audioDurationInSeconds = CMTimeGetSeconds(audioDuration);
    NSLog(@"audioDurationInSeconds is %f",audioDurationInSeconds);

    slider.userInteractionEnabled= YES;
    slider.minimumValue = 0.0;
    slider.maximumValue = audioDurationInSeconds;
    slider.value = 0.0;
    slider.continuous = YES;
    audioPlayer.delegate = self;
    if(audioPlayer == nil){
        NSLog(@"audio player is nil");
    }
    else
    {
        NSLog(@"audio player is not nil");
        NSLog(@"audioplayer.deviceCurrentTime is %f",audioPlayer.deviceCurrentTime);
        [audioPlayer play];
        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sliderTintChanged:) userInfo:nil repeats:YES];
    }
}

在上面我将把一个计时器作为一个类变量

完成歌曲播放后,它转到AVAudioPlayerDelegate audioPlayerDidFinishPlaying :( AVAudioPlayer *)播放器成功:(BOOL)标志

此audioPlayerDidFinishPlaying的代码

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    [timer invalidate];
    NSLog(@"song finished playing");
    NSString *startSongNumber = [[startChapter titleLabel]text];
    NSLog(@"startSongNumber is %@",startSongNumber);
    int songNumber = [startSongNumber intValue];
    NSLog(@"songNumber is %d",songNumber);
    songNumber = songNumber+1;
    NSLog(@"songNumber is %d",songNumber);
    NSString *nextSongNumber = [NSString stringWithFormat:@"%d",songNumber];
    NSLog(@"next song number is %@",nextSongNumber);
   NSString *string = [[NSBundle mainBundle]pathForResource:nextSongNumber ofType:@"mp3" inDirectory:bookTitleString];
    NSLog(@"string is in playerdidfinish is %@",string);
    NSURL *url = [NSURL fileURLWithPath:string isDirectory:YES];
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    AVURLAsset *audioAsset = [AVURLAsset assetWithURL:url];
    CMTime audioDuration = audioAsset.duration;
    audioDurationInSeconds = CMTimeGetSeconds(audioDuration);
    NSLog(@"audioDurationInSeconds is %f",audioDurationInSeconds);
    [audioPlayer play];

}

这里上面的开头我使定时器无效,然后得到下一首歌曲路径并开始播放,但我不知道如何启动在上述方法中停止的定时器

如果有人知道这个请帮帮我..

2 个答案:

答案 0 :(得分:0)

就像你在playButtonClicked方法中所做的那样......

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sliderTintChanged:) userInfo:nil repeats:YES];

答案 1 :(得分:0)

计时器通常用于时间增量,它以秒为单位。 在你的方法

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sliderTintChanged) userInfo:nil repeats:YES];

每秒钟后的计时器调用方法。

-(void)sliderTintChanged
{
   //you can use a variable which will keep timer value
   //like this
   count++;
}

count是一个整数变量assign int count = 0; 当计时器停止时,计数的时间以秒为单位,您可以使用它。

和NSTimer“开火日期”会保留方法调用时的详细信息。 了解更多细节阅读消防日期。