Android - Save video internal storage and playback - android

I have saved the sample mp4 video in android internal storage in a file, But when try to read from file and playback in android videoview by parsing file path as a uri , am getting can't play this video error. I had checked many links but no use, How do i implement this.?
final String mFileName = "sample.mp4";
try{
FileOutputStream mFileOutputStream = openFileOutput(mFileName, Context.MODE_PRIVATE );
mFileOutputStream.write(getResources().getAssets().open("sample_video.mp4").read());
mFileOutputStream.close();
Log.v(TAG, "Success");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoPath(getFilesDir().getAbsolutePath()+"/sample.mp4");
mVideoView.start();

Simply you can't. VideoView internally uses a MediaPlayer, which explicitly requires a file to be world-readable (app's dirs aren't). The only way to bypass this limitation, as doc says, is to pass a File descriptor rather than the file path.
Unfortunately VideoView does not have a method like setDataSource(FileDescriptor), so you have two ways:
Access the VideoView's MediaPlayer through reflection and call setDataSource(FileDescriptor) on it. Not sure if this is a feasible solution, because the internal media player is instantiated when you call VideoView.setVideoPath, but you should not call it... in short, evaluate it, but it may be a dead end.
Use a MediaPlayer, set a surface holder on which play the video and pass a FileDescriptor directly to the MediaPlayer (imo this second solution is cleaner).

Related

how can i use MediaPlayer at android 7.0?

I need straming audio file in my project.
I try this code, it works at android 5.0 but dosen't work at 7.0.
String url = "http://sites.google.com/site/ubiaccessmobile/sample_audio.amr";
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.prepare() throw error
java.io.IOException: Prepare failed.: status=0x1
if i use audio file in resource, this code perfectly work.
but for streaming didn't.
i try prepareAsync(), prepare() in another thread. but same error exist.
of course, i use try-catch.
i think for runtime permisson, but this app need only INTERNET. and it didn't need runtime permisson.
what can i do??
android 7.0 cant use streaming audio???
please tell me problem, or another library.
I solve it....?
I cant know what the hell is it, if i use another audio format - like mp3, m4a - this code perfectly work.
why android 7.0 hate amr file???
You can use try catch for mediaPlayer.prepare() as:
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Playing a music file while downloading it

I'm developing a music download App that basically does webscraping across differents torrent platforms and get the torrents. I want to play some music torrent files while I'm downloading them. I have a Service that basically takes care of doing all this. I set a buffer that waits until the torrent file Its at least 25% downloaded, then I use the MediaPlayer class to start playing the file. The music starts playing OK, but It stops after a while, as if It couldn't play more than that 25%. I tried using both the Android's MediaPlayer class and the FFmpegMediaPlayer library. I tried using several configs:
MediaPlayer mediaPlayer = new MediaPlayer();
FileInputStream fis = null;
try {
fis = new FileInputStream(mCurrentTrack);
mediaPlayer.setDataSource(fis.getFD());
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();
} catch (Exception e) {
Log.w(APP_TAG, String.format("Something went wrong while trying to play the torrent stream: " + e.toString()));
}
mCurrentTrack is the path to the mp3 file that I downloaded into my Download folder in the external storage.
I tried using AudioManager.STREAM_MUSIC. I tried using the sync prepare() method and then calling the start() method. I triead opening the file with a new File(mCurrentTrack) before start playing It and setting It as readeable and writable. I tried many combinations, but always is the same, the music plays a few seconds and then stops. When I check the external storage, the file ends up being fully downloaded without trouble.
The library that I'm using to download the torrent files is this one TorrentStream-Android
Any tip or idea about how play this file while its being downloaded in the fly, would be appreciated.
As someone pointed out here. Using the ExoMedia player library solved the issue. Here's the snippet that I used to get it working:
compile 'com.devbrackets.android:exomedia:3.1.0'
final EMAudioPlayer mediaPlayer = new EMAudioPlayer (getApplicationContext());
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared() {
mediaPlayer.start();
}
});
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(mCurrentTrack));
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();

How does Android MediaPlayer Prepare() actually work?

