私は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];
}
}
上記では、クラス変数としてタイマーを使用します
曲の再生が完了すると、audioPlayerDidFinishPlaying:(AVAudioPlayer *)playerであるAVAudioPlayerDelegateに正常に移動します:(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];
}
上記の開始時にタイマーを無効にしてから次の曲のパスを取得して再生を開始しますが、上記の方法で停止したタイマーを開始する方法がわかりません
誰かがこれを知っているなら、私を助けてください。