I am trying to load video files from my internal storage to a VideoView. I download these video files from a server and then store them locally using this code:
FileOutputStream out = openFileOutput(filename, Context.MODE_WORLD_READABLE);
The documentation here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal made me believe that these files would be created as world readable, but I don't think that is true.
Here is the code I use to load the file into the ViewView:
videoView.setVideoPath(getFilesDir() +"/"+ filename);
And here is the output I get from DDMS:
ERROR/MediaPlayer(4264): error (1, -2147483648)
ERROR/MediaPlayer(4264): Error (1,-2147483648)
DEBUG/VideoView(4264): Error: 1,-2147483648
If I change the code to load the same video from my raw resources directory, everything runs fine. However, I need to be able to download these videos and play them locally.
Are my only options to use external data or is there a way to make internal data public to something like a VideoView?
CommonsWare solution in the comments is repeated below, as it is the one I ended up using:
Note that you can always use MediaPlayer and SurfaceView if you don't like the interface of VideoView. Or, implement a simple ContentProvider with openFile() to return the FileDescriptor and use the content:// Uri with VideoView. - CommonsWare
Related
I working on media application, On app user able to watch video on any video player. all media file is stored in Internal Storage which is .mp4 formate in particular folder called LearningVideo.
My question is I want to encrypt that video so a user won't able access that and also decrypt that video when app play that.
So how can I achieve this task, please help me.
I tried com.google.android.exoplayer:exoplayer:2.6.0
Use CipherOutputStream and CipherInputStream for encryption and decryption of file in android.
There are two ways through which you can achieve your goal
Download a file and encrypt it, when you want to play that file decrypt it in a temporary file and play it.
if you want to play the encrypted file on the fly (not decrypting it in a temp file) then you can use Libmedia library. It streams encrypted file on localhost and plays it from there
Try to write Files inside FilesDir may be this solved your issue
File rootpath = getApplicationContext().getFilesDir();
File path = new File(rootpath.getAbsolutePath() + "/LearningVideo");
You can store your files inside /data/user/0/<packageName>/files
I have a requirement to be able to save and play videos from the internal memory. The files have a mix of .mp4 and .3gp formats. My question has two parts - saving files and playback.
SAVING FILES
I know of two ways to save video into the internal memory:
1. FileOutputStream f = context.openFileOutput(videoName, MODE_WORLD_READABLE);
2. FileOutputStream f = new FileOutputStream(internalFilePath+File.separator+videoName);
where internalFilePath is obtained separately by using getFilesDir() function.
Question: I can only play videos which were saved using #1 and not #2 above. Why? I get errors saying like:
"This video can't be played"
Is it because files stored by #2 are not readable by video playback apps because they are private to my app? But then how can I make my files private and playable at the same time? This is where the second part of the question fits I guess.
PLAYBACK
I know of three ways to play videos in my app:
Building an intent with ACTION_VIEW, setting the data type to the appropriate mime type, startActivity(intent) and letting installed apps take care of the playback.
Using a VideoView. Although I personally don't like this approach because of the amount of coding involved.
Using MediaPlayer class.
Question: Considering that I store my files in the internal memory, which of these methods is the best for playback?
When you are saving a video to your internal storage, that video is accessible to that application only and no other app can use that video. In your case, you have shown two methods to save videos to your file.
FileOutputStream f = context.openFileOutput(videoName, MODE_WORLD_READABLE);
This way you are able to play your file because you have set mode as MODE_WORLD_READABLE. MODE_PRIVATE makes it private for your app. This is why you are able to use the video file from other app by this way as your mode is mode_world_readable and when you are saving the file the second way, it doesn't play because the file is again private to your application only.
For playing videos that have been saved from the second approach, you can try the following code:
FileInputStream fileInputStream = new FileInputStream(filePath);
mediaPlayer.setDataSource(fileInputStream.getFD());
I guess this should work.
Edit: Please change the above code snippet variables as per your project needs.
Source: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Hosting the video online is not possible nor is using an SD card a realistic solution to my issue... Due to several factors such as the likely hood that the SD cards would disappear.
So I found this, and this says it's possible: https://stackoverflow.com/a/5475436/584994
But I can't seem to get the FileInputStream correct... Under the package explorer my file is located under AppName->res->drawable->overview_animatic.mp4
and the string I am passing to FileInputStream is "/res/drawable/overview_animatic.mp4" am I doing this incorrectly?
Does the solution in the above link no longer work?
Is there a better way to play an internal video?
See the top answer for How to play videos in android from assets folder or raw folder? for an explanation of how to play a video from an application resource.
I am going to be downloading audio files to the device's internal memory. At a later time I would like to play this audio with a MediaPlayer. In the "Media Playback" guide on the developer site it mentioned using a "URI". Is there any way I could just use the filename of the file in the internal memory? How can I play saved audio?
To get the URI from a file path this may help:
it looks like
Uri.parse(new File("/sdcard/cats.jpg").toString()) should do the trick.
I have a simple Android webview app, built with Phonegap Build. When trying to play a local mp3 file that's included in the APK, nothing happens. However, if I pull an mp3 from the web it works.
e.g.
<audio src="www.example.com/01.mp3" > WORKS JUST FINE
<audio src="01.mp3" > DOES NOT WORK
I'm new to Android development, so I can only guess that the file is somehow inaccessible by Android's media player. Here's a link to the errors/warnings from my log file if that helps shed any light on the problem.
http://pastebin.com/isS542RE
I have it working using Phonegap's Media class. I must have been doing something wrong initially, but I'm not sure what. The proper way to access it through the media object is as Simon stated previously:
myMedia = new Media("/android_asset/www/test.mp3");
You have to put the full path as the media class defaults to the /SDCARD directory on relative paths.
Simon also has a great write up about using the media class:
http://simonmacdonald.blogspot.com/2011/05/using-media-class-in-phonegap.html?m=1
As for the HTML5 method and using the AUDIO tag, I'm still not certain why it fails on local files. Phonegap's media class works just fine though and is probably a more reliable solution anyhow. Phonegap FTW!
Have you tried pointing the file to the local file system path using "file:///android_assets/01.mp3" assuming your .mp3 is within the applications assets directory?
We had the same problem, the media files, in order to be played, needs to be in the external storage dir. Try to move your HTML and related files to /mnt/sdcard/.
I filed an official bug report on this:
http://code.google.com/p/android/issues/detail?id=41995&thanks=41995&ts=1356643666