如何在缓冲达到我想要的百分比(20%)时启动视频视图

时间:2016-01-22 10:22:03

标签: android video media-player android-videoview buffering

我想从网址流式传输视频。当缓冲区达到20%时,我想在同一时刻开始播放视频。这是我的代码...

 public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       final VideoView vidView = (VideoView) findViewById(R.id.myVideo);
       MediaController vidControl = new MediaController(this);
       vidControl.setAnchorView(vidView);
       vidView.setMediaController(vidControl);


       String vidAddress = "http://d38e3g3vk5nrvg.cloudfront.net/1452268197713.mp4";

       Uri vidUri = Uri.parse(vidAddress);
       vidView.setVideoURI(vidUri);
       vidView.start();

       vidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {

               mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                   @Override
                   public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
                       Log.d(TAG, "onBufferingUpdate: " + i);
                       if (i >= 20) {
                           Log.d(TAG, "onBufferingUpdate: >= 20");
                           vidView.start();

                       }
                   }
               });

            }
        });
   }

问题是当缓冲达到20%时,视频无法启动...每次达到50%时开始......但是当视频较大时,这太多等待用户了。谢谢你提前!

0 个答案:

没有答案