Android 修复ijkPlayer进行m3u8 hls流播放时seek进度条拖动不准确的问题

项目中使用的播放器是ijkPlayer,发现播放切片特点的hls流(m3u8格式的视频)拖动seekBar的时候会莫名的跳转或者seek不到准确的位置,发现网友也遇到了同样的问题,ijk的开发者也说明了是因为UI层的问题导致的,需要自己排查。涉及到该问题的链接:

既然开发者都说了,那么就老实分析代码吧。因为项目中用到的MediaController继承自Android系统的MediaController,所以还得看看源码,分析得出系统中实现是将seek的listener监听器放在onProgressChanged这个方法中,这也是为什么我们断断续续拖动的时候播放器也会播放,知道这点就够了,把onProgressChanged中的mPlayer.seekTo((int) newposition);放到onStopTrackingTouch方法中。

执行顺序是:

onStartTrackingTouch(执行一次) —> onProgressChanged(拖动就会不停的执行) —> onStopTrackingTouch(停止后最后执行一次)

实现代码如下:

public class CustomMediaController extends MediaController implements ICustomMediaController {
    // ....................代码省略.............................

  // There are two scenarios that can trigger the seekbar listener to trigger:
  //
  // The first is the user using the touchpad to adjust the posititon of the
  // seekbar's thumb. In this case onStartTrackingTouch is called followed by
  // a number of onProgressChanged notifications, concluded by onStopTrackingTouch.
  // We're setting the field "mDragging" to true for the duration of the dragging
  // session to avoid jumps in the position in case of ongoing playback.
  //
  // The second scenario involves the user operating the scroll ball, in this
  // case there WON'T BE onStartTrackingTouch/onStopTrackingTouch notifications,
  // we will simply apply the updated position without suspending regular updates.
  private OnSeekBarChangeListener mSeekListener=new OnSeekBarChangeListener(){
    long newposition;

    public void onStartTrackingTouch(SeekBar bar){
      show(3600000);
      mDragging=true;
      if(seekerBarDraggingListener!=null)
        seekerBarDraggingListener.getCurrentDraggingstatus(mDragging);

      // By removing these pending progress messages we make sure
      // that a) we won't update the progress while the user adjusts
      // the seekbar and b) once the user is done dragging the thumb
      // we will post one of these messages to the queue again and
      // this ensures that there will be exactly one message queued up.
      mHandler.removeMessages(SHOW_PROGRESS);
    }

    public void onProgressChanged(SeekBar bar,int progress,boolean fromuser){
      if(!fromuser){
        // We're not interested in programmatically generated changes to
        // the progress bar's position.
        return;
      }
      long duration=mPlayer.getDuration();
      newposition=(duration*progress)/1000L;
      // 系统原来的实现是在progress改变的时候时刻都在进行videoplayer的seek
      //这会导致seek m3u8切片文件的时候拖动seek时不准确,所以需要在拖动完成后才进行播放器的seekTo()
      //                mPlayer.seekTo((int) newposition);
      if(mCurrentTime!=null)
        mCurrentTime.setText(stringForTime((int)newposition));
    }

    public void onStopTrackingTouch(SeekBar bar){
      mDragging=false;
      mPlayer.seekTo((int)newposition);
      if(seekerBarDraggingListener!=null)
        seekerBarDraggingListener.getCurrentDraggingstatus(mDragging);
      setProgress();
      updatePausePlay();
      if(isntNeedStayShowAfterDrag){
        show(sDefaultTimeout);
        // Ensure that progress is properly updated in the future,
        // the call to show() does not guarantee this because it is a
        // no-op if we are already showing.
        mHandler.sendEmptyMessage(SHOW_PROGRESS);
      }
    }
  };

  // ....................代码省略.............................
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在Uniapp项目中引入dplayer.js和hls.js可以实现解析和播放m3u8直播视频文件,具体步骤如下: 1. 首先,将dplayer.js和hls.js的相关文件引入到Uniapp项目中。可以通过npm安装这些库文件,也可以将它们下载到本地然后引入。 2. 在需要使用dplayer的页面中,使用uni.require将dplayer.js引入进来。例如,可以在页面的script标签中使用以下代码: ```javascript import DPlayer from '@/path/to/dplayer.js'; ``` 注意,@/path/to/指的是dplayer.js文件所在的路径。 3. 在页面中创建一个容器元素,用于渲染播放器。可以在template标签中添加一个div元素,例如: ```html <template> <div id="dplayer-container"></div> </template> ``` 这里给div元素设置一个id,以便之后使用。 4. 在页面的mounted钩子函数中,创建并初始化DPlayer实例。可以在mounted函数中添加以下代码: ```javascript mounted() { const container = document.getElementById('dplayer-container'); const options = { // 设置DPlayer的配置选项 }; const player = new DPlayer(options); player.init(); // 其他相关配置和操作 }, ``` 这里需要根据具体项目的需求,设置DPlayer的相关配置选项,比如视频的url、控制栏样式等。可以参考DPlayer的官方文档进行设置。 5. 使用hls.js解析m3u8直播视频文件。在设置DPlayer的配置选项,可以通过设置type为'hls'来启用hls.js的解析功能。例如: ```javascript const options = { // 其他配置选项 type: 'hls', url: 'http://example.com/video.m3u8', }; ``` 这里的url需要替换为实际的m3u8直播视频文件的地址。 通过以上步骤,在Uniapp项目中成功引入dplayer.js和hls.js,并使用DPlayer来解析和播放m3u8直播视频文件。修改相应的配置选项,可以根据需求进行定制化操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值