0

我是 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 *)player 成功:(BOOL)flag

这里是这个 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];

}

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

如果有人知道这一点,请帮助我..

4

2 回答 2

0

timer 一般用于时间增量,以秒为单位。在你的方法中

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 “开火日期” 保留调用该方法的时间的开火细节。有关更多详细信息,请阅读火灾日期。

于 2012-05-24T13:56:35.293 回答
0

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

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sliderTintChanged:) userInfo:nil repeats:YES];
于 2012-05-24T12:57:30.970 回答