AVPlayer SeekToTime not working. Starting from beg

2019-08-31 07:35发布

I'm having trouble with UISlider and AVPlayer scrubbing method. Every time the method is invoked the player restarts from 0. I tried debugging and seems that the slider value is right but when I Step Over, it's set 0, thus the player restarts. This is what I've tried:

    var desiredTime = CMTimeMake(Int64(self.progressSlider.value), 1)

    AudioManager.sharedInstance.audioPlayer.seekToTime(desiredTime)

I've also tried looking at similar questions and found that it was the same when I tried their solutions.

I appreciate your help.

2条回答
在下西门庆
2楼-- · 2019-08-31 07:59

The use of the method wasn't right. You can look up in the Library.

var desiredTime = CMTimeMake(self.progressSlider.value *1000, 1000);
AudioManager.sharedInstance.audioPlayer.seekToTime(desiredTime)
查看更多
叼着烟拽天下
3楼-- · 2019-08-31 08:08

As I indicated in my comment, you need to set the sliders minimum value to 0 and its maximum value to the duration of the asset being played.

To make the slider move as the player plays, add a periodic observer to the player. For example:

CMTime interval = CMTimeMakeWithSeconds(.25,1000); [_player addPeriodicTimeObserverForInterval:interval queue:NULL usingBlock:
    (CMTime time)){ NSTimeInterval t = CMTimeGetSeconds(time);
         mySlider.value = t; }];

(You may need a weak reference to self to access mySlider.) The time parameter to the block is the player's current time.

When the user slides, use seekToTime to set the players position.

One caveat: although this works, the increments of the slider will show as little jumps of the thumb. I have not figured out a simple way to make the slider move continuously - probably worth a question of its own.

查看更多
登录 后发表回答