AVPlayer SeekToTime not working. Starting from beginning everytime

时光总嘲笑我的痴心妄想 提交于 2020-01-25 17:33:30

问题


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.


回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/31711936/avplayer-seektotime-not-working-starting-from-beginning-everytime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!