How to play a video using videoview controlled by a seekBar or Progress bar?

安稳与你 提交于 2019-12-13 05:29:48

问题


After searching for many hours , i am asking this, any related answers are welcomed . .

I'm having a video in my R.raw folder and i am playing it in two videoview of same duration simultaneously , here is my code ,

     sb = (SeekBar)findViewById(R.id.control_seekbar);

     mVideoView1 = (VideoView) findViewById(R.id.surface_view1);
     mVideoView1.setVideoPath("android.resource://" + getPackageName() +"/"+R.raw.play1);              
     mVideoView1.start();

     mVideoView2 = (VideoView) findViewById(R.id.surface_view2);
     mVideoView2.setVideoPath("android.resource://" + getPackageName() +"/"+R.raw.play2);              
     mVideoView2.start();

     v1 = mVideoView1.getDuration();
     v2 = mVideoView2.getDuration();

     mVideoView1.setOnPreparedListener(new OnPreparedListener() 
     {
        @Override
        public void onPrepared(MediaPlayer arg0) 
        {
            sb.setMax(mVideoView1.getDuration()+3);
            sb.postDelayed(onEverySecond, 60);
        }
     });

     sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
     {  
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) 
        {
            mVideoView1.seekTo(sb.getProgress());
            mVideoView2.seekTo(sb.getProgress());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) 
        {
            mVideoView1.seekTo(sb.getProgress());
            mVideoView2.seekTo(sb.getProgress());
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) 
        {
            mVideoView1.seekTo(progress);
            mVideoView2.seekTo(progress);
        }

    });

   }

   private Runnable onEverySecond=new Runnable() 
   {
        @Override
        public void run() 
        {
            if(sb != null) 
            {
                sb.setProgress(mVideoView1.getCurrentPosition());
            }

            if(mVideoView1.isPlaying()) 
            {
                sb.postDelayed(onEverySecond, 1000);
                if(mVideoView1.getDuration() <= sb.getProgress())
                {
                    mVideoView1.seekTo(0);
                    mVideoView2.seekTo(0);
                }
            }
        }
    };

and i could drag the seekbar to the new position and it is playing from the new position correctly .

But i dont want to play the video initially . It should be in pause() and as i drag the seekbar, the videoview should update to the respective position .

When i change the mVideoView.start() to pause() , i could see only a blank black screen and no update for the seekbar position change in the videoview .

in what way i could achieve it .


回答1:


If you dont want to play the video initially then you need not call start in onCreate() Call it when play button is clicked.

Hope this helps you.




回答2:


You have to put a media controller in your videoview.

MediaController mediaController = new MediaController(context);
myVideoView.setMediaController(mediaController).

Now when you click on your video the play, pause buttons will show.



来源:https://stackoverflow.com/questions/13834728/how-to-play-a-video-using-videoview-controlled-by-a-seekbar-or-progress-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!