Reputation: 4455
I am new with android development and I created a new folder under res folder named videos and then I put a video file in it to use in the app. following is my code in attempt to use the video file.
package com.example.touseef.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView MyVideoView =(VideoView) findViewById(R.id.videoView);
MyVideoView.setVideoPath("android.resource://"+getPackageName()+"/"+R.videos.bang);
MyVideoView.start();
}
}
ERROR : It was giving error cannot find variable R.videos before, but then I tried to clean and rebuilt project after starting android studio, and it asked me for somelinking with bang.mp4 file as text document and I said ok.
Now when I try to build it, the bang.mp4 file opens in a text form within android studio and I receive following error.
Content is not allowed in prolog
Note: I cant see the folder videos under res folder within android view in android studio but folder does exist as I can see it in FileEx plorer in my pc and it also has that video file in it. Also note that when I restart my project, I can see the folder, but it again disappears after android studio completes loading with indexing of my project.
Upvotes: 0
Views: 931
Reputation: 971
Create raw folder inside res folder and put your videos in that folder
String uri = "android.resource://" + getPackageName() + "/" + R.raw.video;
Uri video = Uri.parse(uri);
VideoView videoView=(VideoView) findViewById(R.id.videoView);
videoView.setVideoURI(video);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
videoView.start();
}
});
Upvotes: 3
Reputation: 10270
res/videos
is not a valid resource folder. You'll need to put your video into res/raw
and refer to it from R.raw
. Have a look at the Providing Resources section of the Android documentation. One important gotcha to keep in mind though:
However, if you need access to original file names and file hierarchy, you might consider saving some resources in the
assets/
directory (instead ofres/raw/
). Files inassets/
are not given a resource ID, so you can read them only usingAssetManager
.
Upvotes: 4