Brian Var
Brian Var

Reputation: 6227

Setting up a video view

I added the following code to my app to add a video view which is linked to a video in my raw folder but I'm getting an error on VideoView saying that VideoView cannot be resolved or is not a fieldI have included all the relevant imports.Is the an error somewhere in my syntax?

VideoView StudentLife = (VideoView) findViewById(R.id.VideoView);

        Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.learningatgmi);

        videoview.setVideoURI(uri);
        videoview.start();  

My xml layout for videoview is as follows:

<VideoView
        android:id="@+id/videoView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="53dp" />

Upvotes: 0

Views: 6585

Answers (4)

Rohit
Rohit

Reputation: 2681

Actually when you use this code,

Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.learningatgmi);
StudentLife.setVideoURI(uri);

It passes URI as null so it shows you error So instead of that you cant use following code.

StudentLife=(VideoView)findViewById(R.id.videoplayer);
StudentLife.setVideoURI(Uri.parse("android.resource://" +getPackageName()+ "/" +R.raw.sample));
StudentLife.requestFocus();
StudentLife.start();

Instead of Parsing URI before setting, it will be better if you pass it during setting it.

Upvotes: 0

Praful Bhatnagar
Praful Bhatnagar

Reputation: 7435

So you variable name is StudentLife

VideoView StudentLife = (VideoView) findViewById(R.id.VideoView);

and you are calling other mehtod's on videoview which is undefined variable..

So following code:

        videoview.setVideoURI(uri);
        videoview.start();  

should be:

        StudentLife.setVideoURI(uri);
        StudentLife.start();  

EDIT1:

As per you xml the line to get the instance of video view should be following

VideoView StudentLife = (VideoView) findViewById(R.id.videoView1);

Full working code should be as following:

VideoView StudentLife = (VideoView) findViewById(R.id.videoView1);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.learningatgmi);
StudentLife.setVideoURI(uri);
StudentLife.start();  

On the side not; you should not use the class name as the variable name... also in java first character of variable should not be capital... So let use 'videoView' as the variable name.. So now following should be the working code with right kind of variable name..

VideoView videoView = (VideoView) findViewById(R.id.videoView1);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.learningatgmi);
videoView.setVideoURI(uri);
videoView.start();  

Upvotes: 4

Rajesh
Rajesh

Reputation: 15774

The id of your VideoView is videoView1. So you should get a reference to the VideoView object from the view hierarchy using R.id.videoView1:

 VideoView StudentLife = (VideoView) findViewById(R.id.videoView1);
 Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.learningatgmi);
 StudentLife.setVideoURI(uri);
 StudentLife.start();  

Upvotes: 1

Sanket Pandya
Sanket Pandya

Reputation: 1095

My code works ::

mc = new MediaController(this);
vd.setMediaController(mc);
vd.setVideoURI(intentUri);
vd.start();
setContentView(vd);

Upvotes: 0

Related Questions