How to play videos from SD Card - android

I was creating a simple app which stream videos from net and I made it but now I want to change the code so that I can play video files from my SDCard
original code:
Uri vidFile = Uri.parse("MY SITE HERE");
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
videoView.setVideoURI(vidFile);
videoView.setMediaController(new MediaController(this));
videoView.start();
So please help me with changing the code so that it can play videos from my mobile memory card.

The videoView.setVideoURI(vidFile); method needs to be replaced by the videoView.setVideoPath(path); method.
Here path specifies the path of the video file on the SDCARD.
This path can be easily retrieved using the MediaStore.Video.Media.DATA property of that video file or by just entering the songpath statically as /sdcard/songname.

Uri vidFile = Uri.parse(
Environment.getExternalStorageDirectory().getAbsolutePath()+"filename");
...
the rest of the code will be same.

In place of
videoView.setVideoUri(vidFile)
use
videoView.setVideoPath("/sdcard/SONG.").
Let me know.

these links may help you:
Playing 3gp video in SD card of Android
how-play-video-and-audio-android
Using VideoView to play mp4 from sdcard

I also tried your code and got same error message but when I tried with video path with no blank space in path or name, it worked well. Just give it a try.
e.g,
file path "/mnt/sdcard/Movies/Long Drive Song - Khiladi 786 ft. Akshay Kumar_Asin-YouTube.mp4"
gave the error but file path "/mnt/sdcard/Movies/Khiladi.mp4" worked well.

I know is is old but if it can help. add this to your manifest
<uses-permission android:name="com.android.externalstorage.ExternalStorageProvider"/>

Related

How to play .mp4 video on videoview

just want to know how to play a .mp4 video on videoview.
I got a tutorial from http://mrbool.com/how-to-play-video-formats-in-android-using-videoview/28299 and my app is not working.
I followed everything. i named my video "video.mp4".
I used this code to find the path of a specific video:
video_player_view.setVideoPath(Environment.getDataDirectory().getAbsolutePath() + "video.mp4");
I even made a "raw" folder and placed the video there. I used #raw/video.mp4 for it.
Can you guys help me out please. Thanks a lot.
Try assigning the video to the videoview with setVideoURI().
vidVwYourView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.your_video));
Just make sure 'your_video.mp4' is stored in your res/raw/ folder.
I have the similar problem. In may case, I remove video file extension and it works. So try with "video" instead of "video.mp4"

Android path to resource inside package

