Search code examples
androidandroid-videoviewbuffering

How can we play/buffer only few minutes of a video in Android?


I need to play first 2 mins of a video. Using onBufferingUpdate() i get the percentage that is buffered. But by the time onPrepared is called i get buffered % as 40. Which is more than 2 mins of a video. (Considering i have a video of 30 mins)

Is there any way i can play/buffer video for only 2 mins ?

Thanks.


Solution

  • The buffer size is pre-set into the firmware. All you can do is keep tabs on how much the buffer is filled, and even that is just on a percentage basis. The size is set in frameworks/base/media/libstagefright/include/NuCachedSource2.h

    check out this bug report on code.google.com.


    Edit: You can get current position of the video by usingmVideoView.getCurrentPosition(); which will return you the current position in milliseconds and now you will have to use a AsynTask

    private class myAsync extends AsyncTask<Void, Integer, Void>
    {
        @Override
        protected Void doInBackground(Void... params) {
            mVideoView.start();            
            do {            
                try {
                    if(mVideoView.getCurrentPosition() == TimeUnit.MINUTES.toMillis(yourMinutes)){
                        mVideoView.stop();
                    }
                } catch (Exception e) {
                }
            } while (mVideoView.getCurrentPosition() != mVideoView.getDuration());
            return null;
        }
    

    You can start the AsyncTask by using new myAsync().execute(); statement.