简体   繁体   中英

Objective-c, UISlider in AVPlayer not working fine, after scrubber

I have implemented AVPlayer which is play the audio song from an URL. It works fine until I (scrubber) forward/backward the song. UISlider goes to end according the song length and totally works fine. But whenever I try to forward or backward song then , UISlider does not work fine. For example : My song length is 2.46 (min). UISlider reaches a end but song is still playing . (sons is not complete yet). Even I did hard code set maximum value of sliderbar = 2.46 , but not working . On the other hand if i set value of sliderbar = 3.5 (or aruond 4) , then it works fine. Any one has idea why it is happening .Thanks in advance!!!!!

Complete code : http://www.mediafire.com/download/tcpno1y3x8ocmri/AVPlayer_Demo.zip My Sample code :

- (void)updateTime:(NSTimer *)timer {

    currentTime = CMTimeGetSeconds(playerItem.currentTime);
    duration = CMTimeGetSeconds(playerItem.duration);
    [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 ];
    self.audioCurrentTimeLabel.text = timeInfoString;

}

- (IBAction)audioPlayerValueChanged:(UISlider *)aSlider  {

    [player seekToTime:CMTimeMakeWithSeconds(duration_seconds*aSlider.value, 1)];
}

- (IBAction)touchDown:(id)sender {

    [player pause];
    [nsTimer2 invalidate];
}

- (IBAction)touchUp:(id)sender {

   [player play];
   nsTimer2=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];

}

You're calculating the time duration_seconds*aSlider.value but the slider value is not a from 0..1. You should either use the aSlider.value or keep the current code but change the slider min/max = (0,1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM