Android SeekBar控制视频播放进度(二)——seekTo()不准确

news2024/7/30 15:36:47

Android SeekBar控制视频播放进度二——seekTo不准确

    • 简介
    • seekTo()
    • 视频帧 和 视频关键帧
    • 解决办法
      • 方法一
      • 方法二

简介

上一篇文章中,我们介绍了使用SeekBar控制视频播放,使用过程中发现,对于一些视频,我们拖动SeekBar进度条调节播放进度时,调节到指定位置后,进度条会往回跳,并不会在我们拖动位置继续播放。

网上搜索了解到,VideoView.seekTo()方法的策略决定的。具体看一下seekTo()方法:

seekTo()

  1. 如下是VideoView.seekTo(int msec)的代码实现,我们就是通过调用该方法实现进度调节。通过查看代码,我们知道该方法实际调用的是 MediaPlayer.seekTo(msec);
@Override
public void seekTo(int msec) {
    if (isInPlaybackState()) {
        mMediaPlayer.seekTo(msec);
        mSeekWhenPrepared = 0;
    } else {
        mSeekWhenPrepared = msec;
    }
}
  1. 继续查看 MediaPlayer.seekTo(msec);方法的实现,该方法调用seekTo(long msec, @SeekMode int mode)方法,默认的modeSEEK_PREVIOUS_SYNC
/**
 * Seeks to specified time position.
 * Same as {@link #seekTo(long, int)} with {@code mode = SEEK_PREVIOUS_SYNC}.
 *
 * @param msec the offset in milliseconds from the start to seek to
 * @throws IllegalStateException if the internal player engine has not been
 * initialized
 */
public void seekTo(int msec) throws IllegalStateException {
    seekTo(msec, SEEK_PREVIOUS_SYNC /* mode */);
}

/**
 * Moves the media to specified time position by considering the given mode.
 * <p>
 * When seekTo is finished, the user will be notified via OnSeekComplete supplied by the user.
 * There is at most one active seekTo processed at any time. If there is a to-be-completed
 * seekTo, new seekTo requests will be queued in such a way that only the last request
 * is kept. When current seekTo is completed, the queued request will be processed if
 * that request is different from just-finished seekTo operation, i.e., the requested
 * position or mode is different.
 *
 * @param msec the offset in milliseconds from the start to seek to.
 * When seeking to the given time position, there is no guarantee that the data source
 * has a frame located at the position. When this happens, a frame nearby will be rendered.
 * If msec is negative, time position zero will be used.
 * If msec is larger than duration, duration will be used.
 * @param mode the mode indicating where exactly to seek to.
 * Use {@link #SEEK_PREVIOUS_SYNC} if one wants to seek to a sync frame
 * that has a timestamp earlier than or the same as msec. Use
 * {@link #SEEK_NEXT_SYNC} if one wants to seek to a sync frame
 * that has a timestamp later than or the same as msec. Use
 * {@link #SEEK_CLOSEST_SYNC} if one wants to seek to a sync frame
 * that has a timestamp closest to or the same as msec. Use
 * {@link #SEEK_CLOSEST} if one wants to seek to a frame that may
 * or may not be a sync frame but is closest to or the same as msec.
 * {@link #SEEK_CLOSEST} often has larger performance overhead compared
 * to the other options if there is no sync frame located at msec.
 * @throws IllegalStateException if the internal player engine has not been
 * initialized
 * @throws IllegalArgumentException if the mode is invalid.
 */
public void seekTo(long msec, @SeekMode int mode) {
    if (mode < SEEK_PREVIOUS_SYNC || mode > SEEK_CLOSEST) {
        final String msg = "Illegal seek mode: " + mode;
        throw new IllegalArgumentException(msg);
    }
    // TODO: pass long to native, instead of truncating here.
    if (msec > Integer.MAX_VALUE) {
        Log.w(TAG, "seekTo offset " + msec + " is too large, cap to " + Integer.MAX_VALUE);
        msec = Integer.MAX_VALUE;
    } else if (msec < Integer.MIN_VALUE) {
        Log.w(TAG, "seekTo offset " + msec + " is too small, cap to " + Integer.MIN_VALUE);
        msec = Integer.MIN_VALUE;
    }
    _seekTo(msec, mode);
}
  1. SeekMode 有如下几种模式,

