简体   繁体   English

Swift 4带有AVPlayer的UISlider不会滑动

[英]Swift 4 The UISlider with AVPlayer doesn't slide

I am trying to control the audio with the UISlider , the print() statement prints the correct value and the app doesn't crash, however when I try to grab it and move the slider (the thumb tint), the UISlider doesn't slide but just moves a bit when I try to slide it ( like a tap ). 我正在尝试使用UISlider控制音频, print()语句会打印正确的值,并且应用程序不会崩溃,但是当我尝试抓住它并移动滑块(拇指色)时, UISlider不会滑动,但是当我尝试滑动时会稍微移动(如水龙头)。 When I comment the 6th row the slider response correctly (But of course nothing happens). 当我对第六行发表评论时,滑块会正确响应(但当然不会发生任何反应)。

var playerItem : AVPlayerItem?
var player : AVPlayer?    
@IBAction func adjustSongProgress(_ sender: UISlider) {
    player?.pause()
    let floatTime = Float(CMTimeGetSeconds(player!.currentTime()))
    sliderProgressOutlet.value = floatTime
    print(floatTime)
    player?.play()
}

iOS Slider takes value between 0 to 1. if CMTimeGetSeconds return value outside from 0 to 1 it will not set properly. iOS滑块的值介于0到1之间。如果CMTimeGetSeconds返回的值介于0到1之间,则设置不正确。

therefor you have to convert your Time range to slider range. 因此,您必须将时间范围转换为滑块范围。

for ex : your video/audio length is 120 second and you want to move slider to 30 second.than you can calculate new value using below function. 例如:您的视频/音频长度为120秒,并且您想将滑块移至30秒。然后您可以使用以下功能计算新值。

OldRange = (OldMax - OldMin) OldRange =(OldMax-OldMin)
NewRange = NewMax - NewMin NewRange = NewMax-NewMin

NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin NewValue =((((OldValue-OldMin)* NewRange)/ OldRange)+ NewMin

oldRange = 120 - 0
newRange = 1 - 0
New value = (30-0)*1/120+0 = 0.25

Fixed it by deleting AVPlayer and changing AVPlayerItem to AVAudioPlayer , then putting the song URL into data : ` 通过删除固定它AVPlayer和改变AVPlayerItemAVAudioPlayer ,然后把歌曲网址数据:`

    //DOWNLOADS THE SONG IN THE URL AND PUTS IT IN DATA
    var task: URLSessionTask? = nil

    if let songUrl = songUrl {
        task = URLSession.shared.dataTask(with: songUrl, completionHandler: { data, response, error in
            // SELF.PLAYER IS STRONG PROPERTY
            if let data = data {
                self.playerItem = try? AVAudioPlayer(data: data)
                self.playPause()
                DispatchQueue.main.async {
                    self.sliderProgress()
                }
            }
        })
        task?.resume()`

Then I changed UISlider IBAction Value Changed to Touch Down and Touch Up Inside when I connected it to the ViewController : 然后,当我将UISlider IBAction Value Changed连接到ViewController时,将其Value ChangedTouch DownTouch Up Inside

    // TOUCH DOWN
@IBAction func SliderTouchDown(_ sender: UISlider) {
    playerItem?.pause()
}

//TOUCH UP INSIDE
@IBAction func SliderTouchUpInside(_ sender: UISlider) {
    playerItem?.currentTime = TimeInterval(sliderProgressOutlet.value)
    playerItem?.play()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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