6

I am using AVPlayer, and its working good.

I put slider and it is progression according to player duration. But i want if user drags slider then play time changes accordingly. i.e. Song forwards or backwards according to slider position.

I have tried

-(IBAction)sliderChange:(id)sender{
[player pause];
CMTime t = CMTimeMake(slider.value,1);
[player seekToTime:t];    
[player play];
}

But it again takes slider to starting point. Any help would be much appreciated.

EDIT (WORKING WITH BELOW LINK)

AVPlayer Video SeekToTime

Community
  • 1
  • 1
Mann
  • 5,477
  • 6
  • 45
  • 57

1 Answers1

13
-(IBAction) valueChangeSliderTimer:(id)sender{
    [avplayer pause];
    isPlaying = FALSE;
    [btnPauseAndPlay setTitle:@"Play" forState:UIControlStateNormal];

    float timeInSecond = sliderTimer.value;

    timeInSecond *= 1000;
    CMTime cmTime = CMTimeMake(timeInSecond, 1000);

    [avplayer seekToTime:cmTime toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
}

Edited Code .

And try this link as well : Try this link

Community
  • 1
  • 1
Dhruv
  • 2,153
  • 3
  • 21
  • 45
  • Thank you so much for answer but could you please check it again because in my question i wrote i am implementing AVPlayer not AVAudioPlayer. setCurrentTime is not visible for AVPlayer. Thanks – Mann Jun 12 '12 at 12:00
  • With this code when i touch slider then it starts from starting. it reaches at starting point. – Mann Jun 12 '12 at 13:16
  • Pause your player ,when you change the value of slider,fetch the latest value of slider and try implementing this method '-(void) sliderTimerChange{ CMTime time = [queuePlayer currentTime]; if (CMTIME_IS_VALID(time) && isPlaying) { [self showTimeOnScroll:time]; [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(sliderTimerChange) userInfo:nil repeats:NO]; } else { NSLog(@"time is invalid"); } }' – Dhruv Jun 13 '12 at 09:10
  • it shows error [self showTimeOnScroll:time]; on which object shall i call this method? – Mann Jun 13 '12 at 10:05
  • and i am binding slider method with valueChanged in IB not with touchupinside. Am i right? – Mann Jun 13 '12 at 10:06
  • 1
    Yeah,slider changes is to be bind with valuechanged only,its right,the method you getting crash is `-(void) showTimeOnScroll:(CMTime)time{ float timeOriginal = CMTimeGetSeconds(time); sliderTimer.value = timeOriginal; }` – Dhruv Jun 13 '12 at 11:04
  • here is complete handling code. http://paste.org/50513 Please note in sliderTimerChange method i am using player.rate!=0 instead of isPlaying . because isPlaying dont work on AVPlayer. But again it is creating problem – Mann Jun 13 '12 at 11:36
  • isPlaying is just bool variable i used,it checks if current state is playing or paused,wait let me give you in brief. – Dhruv Jun 13 '12 at 11:56
  • please check my complete code. i am progressing slider in other method. – Mann Jun 13 '12 at 11:56
  • Are you using any button to handle play and pause state? – Dhruv Jun 13 '12 at 11:58
  • Yes two buttons , one for [player play ]; and other for [player pause]; – Mann Jun 13 '12 at 11:59
  • I have written complete code in above link in my Edited question before. I guess it will help you to trace problem – Mann Jun 13 '12 at 12:02
  • http://paste.org/50518 check this,i wrote brief code,you can handle the bool variable,that is ,on Sliding pause the track and make isPlaying = False,and while playing make it True,it should work now. :) – Dhruv Jun 13 '12 at 12:05
  • No first of all when i click play button then slider goes to extreem right. i.e 100% played status – Mann Jun 13 '12 at 13:13
  • 1
    @mann : Are you still facing problem,i was searching for something that can be helpful to you. [http://stackoverflow.com/questions/6329706/avplayer-video-seektotime] – Dhruv Jun 15 '12 at 04:36