uni-app ios video seek功能限制为整数
uni-app ios video seek功能限制为整数
操作步骤:
- 一个video组件 设置seek播放的位置
预期结果:
- 设置 1.2s 调到 1.2s
实际结果:
- 设置1.2s 调到 1s
bug描述:
- ios video seek只能是整数 比如 设置 1.2 结果只能调到 1s的位置 安卓可以但是ios不行
5 回复
说错了。 ios支持 安卓不支持 安卓设置1.2变成了1s
我们也遇见类似问题了,什么时候能解决,搜索出来很多类似问题,快1年多了。
求解
什么时候能解决这个问题
在 UniApp 中,iOS 平台的 video
组件的 seek
功能确实存在一些限制,特别是在 iOS 系统中,seek
的时间点通常会被限制为整数秒。这是因为 iOS 系统的 AVPlayer
在处理视频跳转时,默认会将跳转时间点四舍五入到最近的整数秒。
解决方案
-
使用
timeupdate
事件: 你可以监听timeupdate
事件来获取当前播放时间,并在需要跳转时,将目标时间四舍五入到最近的整数秒。<template> <view> <video id="myVideo" src="your_video_url" @timeupdate="onTimeUpdate"></video> <button @click="seekTo(10.5)">Seek to 10.5s</button> </view> </template> <script> export default { methods: { onTimeUpdate(event) { this.currentTime = event.detail.currentTime; }, seekTo(time) { const roundedTime = Math.round(time); const videoContext = uni.createVideoContext('myVideo'); videoContext.seek(roundedTime); } } } </script>
-
使用
playbackRate
控制播放速度: 如果你需要更精确的控制,可以通过调整playbackRate
来实现。例如,你可以将播放速度设置为 2 倍速,然后在接近目标时间时再恢复正常速度。<template> <view> <video id="myVideo" src="your_video_url"></video> <button @click="seekTo(10.5)">Seek to 10.5s</button> </view> </template> <script> export default { methods: { seekTo(time) { const videoContext = uni.createVideoContext('myVideo'); videoContext.playbackRate(2); // 设置播放速度为2倍速 videoContext.seek(Math.floor(time)); // 跳转到整数秒 setTimeout(() => { videoContext.playbackRate(1); // 恢复正常播放速度 }, (time - Math.floor(time)) * 1000); // 等待剩余时间 } } } </script>
-
使用
currentTime
属性: 你可以通过currentTime
属性来手动设置视频的当前时间,但同样会受到 iOS 系统的限制。<template> <view> <video id="myVideo" src="your_video_url"></video> <button @click="seekTo(10.5)">Seek to 10.5s</button> </view> </template> <script> export default { methods: { seekTo(time) { const videoContext = uni.createVideoContext('myVideo'); videoContext.currentTime = Math.round(time); } } } </script>