Home > Blockchain >  How to play a video from raw folder?
How to play a video from raw folder?

Time:12-12

I want to play a video from raw folder. I tried a java code but I give an error on my Logcat and the app is stops on the phone.

This is my code:

public class Help extends AppCompatActivity {
VideoView videoView;

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


    videoView = findViewById(R.id.helpvideo);

    MediaController mediaController = new MediaController(this);
    videoView.setMediaController(mediaController);
    Uri uri = Uri.parse("android.resource://"   getPackageName()   "/"   R.raw.video);
    videoView.setVideoURI(uri);
    videoView.start();



  }
}

And this is the error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sinaMoradi.iran.ps, PID: 25296
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sinaMoradi.iran.ps/com.sinaMoradi.iran.ps.Help}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.VideoView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3827)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4003)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8595)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
 Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.VideoView
    at com.sinaMoradi.iran.ps.Help.onCreate(Help.java:24)
    at android.app.Activity.performCreate(Activity.java:8207)
    at android.app.Activity.performCreate(Activity.java:8191)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3800)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4003) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:246) 
    at android.app.ActivityThread.main(ActivityThread.java:8595) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 

I'm a novice developer and I don't what are this errors. Thanks for your help.

CodePudding user response:

In res/layout/activity_help.xml, you have a widget named helpvideo. This is a ConstraintLayout. It is not a VideoView (or, perhaps, you have two widgets both named helpvideo). Ensure that the only helpvideo in the layout is the VideoView.

CodePudding user response:

You can create your xml file like this to open video view

 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_gravity="center" >
 
     <VideoView
        android:id="@ id/video_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"/>
 
  </FrameLayout>

Paste video file to your res/Raw folder of the project.

Source code for main activity:

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);
    }
}

the part where the URI of the video file from raw folder is below:

 myVideoView.setVideoURI(Uri.parse("android.resource://"   getPackageName()   "/"   R.raw.kitkat));

You can use this code to play video from your raw folder. #HappyCoding

  • Related