Can a videoview play a video stored on internal storage?

前端 未结 6 1492
一向
一向 2020-11-29 04:12

I\'m trying to provide my users with the ability to use either external or internal storage. I\'m displaying both images and videos (of a scientific nature). When storing th

6条回答
  •  死守一世寂寞
    2020-11-29 04:37

    For detail check this tutorial

    public class AndroidVideoViewExample extends Activity {
    
        private VideoView myVideoView;
        private int position = 0;
        private ProgressDialog progressDialog;
        private MediaController mediaControls;
    
        @Override
        protected void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // set the main layout of the activity
            setContentView(R.layout.activity_main);
    
            //set the media controller buttons
            if (mediaControls == null) {
                mediaControls = new MediaController(AndroidVideoViewExample.this);
            }
    
            //initialize the VideoView
            myVideoView = (VideoView) findViewById(R.id.video_view);
    
            // create a progress bar while the video file is loading
            progressDialog = new ProgressDialog(AndroidVideoViewExample.this);
            // set a title for the progress bar
            progressDialog.setTitle("JavaCodeGeeks Android Video View Example");
            // set a message for the progress bar
            progressDialog.setMessage("Loading...");
            //set the progress bar not cancelable on users' touch
            progressDialog.setCancelable(false);
            // show the progress bar
            progressDialog.show();
    
            try {
                //set the media controller in the VideoView
                myVideoView.setMediaController(mediaControls);
    
                //set the uri of the video to be played
                myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.kitkat));
    
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
    
            myVideoView.requestFocus();
            //we also set an setOnPreparedListener in order to know when the video file is ready for playback
            myVideoView.setOnPreparedListener(new OnPreparedListener() {
    
                public void onPrepared(MediaPlayer mediaPlayer) {
                    // close the progress bar and play the video
                    progressDialog.dismiss();
                    //if we have a position on savedInstanceState, the video playback should start from here
                    myVideoView.seekTo(position);
                    if (position == 0) {
                        myVideoView.start();
                    } else {
                        //if we come from a resumed activity, video playback will be paused
                        myVideoView.pause();
                    }
                }
            });
    
        }
    
        @Override
        public void onSaveInstanceState(Bundle savedInstanceState) {
            super.onSaveInstanceState(savedInstanceState);
            //we use onSaveInstanceState in order to store the video playback position for orientation change
            savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
            myVideoView.pause();
        }
    
        @Override
        public void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            //we use onRestoreInstanceState in order to play the video playback from the stored position 
            position = savedInstanceState.getInt("Position");
            myVideoView.seekTo(position);
        }
    }
    

提交回复
热议问题