AVPlayer seektotime与Pangesturerecognizer

时间:2016-05-18 20:24:11

标签: ios swift avplayer uipangesturerecognizer

我正在尝试将搜索结果与Pangesture识别器一起使用。但它没有像预期的那样寻求。

CREATE TABLE [dbo].[tblItemInventoryClassx](
    RecordID uniqueidentifier not null,
    Code char not null,
    BusinessPotential VARCHAR(100) NOT null

    CONSTRAINT PK_tblItemInventoryClass PRIMARY KEY NONCLUSTERED(
        RecordID)
    WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

)   ON [PRIMARY]


INSERT tblItemInventoryClass VALUES(newid(), 'A', 'Maintain substantial inventory')
INSERT tblItemInventoryClass VALUES(newid(), 'B', 'Maintain minimum inventory')
INSERT tblItemInventoryClass VALUES(newid(), 'C', 'Tooling required to order')
INSERT tblItemInventoryClass VALUES(newid(), 'D', 'Sample required to order')
INSERT tblItemInventoryClass VALUES(newid(), 'E', 'Order parts as needed')
INSERT tblItemInventoryClass VALUES(newid(), 'F', 'Low demand, do not quote')
INSERT tblItemInventoryClass VALUES(newid(), 'Z', 'Unclassified')

我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

想想这一行中发生了什么:

let touchDelta = swipeGesture.translationInView(self.view).x / CGFloat(CMTimeGetSeconds(totalTime))

您按时间划分像素(仅在x轴上的平移)。这真的不是一个" delta"或绝对差异。它是各种各样的比例。但它不是一个有任何意义的比例。然后,只需将此比率添加到之前的currentTime,您就可以获得新的currentTime,因此您每秒都会将像素添加到像素中,这不会给出逻辑或有用的数字。

我们需要做的是从手势中取出x轴平移并对其应用比例(这是一个比率),以便获得有用的秒数来推进/后退AVPlayer。 x轴平移以像素为单位,因此我们需要一个刻度来描述每个像素的秒数,并将这两个像素的倍数用于获得我们的秒数。适当的比例是视频中的总秒数与用户在手势中可以移动的像素总数之间的比率。乘以像素时间(秒除以像素)给出了一个以秒为单位的数字。在伪代码中:

scale = totalSeconds / totalPixels
timeDelta = translation * scale
currentTime = oldTime + timeDelta

所以我会像这样重写你的代码:

let totalTime = self.avPlayer.currentItem!.duration
print("time: \(CMTimeGetSeconds(totalTime))")
self.avPlayer.pause()

// BEGIN NEW CODE
let touchDelta = swipeGesture.translationInView(self.view).x
let scale = CGFloat(CMTimeGetSeconds(totalTime)) / self.view.bounds.width
let timeDelta = touchDelta * scale
let currentTime = CMTimeGetSeconds((avPlayer.currentItem?.currentTime())!) + Float64(timeDelta)
// END NEW CODE

print(currentTime)
if currentTime >= 0 && currentTime <= CMTimeGetSeconds(totalTime) {
    let newTime = CMTimeMakeWithSeconds(currentTime, Int32(NSEC_PER_SEC))
    print(newTime)
    self.avPlayer.seekToTime(newTime)
}

答案 1 :(得分:0)

我有同样的问题,然后我创建了UISlider并设置了动作方法,如下所示,

声明AVPlayer为var playerVal = AVPlayer()

@IBAction func sliderAction(sender: UISlider) {

    playerVal.pause()
    displayLink.invalidate()
    let newTime:CMTime = CMTimeMakeWithSeconds(Double(self.getAudioDuration() as! NSNumber) * Double(sender.value), playerVal.currentTime().timescale)
    playerVal.seekToTime(newTime)

    updateTime()

    playerVal.play()

    deepLink()


 }

另一种方法是,

func updateTime() {

        let currentTime = Float(CMTimeGetSeconds(playerItem1.currentTime()))
        let minutes = currentTime/60
        let seconds = currentTime - minutes * 60

        let maxTime =  Float(self.getAudioDuration() as! NSNumber)
        let maxminutes = maxTime / 60
        let maxseconds = maxTime - maxminutes * 60

        startValue.text = NSString(format: "%.2f:%.2f", minutes,seconds) as String
        stopValue.text = NSString(format: "%.2f:%.2f", maxminutes,maxseconds) as String
    }

我使用了CADisplayLink并声明了var displayLink = CADisplayLink(),它使用了继续(自动)播放音频。代码是

func deepLink() {
        displayLink = CADisplayLink(target: self, selector: ("updateSliderProgress"))
        displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
    }

func updateSliderProgress(){
        let progress = Float(CMTimeGetSeconds(playerVal.currentTime())) / Float(self.getAudioDuration() as! NSNumber)
        sliderView.setValue(Float(progress), animated: false)
    }

如果你看到上述答案,你就会明白,希望它有用