I want to implement Scrubber (UISlider) in AVPlayer. I have tried but I did not success completely (sometimes slider bar show incorrect value or sometimes progress timing bar). Any suggestion will be great.
-(void)playButtonClicked
{
// checks internet connnection : working or not
if([InternetUtility testInternetConnection])
{
if (audioButton.selected)
{
audioButton.selected = NO;
printf("\n\ndo audioButton.selected = NO; \n\n");
}
else
{
printf("\n\ndo audioButton.selected = YES; \n\n");
audioButton.selected = YES;
}
if (!isAlredayPlaying) {
printf("\n\nStarted song from 0\n");
isAlredayPlaying=YES;
NSURL *url = [NSURL URLWithString:AUDIO_FILE_URL];
playerItem = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:playerItem];
player.volume=5.0;
[self.audioSliderBar setMinimumValue:0.0];
nsTimer=[NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
[player play];
}
else
{
if (isCurrentlyPlaying)
{
[nsTimer invalidate];
isCurrentlyPlaying=NO;
[player pause];
}
else
{
nsTimer=[NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
isCurrentlyPlaying=YES;
[player play];
}
}
} // internet connection check funtion, ended
else {
[ViewUtilities showAlert:INTERNET_CONNETION_TITLE :INTERNET_CONNECTION_MESSAGE];
}
}
- (void)updateTime:(NSTimer *)timer {
currentTime = CMTimeGetSeconds(playerItem.currentTime);
duration = CMTimeGetSeconds(playerItem.duration);
if (currentTime==duration) {
[self itemDidFinishPlaying];
}
[ self.audioSliderBar setValue:(currentTime/duration)];
float minutes = floor(currentTime/60);
seconds =currentTime - (minutes * 60);
float duration_minutes = floor(duration/60);
duration_seconds =
duration - (duration_minutes * 60);
NSString *timeInfoString = [[NSString alloc]
initWithFormat:@"%0.0f:%0.0f",
minutes, seconds ];//],
//duration_minutes, duration_seconds];
if (!(player.currentItem && player.rate != 0 )&& isCurrentlyPlaying)
{
if (audioPlayerWillStop) {
isAlredayPlaying=NO;
[nsTimer invalidate];
}
else {
[player play];
printf("\n\n\n Force to play song...");
}
}
self.audioCurrentTimeLabel.text = timeInfoString;
}
- (IBAction)audioPlayerValueChanged:(UISlider *)aSlider {
CMTime newSeekTime = CMTimeMakeWithSeconds(aSlider.value*seconds,1) ;
[player seekToTime:newSeekTime];
// This code not working fine completely (slider value goes back up continuously) Please help
}