Playing a music file while downloading it - android

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();

Related

Android MediaPlayer can not play the audio file online but work well if load the same one from local

Some thing weird happened when I use android MediaPlayer to play some audio stream from service.Here is my audio stream url:
http://7xk2r8.com1.z0.glb.clouddn.com/rNeDSn1Octc
When I use it with MediaPlayer like this:
mediaPlayer.setDataSource("http://7xk2r8.com1.z0.glb.clouddn.com/rNeDSn1Octc")
The onCompleteListener will be called immediately
But if I download the file from above url to android device,and use MediaPlayer to load it from local like this:
mediaPlayer.setDataSource("local path")
Everything works fine!
I think the bugs may from the stream file itself,but I don't know why,I have been lost in it for about 2 weeks,Help!!!!!!
PS:I used the most simple code for MediaPlayer,So I think it's fine with my code,I am sure that the bugs from audio stream file itself,could you test the file?The file URL above is totally public.Thanks!
I got same problem. Mediaplayer sometimes can not load cause of network proxy etc. You could call prepareAsync() but it will never invoke to onPrepared(). So I add a boolean flag like follow:
#Override
public void onPrepared(MediaPlayer mp) {
isPrepared = true;}
#Override
public void onCompletion(MediaPlayer mp) {
if(isPrepared) {// Do your code}

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 MediaPlayer OnPreparedListener

I am working on a simple app and using a MediaPlayer to play some background noise in 1 activity. I am reading up on MediaPlayer and am not sure whether or not to implement an OnPreparedListener to trigger the start() method. What are the pros / cons to each approach?
Approach 1:
mediaPlayer = MediaPlayer.create(context, R.raw.sound);
mediaPlayer.setLooping(true);
mediaPlayer.start();
Approach 2:
mediaPlayer = MediaPlayer.create(context, R.raw.sound);
mediaPlayer.setLooping(true);
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
As per the docs, calling start() is effective when you are playing locally available resources for which the MediaPlayer does not require to fetch the data and process it for playing. For example playing audio resources from raw folder.
If you are trying to play a resource from remote source, its a better practice to go for OnPreparedListener() because it might involve fetching and decoding media data.
So, if you know for sure, that your resource is locally available and is of short length, go for Approach 1. Otherwise Approach 2 would be suitable.
Ideally, I prefer this.
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.prepareAsync();
The MediaPlayer has always been tricky for me to work with. So, I would recommend you to start with the developer docs. Go through it, understand the state diagram. I am sure it will help you in solving lot of questions which you are yet to come across.
Let's say you are playing a video from internet. If you start directly the player it would crash because it may be not ready to play because of some internet problems or something else. But if you use preparedlistener then it will not start the player until it is ready to play.
Therefore it is good to have onpreparedlistener so your activity does not crash or misbehaves.

How to play online radio in android

I am working on android application in which i have play online radio streaming.
i have gone through the media player classes but i don't think is there any method to online streaming of radio. If any know about this please help me.
Thank You.
Vikram
Vikram,
You should be able to achieve this using the MediaPlayer; however, depending on your format it may be difficult. For example, if you're trying to play an online radio stream that uses .pls, or .m3u, you would have to parse that file, and pull out the true URLs to use.
Beyond that, you should be able to use MediaPlayer's create method with a URL to start streaming playback. Keep in mind that if the streams URL redirects (which it likely does) you may have to resolve the URL. A simple way to do this is use HttpURLConnection to open a connection, then connect(), then getURL(). You'll likely need a string url, so call toExternalForm() on the result from getURL().
Additionally, If things aren't working for you with MediaPlayer via URL, you might have to come up with your own buffering mechanism to get the data from the server. That being the case, you can try this tutorial: http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/
From what I've read, you should just be able to do:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(streamingURL);
mediaPlayer.prepare();
mediaPlayer.start();
to get basic functionality I believe, but I haven't tested it myself.
the easiest way to play a radio channel in android is to use the built in MediaPlayer, however when the datasource is from web the prepare() method takes a long time to execute and you should use prepareAsync() instead to avoid blocking the ui:
player = new MediaPlayer();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
player.start();
}
}
});
try {
player.setDataSource(currentChannelUrl);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
return;
}
player.prepareAsync();

How to play an audio from url in a app widget

Im implementing app widget to play audio from url,
I used mediaplayer class to play audio when click on a play button in widget.
but im not getting audio output,
is there any way to work out on this issue.
Please let me know, or give me a reference to follow
Thanks in advance
I hope the following code will help you to play audio from URL.
MediaPlayer mp = new MediaPlayer();
mp.reset();
try {
mp.setDataSource(context, Uri.parse("http:\\your url\test.mp3"));
mp.setLooping(false); // Set looping
mp.prepare();
mp.start();
}
catch(Exception e){
e.printStackTrace();
}

Categories

Resources