I am working on Android 3.1 ,
My problem is that the downloaded video from link to my applications cache directory does not working,
but Working the same downloaded video from the Cache?
The video is being downloaded and I am saving it into memory application like the following senarios
// Works
File file = new File(getCacheDir(), "sample.mp4");
// Does not work
String RootDir = getCacheDir() + File.separator + "FirstData";
File RootFile = new File(RootDir);
RootFile.mkdirs();
File file = new File(RootFile.getAbsolutePath(), "sample.mp4");
If I attempt to play the video and set the path like .
videoview.setVideoPath(file.getAbsolutePath());
and trying to "stream" the video to my VideoView, it is showing sorry can not play the video
and showing the errors like
03-22 12:43:39.898: E/MediaPlayer(1335): error (1, -2147483648)
and also i sets the file permissions like
file.setReadable(true,false);
even it showing the same.
but its working fine when the location of video is getCacheDir(), Only the problem with inside cache one more directory i have created and trying to play then the problem is coming.
So Can anyone explain this behavior?
Related
I have an APP that is only a container for a VUE PWA. In one page the user could upload some file. I implemented the following solution to give the user the option to select a file from Galery or take a new photo.
https://blog.verslu.is/xamarin/xamarin-forms-xamarin/building-a-hybrid-app-with-xamarin-forms/
The problem is the FileReader is not able to read the file when the user takes a new photo with the camera. if the user selects an existing file, the upload works well. This is the error:
FileReader:
message: "The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired."
name: "NotReadableError"
I noticed one difference: when the user selects a file from galery, the URI path that the APP sent to the WebView looks like the following:
{content://com.android.providers.media.documents/document/image%3A32681}
When the user takes a new photo:
file:///storage/emulated/0/Pictures/JBSTRIP/IMG_637273232207442660.jpg
Anyone had the same problem? I didn't found any solution from web.
Thanks a lot!
I solved the problem.
I changed the directory where the picture from camera is saved:
Instead get the path using:
File imageStorageDir = new File (Android.OS.Environment.GetExternalStoragePublicDirectory (
Android.OS.Environment.DirectoryPictures), "APP");
I used the APP's folder:
File imageStorageDir = new File (_context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures), "APP");
I am downloading video files using retrofit streaming method and saving them inside external storage. I wish to delete any file that does not reach 100% download ( i.e if 10MG is successfully downloaded out of a 20MB video file), the file is saved as though it is 100% complete, but when playing, let's say the video length is 30seconds, when it reaches 15 seconds, it displays 'bad file, can't play'. My wish is to know whether or not the file is complete or not by locating file path with the example code below;
File f = new File(path);
boolean d = f.delete();
if (f.exist()){
// check if file will not display 'bad file' while playing.
// if file will break while playing, then execute f.delete() to delete file so a new one can be downloaded
}
Currently I am downloading a ".mp4" video and saving it in a folder. I want to hide it from gallery. I know how to hide a folder. Currently I am saving it in a different extension for ex. like ".sss" and it is saving properly. Now how can I play that video in my application.
I tried to change the extension and in file and try to play it, but it is not working, here is my code
File extStore = Environment.getExternalStorageDirectory()+"/"+"downloadFolder"+"/"+"video1.sss".replace(".sss",".mp4");
JCVideoPlayer mJcVideoPlayerStandard = (JCVideoPlayerStandard) findViewById(R.id.videoplayer);
mJcVideoPlayerStandard.setUp(extStore.getPath(),JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "Normal");
mJcVideoPlayerStandard.showContextMenu();
I am using this path to play the video,and I am usign JcVideo player lib for playing the video, here is the gradle for it
compile 'fm.jiecao:jiecaovideoplayer:5.4'
When I am saving the file in ".mp4" format I am able to play the video. But when I use different format,I am not able to play it. Any help will be preferable
I think what you are trying to do is to rename the file to mp4 before you play it?
If so the code above is replacing the the '.sss' with '.mp4' in the search string rather than renaming it in the file system.
In other words the line:
File extStore = Environment.getExternalStorageDirectory()+"/"+"downloadFolder"+"/"+"video1.sss".replace(".sss",".mp4");
simply translates to:
File extStore = Environment.getExternalStorageDirectory()+"/"+"downloadFolder"+"/"+"video1.mp4";
As your actual file is still video1.sss you get an error when you try to play a file named video1.mp4 as it can't be found.
If you do want to rename a file you can do it like this:
File file1 = new File("Path of file");
File file2 = new File("Path with new name for the file");
boolean ok = file1.renameTo(file2);
I think you simply can't do it! by changing the extension of a video file you can't play it. because no matter what its extension is, the Audio and Video format coding format won't change by changing extension name. If a player can't play such extension, it is not in its supported video codec.
The Audio and Video format define how a video file is build byte to byte. check this Wikipedia page.
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!
hi guys i read many example about play video in video view, but no one work for me, i get this error:
java.io.FileNotFoundException: /android.resource:/frt.com.maint/2130968576 (No such file or directory)
this is my code-------------------------------------------------------------------------:
FileInputStream fi = new FileInputStream("android.resource://frt.com.maint/" + R.raw.videointro);
MediaPlayer pl = new MediaPlayer();
pl.setDataSource(fi.getFD());
pl.prepare();
pl.start();
MediaPlayer don't have method setVideoURI, i use the first solution that you give me but i still get same error, after i use this code with videoview:
Uri video = Uri.parse("android.resource://frt.com.maint/videointro");
vidview_gdf.setVideoURI(video);
vidview_gdf.start();
but i get an error with message "you can not play the video"
p.s: additional info: introvideo.mp4 - 7 MB
You're trying to use the ID of the resource, which is just an int index.
Use the filename instead:
fi = new FileInputStream("android.resource://frt.com.maint/nitrovideo");
Or better:
StringBuilder videoURIPath = new StringBuilder();
videoURIPath.append("android.resource://");
videoURIPath.append(getPackageName() + "/");
videoURIPath.append("raw/");
videoURIPath.append(videoFileName);
pl.setVideoURI(Uri.parse(videoURIPath.toString());
Where videoFileName is a string of the name of your file.
Are you doing this on emulator or actual device?
I had a bit of bad experience with H.264 encoded video before. Basically, I tried to play it on the first GalaxyTab but it didn't work. Turned out that GalaxyTab I had didn't support H.264.
So, I would advise you to make sure that the default video player can play this file before proceed further. If that's not the case for you then I'm not sure what's wrong. Your code looks fine to me.