I created an app in which I am using Android's mediaplayer to stream multiple video files from a HTTP source. I am using the Prepare() method instead of prepareAsync() since I can't continue unless something is being shown.
Here I have a simple method that return each of the mediaplayer instances:
MediaPlayer mediaPreparation(String filename) {
String url = "myURL"; // your URL here
// create mediaplayer instance
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
} catch (IOException e) {
}
mediaPlayer.setLooping(true);
// mediaPlayer.start();
return mediaPlayer;
}
And then I start them one by one using start(). Now I have two questions:
1- How does Prepare() really work? It takes a while for my app to start. Isn't it supposed to first receive a portion of the video stream (not download the whole) and then start playing?!
2- Can I manually buffer the video so I have more control over the buffer, and feed it into a mediaplayer to play? In specific, given a condition I need to start the video from a specific time. So probably I need to buffer the video stream from a specific time of the video.

Android: Sound File Not Found

I know there are lots of similar problems to mine, but for some reason the answers to those questions don't seem to work. I am using android and have a sound file named 'backgroundbeat.wav' and I'm trying to import it but instead I get the error:
"failed to open file 'res\raw\backgroundbeat.wav'. (No such file in directory)"
Here is the code where I use the media player:
mp = new MediaPlayer();
try{
mp.setDataSource("res/raw/backgroundbeat.wav");
mp.prepare();
mp.start();
}catch(Exception e){
e.printStackTrace();
}
Use your app package name to define the correct path :
mp.setDataSource("android.resource://<YOURPACKAGE_NAME>/raw/backgroundbeat");
example:
mp.setDataSource("android.resource://com.jorgesys.myapp/raw/backgroundbeat");
or other way to load a media file from /raw folder, if you are inside an activity you could use:
mp = MediaPlayer.create(this, R.raw.backgroundbeat);
or You can use a VideoView to play your file
Videoview on splash screen
Not much experienced in this but you can try to use mediaPlayer.setDataSource(FileDescriptor fd) instead of mediaPlayer.setDataSource(String path). See MediaPlayer setDataSource, better to use path or FileDescriptor? . Apparently if you call prepare() while using string as a parameter to setDataSource, it is causing some issues as per the answers.
You may try something like:
// Use the proper context instead of *context* variable
InputStream in = context.getResources().openRawResource(R.raw.backgroundbeat);
mediaPlayer.setDataSource(in.getFD())
Let me know if it helps.

Android Streaming Video using MediaPlayer

I'm back with another problem!
I'm trying to create an app that would list a selected Livestreams, from Own3d.TV, Justin.Tv etc...
If my research isn't totally failed, I can use the MediaPlayer object to Stream video, the only question is how do I use it?
So far my code looks like this, but it's giving me an Exception when trying to prepare the MediaPlayer.
public class Media extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SurfaceView sw = new SurfaceView(this);
SurfaceHolder sh = sw.getHolder();
setContentView(sw);
Uri ur = Uri.parse("http://www.twitch.tv/widgets/live_embed_player.swf?channel=hashe");
MediaPlayer mp = new MediaPlayer();
mp.setDisplay(sh);
try {
mp.setDataSource("http://www.twitch.tv/widgets/live_embed_player.swf?channel=hashe");
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
//mp.setDisplay(sw);
}
}
Is it even possible to Stream the video from these sites using the MediaPlayer?
If not, how shoud I approach this problem?
Thanks!
when implementing mediaPlayer, try something like this, it worked fine for me: http://android-er.blogspot.com/2010/11/play-3gp-video-file-using-mediaplayer.html
but I think, u cant use source like u have written in your code. You have to use some URL from where is video streamed directly with progresive streaming, not URL of html site with player embeded. You can recognize: when u write useful URL with streamed video in your internet browser, browser starts to download this streamed video in your computer. And I think, MediaPlayer supports in core .3gp or .mp4 format only..
I hope u understand my bad english
Use ExoPlayer instead of MediaPlayer. See Android official documentation:
ExoPlayer supports features like Dynamic adaptive streaming over HTTP (DASH), SmoothStreaming and Common Encryption, which are not supported by MediaPlayer. It's designed to be easy to customize and extend.
ExoPlayer - Android Developers

Categories

Resources