VideoView can play mp4 file in application data directory - android

While save mp4 file to /data/data/mypackage/files/my.mp4, then call VideoView's setVideoPath(); it can not work.
For the same file, if I save the file to /mnt/sdcard/my.mp4, then call the same VideoView's setVideoPath, it plays correctly.
Is there any way I can play mp4 file under /data/data/mypackage/files/?

The MP or VideoView uses a native player which cannot access non-worldreadable files.
So you have to options basically:
1) Make the created files world-readable
2) Open an input stream to the file in your program and just hand over the file descriptor to the media player:
FileInputStream fi = new FileInputStream(file);
MediaPlayer pl = new MediaPlayer();
pl.setDataSource(fi.getFD());
pl.prepare();
pl.start();
Also look at this thread VideoView/MediaPlayer doesn't play video from internal storage And find a Custom VideoView class code from here also look at this SO question Can a videoview play a video stored on internal storage?

Related

MP4Parser Can we have Android MediaPlayer directly act as a sink?

Lets say my I have my Mp4Parser Container java object and then instead of writing to a file and then supplying the URL of this file to the Android Media Player to play this video.
Container outMux = new DefaultMp4Builder().build(countVideo);
FileOutputStream fos = new FileOutputStream(
new File("outputFinalVideo.mp4"));
outMux.writeContainer(fos.getChannel());
fos.close();
In the above I am concatenating multiple videos and then Muxing with Audio and then I want this to be in memory and play directly in android media player or if there is any other way.
Can we write the object representation to Android media Player directly so android media player will be acting like a sink. Is that even possible ? or is there any better way to achieve playing this video all in memory ?
So it will be like all done in Memory rather than writing to a file as the sink and then supplying the file URL to the android media player.
import android.media.MediaPlayer;
public class MainActivity implements Activity{
private MediaPlayer mediaPlayer;
//url is the url for the outputFinalVideo.mp4 after being generated
mediaPlayer.setDataSource(getApplicationContext(), url);
.......
}
The video is 20 minute long I was able to do everything in memory in iOS using AVFoundation Framework.

Playing media from internal storage directory I created

So I created a directory on the internal storage like so:
File mediadir = getDir("tvr", Context.MODE_PRIVATE);
Then I download files from a server and save them inside the directory like this:
URL url = new URL(urlString);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
InputStream is = url.openStream();
Log.d("DOWNLOAD NAME",name);
FileOutputStream fos = new FileOutputStream(mediadir+name);
etc
Then files are saved successfully, then next I want to play them like this:
String path = filelist[playListIndex].getAbsolutePath();
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVisibility(View.VISIBLE);
videoView.setOnCompletionListener(this);
videoView.setVideoPath(path);
videoView.start();
where path is :
/data/data/com.mypackage/tvr/video.mp4
The file does not want to play with this error:
02-20 15:57:21.447: E/MediaPlayer(24143): error (1, -2147483648)
And on the device a message pops up : Cannot play video, Sorry this video cannot be player.
Is this a issue with rights or what? If it is, I was thinking because I created them, I have the rights to them?
CommonsWare has a good example on how to use the VideoView. Here is a link to someone who had a similar issue with video not wanting to play:
Playing a video in VideoView in Android
Also here is a video class of his example that I found useful when learning how to incorporate the VideoView:
https://github.com/commonsguy/cw-advandroid/blob/master/Media/Video/src/com/commonsware/android/video/VideoDemo.java hope this helps.
I would also make sure that you have the permission settings in your manifest set:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The problem might be with the video encoding. Android FROYO and Gingerbread doesn't support H264 formats other than "Baseline" H264. So if your video is Mp4 & H264 encoded make sure its "AVC baseline" encoded. Use some tools like "Media info" in windows/Linux and check your video encoding. Convert the video to Baseline if possible.
An alternative workaround is to skip the Videoview and use a video play intent and redirect the playback to an app. User will be prompted to pick a player to handle the playback. Obviously if the video view cant play the file, the default player also wont be able to handle the file. you can choose some other installed player like Mx-Player which will stream the file perfectly.