SEEK_PREVIOUS_SYNC: seek到上一个关键帧
SEEK_NEXT_SYNC: seek到下一个关键帧
SEEK_CLOSEST_SYNC: seek到最近的关键帧
SEEK_CLOSEST: seek到最近的帧(不需要是关键帧)

    /**
     * Seek modes used in method seekTo(long, int) to move media position
     * to a specified location.
     *
     * Do not change these mode values without updating their counterparts
     * in include/media/IMediaSource.h!
     */
    /**
     * This mode is used with {@link #seekTo(long, int)} to move media position to
     * a sync (or key) frame associated with a data source that is located
     * right before or at the given time.
     *
     * @see #seekTo(long, int)
     */
    public static final int SEEK_PREVIOUS_SYNC    = 0x00;
    /**
     * This mode is used with {@link #seekTo(long, int)} to move media position to
     * a sync (or key) frame associated with a data source that is located
     * right after or at the given time.
     *
     * @see #seekTo(long, int)
     */
    public static final int SEEK_NEXT_SYNC        = 0x01;
    /**
     * This mode is used with {@link #seekTo(long, int)} to move media position to
     * a sync (or key) frame associated with a data source that is located
     * closest to (in time) or at the given time.
     *
     * @see #seekTo(long, int)
     */
    public static final int SEEK_CLOSEST_SYNC     = 0x02;
    /**
     * This mode is used with {@link #seekTo(long, int)} to move media position to
     * a frame (not necessarily a key frame) associated with a data source that
     * is located closest to or at the given time.
     *
     * @see #seekTo(long, int)
     */
    public static final int SEEK_CLOSEST          = 0x03;

  1. 所以当视频在跳转到相应的 position 位置缺少关键帧的情况下,调用 seekTo 方法是无法在当前位置开始播放。这时会寻找离指定 position 最近的关键帧位置开始播放。
    我们通过seekTo函数调用的实际是默认的mode——SEEK_PREVIOUS_SYNC ,这时会寻找position的上一个关键帧。所以调节视频进度后,视频会往回跳一段,并没有在我们拖动位置继续播放。

视频帧 和 视频关键帧

上面的方法提到了帧和关键帧,下面我们简单的介绍一下两者的关联和区别。我们知道视频是由一帧一帧的图像组成的,而关键帧则是其中的某些帧。理想情况下,我们将所有的普通帧都变为关键帧,那么调节视频播放进度时将不会发生回跳情况。
在这里插入图片描述

解决办法

方法一

根据SeekMode 几种模式的描述,调用时指定modeSEEK_CLOSEST

方法二

对视频源文件进行处理,增加其关键帧数量。使用FFmpeg对视频处理,增加视频的关键帧。

  1. 首先我们通过如下命令查看当前视频中关键帧的数量:
ffprobe -show_frames /Users/Admin/Desktop/test.mp4 >video_log.txt

将视频信息输出到文本文件中,打开video_log.txt文件,搜索关键字pict_type=I查看关键帧。可以看到我们当前视频只有11个关键帧。
在这里插入图片描述
2. 对视频增加关键帧,keyint=30每隔 30 帧设置一个关键帧。命令如下:

ffmpeg.exe -i "/Users/Admin/Desktop/test.mp4" -c:v libx264 -preset superfast -x264opts keyint=30 -acodec copy -f mp4 "/Users/Admin/Desktop/test_out.mp4"

使用步骤1的命令,查看处理后的视频信息。可以看到处理后,我们视频的关键帧数量有99个。
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/463206.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

喜报 | ScanA内容安全云监测获评“新一代信息技术创新产品”

4月20日&#xff0c;在赛迪主办的2023 IT市场年会上&#xff0c;“年度IT市场权威榜单”正式发布。 知道创宇的ScanA内容安全云监测产品荣获“新一代信息技术创新产品”奖项。作为中国IT业界延续时间最长的年度盛会之一&#xff0c;历届IT市场年会公布的IT市场权威榜单已成为市…

