Why VideoView doesn't play file from path - android

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

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"

How to play videos from SD Card

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"/>

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 Media Player File Location

I'm trying to run a mp3 file using android media player. I'm using this code below
MediaPlayer mediaPlayer = MediaPlayer.create(this, "mmt/sdcard/mp3/file.mp3");
mediaPlayer.start();
But Ecliplse showing error & it says I can't use string as argument of create method. How can I run the file using this file location?
Plz help
you should use setDataSource to set the source of your media files. For files on the sdcard, the setDataSource(String ds) works best.
Also, note that your url should probably be mnt instead of mmt, and you need a forwardslash at the beginning, making your full url look like this: /mnt/sdcard/mp3/file.mp3

How to play videos in android from assets folder or raw folder?

I am trying to play a video in android emulator
I have the video in my assets folder as well as the raw folder
But after doing some research still i cant play video in my emulator
i am working on android 2.1
My video format is mp4 so i don't think that should be a problem
Could anyone just give me an example code so that i can understand a bit more?
The problem is that the VideoView that I need to display the Video will take only a URI or a File path to point to the Video.
If I save the video in the raw or assets folder I can only get an input stream or a file descriptor and it seems nothing of that can be used to initialize the VideoView.
Update
I took a closer look at the MediaPlayer example and tried to start a MediaPlayer with a FileDescriptor to the assets files as in the code below:
SurfaceView videoView = (SurfaceView) findViewById(gettingStarted)
SurfaceHolder holder = videoView.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
final MediaPlayer player = new MediaPlayer();
player.setDisplay(holder);
player.setDataSource(getAssets().openFd(fileName).getFileDescriptor());
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
Now I get a the following exception:
java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
It seems there is no other way then copying the file to the sdcard on startup and that seems like a waste of time and memory.
## Perfectly Working since Android 1.6 ##
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = getUriFromRawFile(context, R.raw.your_raw_file);
//if your file is named sherif.mp4 and placed in /raw
//use R.raw.sherif
videoHolder.setVideoURI(video);
setContentView(videoHolder);
videoHolder.start();
And then
public static Uri getUriFromRawFile(Context context, #ResRaw int rawResourceId) {
return Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(context.getPackageName())
.path(String.valueOf(rawResourceId))
.build();
}
## Check complete tutorial ##
String UrlPath="android.resource://"+getPackageName()+"/"+R.raw.ur file name;
videocontainer.setVideoURI(Uri.parse(UrlPath));
videocontainer.start();
where videocontainer of type videoview.
Try:
AssetFileDescriptor afd = getAssets().openFd(fileName);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
PlayVideoActivity.java:
public class PlayVideoActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_video);
VideoView videoView = (VideoView) findViewById(R.id.video_view);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.documentariesandyou));
videoView.start();
}
}
activity_play_video.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</VideoView>
</LinearLayout>
If I remember well, I had the same kind of issue when loading stuff from the asset folder but with a database. It seems that the stuff in your asset folder can have 2 stats : compressed or not.
If it is compressed, then you are allowed 1 Mo of memory to uncompress it, otherwise you will get this kind of exception. There are several bug reports about that because the documentation is not clear. So if you still want to to use your format, you have to either use an uncompressed version, or give an extension like .mp3 or .png to your file. I know it's a bit crazy but I load a database with a .mp3 extension and it works perfectly fine. This other solution is to package your application with a special option to tell it not to compress certain extension. But then you need to build your app manually and add "zip -0" option.
The advantage of an uncompressed assest is that the phase of zip-align before publication of an application will align the data correctly so that when loaded in memory it can be directly mapped.
So, solutions :
change the extension of the file to .mp3 or .png and see if it works
build your app manually and use the zip-0 option
Did you try to put manually Video on SDCard and try to play video store on SDCard ?
If it's working you can find the way to copy from Raw to SDcard here :
android-copy-rawfile-to-sdcard-video-mp4.
I found it :
Uri raw_uri = Uri.parse(<package_name>/+R.raw.<video_file_name>);
Personnaly Android found the resource, but can't play it for now. My file is a .m4v
VideoView myVideo = (VideoView) rootView.findViewById(R.id.definition_video_view);
//Set video name (no extension)
String myVideoName = "my_video";
//Set app package
String myAppPackage = "com.myapp";
//Get video URI from raw directory
Uri myVideoUri = Uri.parse("android.resource://"+myAppPackage+"/raw/"+myVideoName);
//Set the video URI
myVideo.setVideoURI(myVideoUri);
//Play the video
myVideo.start();
https://gist.github.com/jrejaud/b5eb1b013c27a1f3ae5f
I think that you need to look at this -- it should have everything you want.
EDIT: If you don't want to look at the link -- this pretty much sums up what you'd like.
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
But I still recommend reading the information at the link.
It sounds maybe like you have the same issue as i do. instead of using MP4, is 3GPP possible? i think i used like HandBrake or something as the video converter... you just need to make sure you have the right encoder, like H.264x or something. sorry for being a little vague, it's been a while. Also, if it's possible, don't bother worrying about android 2.1 anymore, and also, something things just WILL NOT WORK IN EMULATOR. so if it works on a lot of devices, then just assume it works (especially with different manufacurers)
here, you can read my problem and how i solved the issue (after a long time and no one had an answer). i explained in a lot more detail here:
android media player not working
In the fileName you must put the relative path to the file (without /asset)
for example:
player.setDataSource(
getAssets().openFd(**"media/video.mp4"**).getFileDescriptor()
);
Use the MediaPlayer API and the sample code.
Put the media file in raw folder.
Get the file descriptor to the file.
mediaplayer.setDataSource(fd,offset,length); - its a three
argument constructor.
Then when onPreared , mediaplayer.start();
MainCode
Uri raw_uri=Uri.parse("android.resource://<package_name>/+R.raw.<video_file_name>);
myVideoView=(VideoView)findViewbyID(R.idV.Video_view);
myVideoView.setVideoURI(raw_uri);
myVideoView.setMediaController(new MediaController(this));
myVideoView.start();
myVideoView.requestFocus();
XML
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="+#/Video_View"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

Categories

Resources