この機能は私のアプリにとって本当に重要なことであり、実際にはこの機能が役立つと思います。基本的には、ビデオプレーヤーにUIPanGestureRecognizer
を追加して、ユーザーがジェスチャーでビデオを早送り/巻き戻しできるようにしたいと思います。AVPlayerを使ったパンのジェスチャー
AppleにはSwift 3を使用するサンプルコードがあり、ビデオプレーヤー全体が既に作成されています。欠けているのはUIPanGestureRecognizer
です。これは、リンクです:https://developer.apple.com/library/content/samplecode/AVFoundationSimplePlayer-iOS/Introduction/Intro.html#//apple_ref/doc/uid/TP40016103viewWillAppear
で
ので、同様に、私はジェスチャーを追加しました:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))
view.addGestureRecognizer(panGesture)
これは多少働いているコードです。それは現在スクラブしています。
問題は、私がパンジェスチャーを開始するたびに、ビデオが途中までスキップしてそこから開始することです。ビデオの現在の位置からの早送り/巻き戻しの代わりに、パンジェスチャーはビデオを途中までスキップして、早送り/巻き戻しを可能にします。
func handlePanGesture(sender: UIPanGestureRecognizer) {
switch sender.state {
case .began, .changed:
let translation = sender.translation(in: self.view)
var horizontalTranslation = Float(translation.x)
let durationInSeconds = Float(CMTimeGetSeconds(player.currentItem!.asset.duration))
// Using 275 as the limit for delta along x
let translationLimit: Float = 275
let minTranslation: Float = -1 * translationLimit
let maxTranslation: Float = translationLimit
if horizontalTranslation > maxTranslation {
horizontalTranslation = maxTranslation
}
if horizontalTranslation < minTranslation {
horizontalTranslation = minTranslation
}
let timeToSeekTo = normalize(delta: horizontalTranslation , minDelta: minTranslation, maxDelta: maxTranslation, minDuration: 0, maxDuration: durationInSeconds)
print("horizontal translation \(horizontalTranslation) \n timeToSeekTo: \(timeToSeekTo)")
let cmTime = CMTimeMakeWithSeconds(Float64(timeToSeekTo), self.player.currentTime().timescale)
player.seek(to: cmTime)
default:
print("default")
}
}
func normalize(delta: Float, minDelta: Float, maxDelta: Float, minDuration: Float, maxDuration: Float) -> Float {
let result = ((delta - minDelta) * (maxDuration - minDuration)/(maxDelta - minDelta) + minDuration)
return result
}
すべてのヘルプは素晴らしいことだ - ありがとう!