备份数据看这里,免费教你苹果手机怎么备份所有数据!

案例&#xff1a;苹果手机怎么算备份成功&#xff1f; 【友友们&#xff0c;手机恢复出厂设置前&#xff0c;怎么样可以备份苹果手机里面的所有数据&#xff1f;】 苹果手机备份数据对于用户来说是非常重要的。在备份数据的同时&#xff0c;还需要学会如何恢复误删的数据。那么…

【微服务笔记22】微服务组件之Sentinel控制台的使用(Sentinel Dashboard)

这篇文章&#xff0c;主要介绍微服务组件之Sentinel控制台的使用&#xff08;Sentinel Dashboard&#xff09;。 目录 一、Sentinel控制台 1.1、下载Dashboard控制台 1.2、搭建测试工程 &#xff08;1&#xff09;引入依赖 &#xff08;2&#xff09;添加配置信息 &#…

微服务生态 -- dubbo -- dubbo3应用级别服务发现(阅读官方文档)

服务发现概述 从 Internet 刚开始兴起&#xff0c;如何动态感知后端服务的地址变化就是一个必须要面对的问题&#xff0c;为此人们定义了 DNS 协议&#xff0c;基于此协议&#xff0c;调用方只需要记住由固定字符串组成的域名&#xff0c;就能轻松完成对后端服务的访问&#x…

236. 二叉树的最近公共祖先【190】

难度等级&#xff1a;中等 上一篇算法&#xff1a; 103. 二叉树的锯齿形层序遍历【191】 力扣此题地址&#xff1a; 236. 二叉树的最近公共祖先 - 力扣&#xff08;Leetcode&#xff09; 1.题目&#xff1a;236. 二叉树的最近公共祖先 给定一个二叉树, 找到该树中两个指定节点…

【MySQL】数据表的增删查改

1、CRUD的解释 C&#xff1a;Create增加 R&#xff1a;Retrieve查询 U&#xff1a;Update更新 D&#xff1a;Deleta删除 2、添加数据 2.1 添加一条记录 添加数据是对表进行添加数据的&#xff0c;表在数据库中&#xff0c;所以还是得先选中数据库&#xff0c;选中数据库还在进行…

STM32F429移植microPython笔记

目录 一、microPython下载。二、安装开发环境。三、编译开发板源码。四、下载验证。 一、microPython下载。 https://micropython.org/download/官网 下载后放在linux中。 解压命令&#xff1a; tar -xvf micropython-1.19.1.tar.xz 二、安装开发环境。 sudo apt-get inst…

MUSIC算法仿真

DOA波达方向估计 DOA&#xff08;Direction Of Arrival&#xff09;波达方向是指通过阵列信号处理来估计来波的方向&#xff0c;这里的信源可能是多个&#xff0c;角度也有多个。DOA技术主要有ARMA谱分析、最大似然法、熵谱分析法和特征分解法&#xff0c;特征分解法主要有MUS…

HTML+CSS+JS 学习笔记(四)———jQuery

&#x1f331;博客主页&#xff1a;大寄一场. &#x1f331;系列专栏&#xff1a;前端 &#x1f331;往期回顾&#xff1a; &#x1f618;博客制作不易欢迎各位&#x1f44d;点赞⭐收藏➕关注​​ 目录 jQuery 基础 jQuery 概述 下载与配置jQuery 2. 配置jQuery jQuery 选…

数据库管理-第七十期 自己?自己(20230425)

数据库管理 2023-04-25 第七十期 自己&#xff1f;自己1 自己吓自己2 自己坑自己3 自己挺自己4 自己懵自己总结 第七十期 自己&#xff1f;自己 来到70了&#xff0c;最近有点卷&#xff0c;写的稍微多了些。 吐槽一下五一调休&#xff0c;周末砍一天&#xff0c;连6天&#x…

重学Java第一篇——数组

