zoukankan      html  css  js  c++  java
  • 监听VideoView的缓冲区状态和播放时间两种方法。

    最近自己开发的应用需要个视频演示功能,就想到了用VideoView来播放百度云上存放的视频,但是Android提供的MediaController满足不了需求。所以就考虑自己写个视频控制器,看了下VideoView的API发现有getBufferPercentage()和getCurrentPosition ()来获取当前缓冲区的大小和当前播放位置,没发现有监听缓冲区的方法。只能自己写个定时器来监听了。

     1     private Handler handler = new Handler();
     2     private Runnable run =  new Runnable() {
     3             int buffer, currentPosition, duration;
     4             public void run() {
     5                 // 获得当前播放时间和当前视频的长度
     6                 currentPosition = videoView.getCurrentPosition();
     7                 duration = videoView.getDuration(); 
     8                 int time = ((currentPosition * 100) / duration);
     9                 // 设置进度条的主要进度,表示当前的播放时间
    10                 seekBar.setProgress(time);
    11                 // 设置进度条的次要进度,表示视频的缓冲进度
    12                 buffer = videoView.getBufferPercentage();
    13                 seekBar.setSecondaryProgress(percent);
    14                 
    15                 handler.postDelayed(run, 1000);
    16             }
    17         };

    设置为每1000毫秒获取一次缓冲区和当前播放位置的状态。因为没有调用start(),所以实际handler把run加到了消息队列里等主线程来执行,直接在run里面更新UI就可以。

    在videoView.start();后面加入handler.post(run);就可以启用定时器了。或者在设置了videoView.setOnPreparedListener(this);后放进onPrepared()方法里,等视频文件加载完毕自动启动。

    特别注意一下记得在视频播放器退出时用handler.removeCallbacks(run);来销毁线程。重写Activity的onDestroy()方法

    1   @Override    
    2   protected void onDestroy() {    
    3     handler.removeCallbacks(run);    
    4     super.onDestroy();    
    5   }

    后来查看资料的时候发现有另外一种方法,只要拿到MediaPlayer对象再设置setOnBufferingUpdateListener()监听器就能监听缓冲区的状态了。

     1 public class TestActivity extends Activity implements OnPreparedListener{
     2     private TextView videoback;
     3     private VideoView videoView;
     4 
     5     @Override
     6     protected void onCreate(Bundle savedInstanceState) {
     7 
     8         super.onCreate(savedInstanceState);
     9         setContentView(R.layout.test);
    10         seekBar = (SeekBar) findViewById(R.id.test_seekbar);
    11         videoView = (VideoView) findViewById(R.id.test_video);
    12         videoView.setOnPreparedListener(this);
    13     }
    14 
    15     public void onPrepared(MediaPlayer mp) {
    16         mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
    17             int currentPosition, duration;
    18             public void onBufferingUpdate(MediaPlayer mp, int percent) {
    19                 // 获得当前播放时间和当前视频的长度
    20                 currentPosition = videoView.getCurrentPosition();
    21                 duration = videoView.getDuration(); 
    22                 int time = ((currentPosition * 100) / duration);
    23                 // 设置进度条的主要进度,表示当前的播放时间
    24                 SeekBar seekBar = new SeekBar(EsActivity.this);
    25                 seekBar.setProgress(time);
    26                 // 设置进度条的次要进度,表示视频的缓冲进度
    27                 seekBar.setSecondaryProgress(percent);
    28             }
    29         });
    30     }
    31 }
  • 相关阅读:
    康复计划
    Leetcode 08.02 迷路的机器人 缓存加回溯
    Leetcode 38 外观数列
    Leetcode 801 使序列递增的最小交换次数
    Leetcode 1143 最长公共子序列
    Leetcode 11 盛水最多的容器 贪心算法
    Leetcode 1186 删除一次得到子数组最大和
    Leetcode 300 最长上升子序列
    Leetcode95 不同的二叉搜索树II 精致的分治
    Leetcode 1367 二叉树中的列表 DFS
  • 原文地址:https://www.cnblogs.com/zhengxt/p/3563978.html
Copyright © 2011-2022 走看看