我是新来的 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];
}
}
以上I 中将使用一个计时器作为类变量
after complete the playing of song it goes to AVAudioPlayerDelegate that is audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
在此显示此音频Player 已完成播放的代码
-(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];
}
开始的时候, 我将取消计时器, 然后获得下一首歌路径并开始播放, 但我不知道如何启动在以上方法中停止的计时器
如果任何机构知道这个 请帮助我..