本片博客主要讲述了以下内容&#xff1a; 1、 一维数组和二维数组的创建和初始化方式&#xff1b; 2、数组的遍历和赋值 3、java.util.Arrays的常用方法 4、数组在内存中的分布&#xff08;图示&#xff09; 创建数组和初始化 type[] arr_name;//方式一 type arr_name[];//方式…

一家传统制造企业的上云之旅,怎样成为了数字化转型典范?

众所周知&#xff0c;中国是一个制造业大国。在想要上云以及正在上云的企业当中&#xff0c;传统制造企业也占据了相当大的比例。 那么这类企业在实施数字化转型的时候&#xff0c;应该如何着手&#xff1f;我们不妨来看看一家传统制造企业的现身说法。 国茂股份的数字化转型诉…

云原生-如何部署k8s集群与部署sms集群

阿里云开通三台云服务器实例&#xff0c;&#xff08;同一个vpc下&#xff09;&#xff0c;配置安全组入规则&#xff0c;加入80端口 ssh登录三台云服务器 在三台云服务器上部署容器环境&#xff08;安装docker&#xff09;&#xff08;https://www.yuque.com/leifengyang/oncl…

Springboot Mybatis使用pageHelper实现分页查询

以下介绍实战中数据库框架使用的是mybatis&#xff0c;对整合mybatis此处不做介绍。 使用pageHelper实现分页查询其实非常简单&#xff0c;共两步&#xff1a; 一、导入依赖&#xff1b; 二、添加配置&#xff1b; 那么开始&#xff0c; 第一步&#xff1a; pom.xml添加依…

工具链和其他-超级好用的web调试工具whistle

目录 whistle介绍 整体结构 能力 规则 6个使用场景示例 1.修改Host 2.代理 3.替换文件&#xff08;线上报错时&#xff09; 4.替换UA 5.远程调试 6.JS注入 互动 whistle介绍 整体结构 安装&#xff1a; npm install whistle -g cli&#xff1a;whistle help 启动…

前端系列第10集-实战篇

用户体验&#xff1a;性能&#xff0c;交互方式&#xff0c;骨架屏&#xff0c;反馈&#xff0c;需求分析等 组件库&#xff1a;通用表单&#xff0c;表格&#xff0c;弹窗&#xff0c;组件库设计&#xff0c;表单等 项目质量&#xff1a;单元测试&#xff0c;规范&#xff0c;…

mac十大必备软件排行榜 mac垃圾清理软件哪个好

刚拿到全新的mac电脑却不知道该怎么使用&#xff1f;首先应该装什么软件呢&#xff1f;如果你有同样的疑惑&#xff0c;今天这篇文章一定不要错过。接下来小编为大家介绍mac十大必备软件排行榜&#xff0c;以及mac垃圾清理软件哪个好。 一、mac十大必备软件排行榜 1.CleanMyM…

权限提升:AT || SC || PS 提权.(本地权限提升)

权限提升&#xff1a;AT || SC || PS 提权 权限提升简称提权&#xff0c;由于操作系统都是多用户操作系统&#xff0c;用户之间都有权限控制&#xff0c;比如通过 Web 漏洞拿到的是 Web 进程的权限&#xff0c;往往 Web 服务都是以一个权限很低的账号启动的&#xff0c;因此通…

电能计量管理系统在煤矿上的应用

摘要&#xff1a;随着煤矿供电系统管、控一体化的发展需要&#xff0c;本文提出了一种基于矿井光纤网络构成的煤矿电参数计量系统&#xff0c;该系统具有实现变电所各类开关、动力设备的用电高精度计量&#xff1b;远程实时监测各路电参数&#xff1b;远程抄表&#xff1b;远程…

Aspose.Pdf使用教程:在PDF文件中添加水印

Aspose.PDF 是一款高级PDF处理API&#xff0c;可以在跨平台应用程序中轻松生成&#xff0c;修改&#xff0c;转换&#xff0c;呈现&#xff0c;保护和打印文档。无需使用Adobe Acrobat。此外&#xff0c;API提供压缩选项&#xff0c;表创建和处理&#xff0c;图形和图像功能&am…