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.
Related
I'm testing the demo at,
http://docs.gstreamer.com/display/GstSDK/Android+tutorial+5%3A+A+Complete+media+player
I only changed the permissions to allow reading from the SD card, and when I try to play a MP3 file, it says, amcaudiodec-omxgooglemp3decoder0: Internal data stream error. Any idea how to fix this?
Thanks!
Edit: Ogg files work fine.
I have my android app where data is packed using FileWrap. The thing is I want to wrap the mp3 data into the android file and play from that memory. I have checked the nativeaudio example in NDK which shows how to play MP3 as an asset or as a filesystem file, but it does not show how I can play an MP3 that is embedded into memory.
Please help. Thanks.
I don't think that is possible... yet.
AFAIK android opensl only supports MP3 decode & playback from a stream...
I have not seen the latest ndk release (r9b) yet, but if the opensl example app does not support in-memory, on-the-fly decoding, I doubt it is available.
Perhaps having a look here may clear things up:
https://groups.google.com/forum/#!searchin/android-ndk/opensl|sort:date/android-ndk/cMHlkyQkFU0/vMkyO2201yYJ
Or you could just dump your MP3 files in the "assets" folder and play them using the AAssetManager as per usual.
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 have used the following code: the mp3 is saved in the mobile sdcard.
notification.sound = Uri.parse("file:///sdcard/File/sdcard/introduction_file.mp3");
do you know how a mp3 file can be played as an application recources and not from the sdcard?
thank you
See android guide for playing from raw resource.