- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试将 seektotime 与 Pangesture 识别器一起使用。但它没有按预期进行搜索。
let totalTime = self.avPlayer.currentItem!.duration
print("time: \(CMTimeGetSeconds(totalTime))")
self.avPlayer.pause()
let touchDelta = swipeGesture.translationInView(self.view).x / CGFloat(CMTimeGetSeconds(totalTime))
let currentTime = CMTimeGetSeconds((avPlayer.currentItem?.currentTime())!) + Float64(touchDelta)
print(currentTime)
if currentTime >= 0 && currentTime <= CMTimeGetSeconds(totalTime) {
let newTime = CMTimeMakeWithSeconds(currentTime, Int32(NSEC_PER_SEC))
print(newTime)
self.avPlayer.seekToTime(newTime)
}
我在这里做错了什么?
最佳答案
想想这里这一行发生了什么:
let touchDelta = swipeGesture.translationInView(self.view).x / CGFloat(CMTimeGetSeconds(totalTime))
您将像素(仅在 x 轴上的平移)除以时间。这确实不是“增量”或绝对差异。这是某种比率。但这不是一个有任何意义的比率。然后,您只需将此比率添加到先前的 currentTime
即可获得新的 currentTime
,因此您将每秒像素添加到像素,这不符合逻辑或有用的数字。
我们需要做的是从手势中获取 x 轴平移并对其应用 scale(这是一个比率)以获得有用的秒数来前进/后退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)
}
关于ios - AVPlayer seektotime with Pangesturerecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37309445/
我有一个播放广播电台的 AVPlayer,并有一个我制作的快退按钮。当您倒带时,该方法被调用并且在大约 80% 的时间内工作,成功地将 AVPLayer 的当前时间设置为 X 秒。然而,另外 20%
我正在使用 AVPlayer 远程传输视频。每当我擦洗和使用 seekToTime 时,它都会工作,并且视频会从该位置播放,但是如果我按顺序执行此操作足够多次,则 seekToTime 的完成处理
这是我的代码。我使用 UISlider 作为搜索栏,因此当 UISlider 值更改时 - (IBAction)sliderValueChanged:(UISlider *)sender 被调用。 -
我正在开发视频应用程序并在 AVPlayer 上播放视频。但问题是当我在 UISlider 的帮助下寻找特定时间的视频时。播放器 seekToTime 方法在 integer 中获取时间值,但我希望它
如何让 AVPlayerLayer 在 seekToTime 方法中显示准确的视频帧。 现在 [AVPlayer seekToTime:CMTimeMakeWithSeconds(CMTimeGetS
我在一个应用程序中工作,我可以使用 AVPlayer 实例毫无问题地播放流式广播的 HLS m3u8 播放列表(仅音频)。使用 Charles,我可以看到播放列表如何以正常速度正确更新(每 9-10
我在我的应用程序中使用了 seekToTime 并且它工作正常。但我想了解更多有关它的信息。也就是说,如果我现在有一个 1 分钟的流媒体视频文件,我想从第 15 秒到第 45 秒播放它(前 15 秒和
我正在尝试将 seektotime 与 Pangesture 识别器一起使用。但它没有按预期进行搜索。 let totalTime = self.avPlayer.currentItem!.durat
我在使用 AVPlayer.seekToTime 进行搜索时遇到问题,我有时间索引,我想在这样的 scrollViewDidScroll 方法中搜索: func scrollViewDidScroll
我正在使用 AVPlayer 来使用 slider 和一些按钮播放我的视频。这是我使用按钮向前和向后移动的方法。 -(IBAction)MoveForward { //int value =
我有一个正在播放 HLS 视频流的 AVPlayer。我的用户界面提供了一排按钮,一个用于视频中的每个“章节”(按钮标记为“1”、“2”、“3”)。该应用程序从服务器下载一些元数据,其中包含以秒表示的
- (void)viewDidLoad { [super viewDidLoad]; self.slider.maximumValue = 30; } - (void)slide:(U
我在使用 UISlider 和 AVPlayer 清理方法时遇到问题。每次调用该方法时,播放器都会从 0 重新启动。我尝试调试,似乎 slider 值是正确的,但当我单步执行时,它会设置为 0,因此播
我正在使用 AVPlayer 流式传输音频。它运作良好。但现在我需要制作一个 slider 来向前和向后移动音频,就像典型的音乐播放器一样。我使用了 seekToTime 函数,它非常适合本地音频文件
我正在制作一个使用 AVPlayer 的应用程序。在我的一个观点中,我有一个 UISlider,用户应该能够使用它向前和向后滑动。我在让 seekToTime 按我的意愿工作时遇到了一些问题。当我尝试
我试图让 avPlayer 在选择跳回按钮时重新启动当前歌曲,如果它是在进入歌曲大约 5 秒后,并根据 slider 移动到的位置跳转到时间。这是我为这些功能编写的代码: @IBAction func
我正在开发一个使用HTTP Live Streaming 技术播放视频的应用程序。 为此,我使用 AVPlayer 初始化如下: [[AVPlayer alloc] initWithURL:[NSUR
我或多或少使用了这里的代码:AVPlayer Video SeekToTime但是,当我尝试滚动时,它似乎锁定到某些时间点(基本上是第二个时间标记上的每一帧),所以当我擦洗擦洗器时,它会在我手指所在的
我有以下代码: AVPlayerItem *currentItem = [AVPlayerItem playerItemWithURL:soundURL]; [self.audioPlayer rep
我知道有很多帖子,但没有一个有帮助。我的设备上有不同 FPS 的视频(如果有关系的话)。我正在计算准确的 CMTime(如果我们将它们转换为秒,它们将为 10.33333、10.4444 等)。我正在
我是一名优秀的程序员,十分优秀!