Where to put the Video file in android Project - android

I have a video, I need to know where to place and how to get the path of that video.
I know how to add the video form URL,
Uri uri=Uri.parse("www.abc.com/myVid.mp4");
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVideoURI(uri);
This works fine, but now the video file is in my project, I need to know how to get the path from the folder structure
Kindly guide me.
Thanks

You can create asset folder inside your project and store your video in that folder.
Then you can get that using getAssets() function which is provided by Android.
EDIT 1:
You can click on the Project window, press Alt-Insert, and select Folder -> Assets Folder. Android Studio will add it automatically to the correct location.
Also, you can do this.
VideoView view = (VideoView)findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
view.setVideoURI(Uri.parse(path));
view.start();
Where video_file is your file name.

You can Create a folder under Resources and Name it raw. Then to provide the Path to the Video you can simply do
String path = "android.resource://" + getPackageName() + "/"
+ R.raw.intro_land;
and then
videoplayer.setVideoURI(Uri.parse(path));

You can view your own video by creating a folder under res as follows:
Right click on res -> New -> Android Resource Directory
select Resource type as raw
Then you can upload your video into this directory.
VideoView videoView = videoViewFragment.findViewById(R.id.videoView);
String path = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.video01;
herevideoView.setVideoURI(Uri.parse(path));
videoView.start();
video01 is your mp4 file name

You can create raw folder under res and put your video there,
Check this
VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
String uriPath = "android.resource://com.android.AndroidVideoPlayer/"+R.raw.k;
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();

Related

'Can't play this video' error when playing mp4 with video view?

I have the following code
val path = "android.resource://" + activity?.packageName + "/" + R.raw.body_video
val uri = Uri.parse(path)
videoView.setVideoURI(uri)
videoView.start()
but on loading the view i get this
The path all seems to be correct, so im unsure what could be wrong. This asset works on our iOS app so the file is ok
The way you are getting the id from resource folder (raw) is wrong. Try will following code it will work.
In Java
// To get files from any resource folder (eg: raw, drawable, etc.)
// Use the resource id
int rawId = getResources().getIdentifier(file_name_without_extension, "raw", getPackageName());
// URI formation
String path = "android.resource://" + getPackageName() + "/" + rawId;
// Set the URI to play video file
videoView.setVideoURI(Uri.parse(path));
videoView.start()
Kotlin Activity
// To get files from any resource folder (eg: raw, drawable, etc.)
//Use the resource id
val rawId = resources.getIdentifier(file_name_without_extension, "raw", packageName)
// URI formation
val path = "android.resource://$packageName/$rawId"
// Set the URI to play video file
videoView.setVideoURI(Uri.parse(path))
videoView.start()
Kotlin Fragment #Update
// To get files from any resource folder (eg: raw, drawable, etc.) Use the resource id
val packageNameValue = activity!!.packageName
val rawId = activity!!.resources.getIdentifier(file_name_without_extension, "raw", packageNameValue)
// URI formation
val path = "android.resource://$packageNameValue/$rawId"
// Set the URI to play video file
videoView.setVideoURI(Uri.parse(path))
videoView.start()

Android videoView not finding file in R.raw

I created the raw resources directory and inserted the video test.mp4 into it.
I am try to run the following code but it is giving me an unresolved reference on test
val uri = Uri.parse("android.resource://" + packageName + "/" + R.raw.test)
videoview.setVideoURI(uri)
videoview.setOnPreparedListener { videoview.start() }

How to play video from raw folder with Android device?

