设置滑块值以在AVPlayer中设置SeekToTime [英] Setting Slider Value to Set SeekToTime in AVPlayer

查看:92
本文介绍了设置滑块值以在AVPlayer中设置SeekToTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用播放器库,即使用AVPlayer& AVFoundation,这对我来说很安静。我成功地设法播放了视频并添加了一个滑块。我将滑块的min设置为0,将max设置为视频的持续时间..



此时,为了将滑块连接到当前播放时间,我使用了这个答案。我设置了一个协议并使用了 addPeriodicTimeObserverForInterval ,因此滑块链接到currentTime并随着视频的成功移动而移动。



现在,这是我的问题。我想反过来做 - 当滑块移动时,设置视频的当前时间(向前,向后)。



我做了一些研究,我发现它是 seekToTime 我应链接 slider.value sender.value 在我的情况下)但是,我无法理解如何将它应用于播放器。如何让滑块控制当前时间?






我不确定我是否正确,但至于我明白了,第一步应该是:

  protocol UserSetsTheTimeDelegate 
{
func userSetsTheTime(result:Int )
}

类ViewController:UIViewController,PlayerDelegate {
...

func sliderValueDidChange(发送者:UISlider!){
打印(value - \(sender.value))
self.delegate?.userSetsTheTime(sender.value)
}
}

并补充说,我想我应该在Player.swift中添加 UserSetsTheTimeDelegate ,但我感到困惑这里。






更新



<我意识到我可以通过使用ViewController中的实例并在Player类中添加方法将数据发送到Player.swift。这是第一步。但现在,因为currentTime正在快速更新(修改滑块),它不会让我操纵滑块值。

解决方案

从UX的角度来看,最好是在播放器之后调用 seek()方法滑块上的 touchUp ,而不是值发生变化时。



正如您所注意到的,您对 sliderValueDidChange 的体验不佳。



相反,尝试在滑块上的UIControlEvents.TouchUpInside(例如)上添加目标/操作,然后只搜索。经验应该会更好。

 

var slider:UISlider! {
didSet {
slider.addTarget(self,action:Selector(touchUp:),forControlEvents:[UIControlEvents.TouchUpInside])
}
}

func touchUp(发送者:AnyObject?){
// player.seek(slider.currentValue)
}


I am using Player library, that is using AVPlayer & AVFoundation, which is quiet convenient for my case. I successfully managed to play the video and add a slider. I set the slider's min to 0 and max to duration of the video..

At this point, in order to connect slider to current playtime, I used this answer, on StackOverflow. I setup a protocol and used addPeriodicTimeObserverForInterval, so slider is linked to the currentTime and moving as video moves along successfully.

Now, here is my problem. I want to do the reverse and make - when slider moves, set currentTime of the video (forward,backward).

I made some research and I found out that it's seekToTime that I should link the slider.value (sender.value in my case) but, I couldn't understand how to apply it to Player. How can I make the slider to control the currentTime?


I am not sure if I am right, but as far as I understand, the first step should be:

protocol UserSetsTheTimeDelegate
{
  func userSetsTheTime(result: Int)
}

class ViewController: UIViewController, PlayerDelegate {
       ...

  func sliderValueDidChange(sender:UISlider!) {
     print("value--\(sender.value)")
     self.delegate?.userSetsTheTime(sender.value)
  }
}

And add, I think I should add the UserSetsTheTimeDelegate in Player.swift, but I got confused here.


Update:

I realised that I can send the data to the Player.swift by using an instance in ViewController and adding a method in Player class. This is the first step. But now, as currentTime is getting updated (with modifying the slider) so quickly, it doesn't let me manipulate the slider value.

解决方案

It is preferable from an UX point of view to call the seek() method on your Player after a touchUp on your slider, rather than whenever the value changes.

As you noticed, you had poor experience with the sliderValueDidChange.

Instead, try by adding a target/action on UIControlEvents.TouchUpInside (for instance) on your slider, then only seek. The experience should be better.


    var slider : UISlider! {
        didSet {
          slider.addTarget(self, action: Selector("touchUp:"), forControlEvents: [UIControlEvents.TouchUpInside])
        }
    }

    func touchUp(sender:AnyObject?) {
        // player.seek(slider.currentValue)
    }

这篇关于设置滑块值以在AVPlayer中设置SeekToTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