How to initialize MediaPlayer in android without an R.raw file

How do I initialize MediaPlayer in Android without an R.raw or some kind of file?
If I don`t initialize MediaPlayer with some file I will get a null pointer exception at runtime. But when the program is started there is no file path to use because the user has not yet pressed a button to select the mp3 file from the SD card.
When initialzing the Media Player there is another probem. It only takes an R.raw type file, not the path of a file from the SD card. And if I don't have any file in the R.raw directory then I can`t initialize with a file. It appears that you need an audio file in the local R.raw folder to do this.
Mediaplayer player = MediaPlayer.create(this, R.raw.sample_music);
There is a method called "setDataSource" that allows me to set the path of the file, however I have to initialize the MediaPlayer first.
player.setDataSource(selectedAudioPath);
Any other way to initialize MediaPlayer?
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filepath);
This post helped me and the code below works as well:
Mediaplayer player = MediaPlayer.create(this, Uri.parse(File.getAbsolutePath()));

Playing sound files from expansion file

I have an expansion file with a series of sound files. The longer ones are .mp3 files while the shorter sound effects are .wav files. I pack them in a .zip file with no compression. I use the Zip File reader and downloader library provided by Google.
I'm opening the sound files like so:
MediaPlayer mPlayer;
...
AssetFileDescript asd = expansionFile.getAssFileDescriptor(fileName);
FileDescriptor soundFile = asd.getFileDescriptor();
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(soundFile);
mPlayer.prepare();
mPlayer.start();
The odd thing is that it plays the same sound file every single time. It seems to be whatever it finds first. The byte size for asd.getDeclaredLength() and asd.getLength() is exactly what it should be. This means the file descriptor is at least finding the file, but for some reason the MediaPlayer plays whatever random file it decides.
What exactly am I missing here? Any help would be greatly appreciated.
Thanks.
The first approximation is correct, except for when the zip file contains more than one file, then you should use:
mPlayer.setDataSource(asd.getFileDescriptor(), asd.getStartOffset(), asd.getLength());
You can get more information from this other question:
Play audio file from the assets directory
Please, also remember to close the AssetFileDescriptor just after setDataSource to media player:
asd.close();
http://developer.android.com/reference/android/media/MediaPlayer.html#setDataSource%28java.io.FileDescriptor,%20long,%20long%29

Right place for putting mp3 files in an android project

Is there any folder like res/drawable for mp3 or generally audio files? If yes, what is it and how can I get access to it from the app?
The best place to put such .mp3 or any other files would be in the assets folder.
These files once stored will become a part of your android app itself and can be read easily. This tutorial describes it well.
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
Alternatively you can also store it in the raw folder and read it directly by specifying the path as the raw folder.
this can be played as:
int resID=getResources().getIdentifier(fname, "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
Here are some steps you can easily follow.
Open the android studio with the project in which you want to add-on audio clip/media file.
Create a raw folder in the resources folder.
Add media file to the raw folder by simply copy and paste that to the raw folder.
Here we added a media file “ring.mp3”. Now open the Java File of the desired activity, here we are adding the audio in the MainActivity.
Further add this code.
MediaPlayer ring = MediaPlayer.create(MainActivity.this, R.raw.ring);
ring.start();
Now run the App and your music will play when App starts
You should save the .mp3 into res/raw. AndroidStudio recognizes the raw folder. (By contrast, it does not automatically recognize a res/assets folder).
To play music.mp3:
mediaPlayer = MediaPlayer.create(ctx, R.raw.cat_meow);
mediaPlayer.start();
Note the convenient use of R. syntax.
Place it into your assets folder. Preferably under assets/raw/myfile.mp3
You can access it using:
String mp3File = "raw/music.mp3";
AssetManager assetMan = getAssets();
MediaPlayer media = new MediaPlayer();
FileInputStream mp3Stream = assetMan.openFd(mp3File).createInputStream();
media.setDataSource(mp3Stream.getFD());
media.prepare();
media.start();

Categories

Resources