Please help,
How to play videos in android device from raw folder for offline mode?
Successful example1: I can play the video from SDcard used the below code.
Intent intent = new Intent(Intent.ACTION_VIEW);
String type = "video/mp4";
Uri uri = Uri.parse("file:///sdcard/test.mp4");
intent.setDataAndType(uri, type);
startActivity(intent);
Failed example2:
Question: May I put the test.mp4 to res/raw folder?
Intent intent = new Intent(Intent.ACTION_VIEW);
String type = "video/mp4";
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.taipei);
intent.setDataAndType(uri, type);
startActivity(intent);
Have anyone can help me? Please.
Copy the video into your project's res/raw folder. Create raw folder under res folder. It must be in a supported format (3gp, wmv, mp4 ) and named with lower case, numerics, underscores and dots in its filename likewise:video_file.mp4.
VideoView view = (VideoView)findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
view.setVideoURI(Uri.parse(path));
view.start();
Create videoView in your xml file.
// To get files from any resource folder (eg: raw, drawable, etc.)
// Use the resource id
int rawId = getResources().getIdentifier(file_name_without_extension, "raw", getPackageName());
// URI formation
String path = "android.resource://" + getPackageName() + "/" + rawId;
// Set the URI to play video file
videoView.setVideoURI(Uri.parse(path));
Check this solution How to play videos in android from assets folder or raw folder?
VideoView videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.your_raw_file); //do not add any extension
//if your file is named sherif.mp4 and placed in /raw
//use R.raw.sherif
in my code "applicationdemo" is the the name of my video file.
String video_url = "android.resource://" + context.getPackageName() + "/" + R.raw.applicationdemo;
final VideoView videoView = findViewById(R.id.dialog_video);
Uri videoUri = Uri.parse(video_url);
MediaController mediaController= new MediaController(context);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(videoUri);
videoView.requestFocus();
videoView.start();
I struggled with this for dynamic video names. The solution that worked for me was:
//Somewhere set the video name variable
String video+name="myvideo";
//setup up and play video
VideoView videoView=(VideoView)findViewById(R.id.video);
videoView.setVisibility(View.VISIBLE);
String uriPath = "android.resource://"+getPackageName()+"/raw/"+ video_name;
Uri UrlPath=Uri.parse(uriPath);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(UrlPath);
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
if (position == 0) {
try{
videoView.requestFocus();
videoView.start();
}catch (Exception e){
System.out.printf("Error playing video %s\n", e);
}
}else{
videoView.pause();
}
}
});
And in XML
<VideoView android:layout_width="300dp"
android:id="#+id/video"
android:layout_height="300dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:keepScreenOn="true"
/>
i think , everyone gave an answer, but doesn't explain the scenario. The main problem here is, If im not mistaken , Android assume that the video coming from your SD Card is dynamic, where in it could be possible , that the format is not supported or supported, thus it enables / ask you to select or open for other third party media software.
While anything you play UNDER RAW folder, requires a handler such as videoview or built in media player which leads to conclusion that anything you put in your RAW folder should be supported / readable by the Android OS.
However , the thread starter here wants that his RAW files , to be played using a third party media player.
This Solution will exactly helps you that what you want.
VideoView myVideo;
private MediaController media_control;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideo = (VideoView) findViewById(R.id.playVideo);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bootanimation_nexus);
media_control = new MediaController(this);
myVideo.setMediaController(media_control);
myVideo.setVideoURI(uri);
myVideo.start();
}
player = ExoPlayerFactory.newSimpleInstance(requireActivity(), new DefaultTrackSelector(), new DefaultLoadControl());
Uri uri = RawResourceDataSource.buildRawResourceUri(getOfflineVideo(offlinePosition));
MediaSource mediaSource = new ExtractorMediaSource(uri, new DefaultDataSourceFactory(requireActivity(),
"MyExoplayer"), new DefaultExtractorsFactory(), null, null);
//setup player with mediaSource

Eclipse is Marking File Path for VideoView as incorrect

I have a VideoView here but Eclipse is saying raw cannot be resolved to a verable and apw cannot be resolved to a variable at line videoHolder.setVideoURI(Uri.parse(#raw/apw.avi));
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = (VideoView) findViewById(R.id.VideoView);
videoHolder.setMediaController(new MediaController(this));
videoHolder.setVideoURI(Uri.parse(#raw/apw.avi));
videoHolder.requestFocus();
videoHolder.start();
Try this way :
Uri videouri = Uri.parse("android.resource://" + getPackageName() + "/raw/apw");
videoHolder.setVideoURI(videouri);
Thanks.
You must copy the video into your project's res/raw folder.
It must be in a supported format (3gp, wmv, mp4 ) and named with lower case, numerics, underscores and dots in its filename: apw.mp4
When you work with this resource in code, you will reference through the generated R statics - it will have the file extension removed: R.raw.apw
VideoView videoHolder = (VideoView) findViewById(R.id.VideoView);
videoHolder.setMediaController(new MediaController(this));
String uri = "android.resource://" + getPackageName() + "/" + R.raw.apw;
videoHolder.setVideoURI(Uri.parse(uri));
videoHolder.requestFocus();
videoHolder.start();

Can't play video from raw folder using dynamic path

i have a strange behavior while coding a video player. I have an html5 menu that targets mp4 videos. When you click on a video, the path will be treated and a native video player intent will start to play the video. My videos are placed in "raw" folder.
When I use a static path, the video plays very well:
String uriPath = "android.resource://" + getPackageName() + "/" + R.raw.video1;
When I use the following path, I can't play it:
video_title = getIntent().getExtras().getString("video_title");
String uriPath = "android.resource://" + getPackageName() + "/" + "R.raw."+video_title;
Note that i removed the extension of the file in the main intent so the variable "video_title" will hold the video title without extension.
I resolved this issue, by changing the uriPath variable:
String uriPath = "android.resource://" + getPackageName() + "/" + "R.raw."+video_title; // BAD
String uriPath = "android.resource://" + getPackageName() + "/" + "raw/"+video_title; // GOOD

Categories

Resources