I tried to add a video path to an Android sample project - MediaPlayerDemo
I can playback the video when it stored in sdcard, the path is
"file:///sdcard/dcim/a.m4v"
But I can't playback the video when it stored in res/drawable. the path is
"android.resource://" +this.getPackageName () + "/" + R.drawable.a
I can read the id of the video in debug mode, but just can't replay the video.
How to solve it?
UPDATES
Thank you for the reply, so far i have tried:
put the video in assets, set path to "file:///android_asset/a.m4v".
put the video in raw, set path to
("android.resource://" +this.getPackageName () + "/" + R.raw.a) or ("android.resource://" +this.getPackageName () + "/raw/a)
but none of them can playback video.
My video is 1.8Mb, does it matter?
Create a new folder with name raw in res folder, if already created let it be. Copy your playable video file (e.g., myvideo.mp4) to raw folder. Use below code in your app.
String uriPath = "android.resource://"+getPackageName()+"/raw/myvideo";
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
I tested, its working for me. If the video is playable from sdcard then only it will play from raw folder, otherwise it will show a dialog box says Cannot play video.
try it and let me know what happened.
FYI, drawable is to store icons, images, drawables for the application. So You can put the same video either in assets or in raw folder.
I have found the solution, but not a straight forward way.
First, the file not found problem is not the path problem, it is because the permission problem.
To solve that, many people suggest copy the file into FileInputStream. But still got file not found problem.
But the File can be written to Inputstream. However, setDataSource() of Mediaplayer class does not acccept InpuStream. Therefore need to write the Inputstream to a temp File by BufferedOutputStream.
finally, setDataSource(tempfile_path) without error.
This question is quite old and still not answered well so here I will answer.
First of all, do not put videos in the assets folder. It is a bad practice. Create another folder (Preferably one named raw).
The second thing is that please do not use m4v format. Use a mp4 video.
Here is the code to insert video:
//Here it is assumed here that the file name of video in raw folder is demo
VideoView video = (VideoView) findViewById(R.id.videoView);
video.setVideoPath("android.resource//" + getPackageName() + "/" + R.raw.demo);
video.start();
Hope this clarifies your doubt!

Android :unable to play video

I am working in android. I just used a video player. I am accessing video from my sdcard.
When i access video from myFolder then it works fine, but there are some folder which name are Japanese character. So whenever i want to access video from those folder then "Sorry, This video can not play" error is occurred.
This is my code which i am using:-
VideoView video = (VideoView) findViewById(R.id.videoId);
MediaController controller = new MediaController(this);
controller.setPadding(0, 0, 0, 55);
video.setVideoPath(URLDecoder.decode(sdCardUrl));
video.setMediaController(controller);
video.requestFocus();
video.start();
and this my path in which i am facing problem to video:-
url = /sdcard/.FileStorage/History/Myfilestorage/のフダ名/H.264(avc)_mp3_1000kbps_640x480_25fps.mp4
please suggest what should i do ?
Thank you in advance.
Try by converting that Japanese name in to UTF-8 format and then try to use it.
I have resolved my problem to trace the sequence of output of each line. I have done mistake that there is no need to decode url.
So the solution is to use following line:-
video.setVideoPath(sdCardUrl);
in place of video.setVideoPath(URLDecoder.decode(sdCardUrl));

Why VideoView doesn't play file from path

I am using VideoView on Activity and I record video ( I checked it is on "/mnt/sdcard/DCIM/v110.3gp" exists) and when I try in code
videoView.setVideoPath("/mnt/sdcard/DCIM/v110.3gp");
videoView.start();
nothing happens. That file exists but videoView doesn't want to play that. What can be wrong ?
You can't be 100% sure that all phones use /mnt/sdcard as the external storage directory. Use
Environment.getExternalStorageDirectory() instead.
videoView.setVideoPath(Environment.getExternalStorageDirectory()+/"DCIM/v110.3gp");

I want to play a video from my assets or raw folder

I want to play a video from my assets or raw folder in my app in Android
using VideoView I am getting the error as video cannot be played
please anyone give me a solution.
Here is the code I used
VideoView vd = (VideoView)findViewById(R.id.Video);
Uri uri = Uri.parse("android.resource:" + R.raw.video);
MediaController mc = new MediaController(this);
vd.setMediaController(mc);
vd.setVideoURI(uri);
vd.start();
A few things to note:
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: my_video_file.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.my_video_file
The Activity class has a helper method getPackageName() which can be used by your code when constructing the correct URI to your video.
VideoView vv = (VideoView)this.findViewById(R.id.videoView)
String uri = "android.resource://" + getPackageName() + "/" + R.raw.my_video_file;
vv.setVideoURI(Uri.parse(uri));
vv.start();
There is more information on this here.
You must include the package name in the uri:
Uri uri = Uri.parse("android.resource://[package]/raw/video")
or
Uri uri = Uri.parse("android.resource://[package]/"+R.raw.video);
Also, check out these examples.
There are so many ways to go wrong with VideoView ! Mainly because the logcat gives you no help, always giving error UNKNOWN.
I found this link was by far the best way to get started...
A complete description so you can't go wrong. Thanks go to the author...
http://androidexample.com/Play_Video_File_-_Android_Example/index.php?view=article_discription&aid=124&aaid=144
When we include a video resource in /resource/raw/ or assets/, by default, it looks for the .mp4 format, it won't accept .wmv files. If you read video file from external locations(like: /mnt/sdcard/demo.wmv) then it'll accept them.
Try:
AssetFileDescriptor afd = getAssets().openFd(fileName);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
Make sure to write the file video name without extension.

Categories

Resources