I\'m making an radio like application that uses streaming. Here i need to stream the audio from a link (http://somedomain/some.pls
).
I have created
You can try download pls file, parse the urls that are in there, and put them in MediaPlayer.
Mediaplayer api from android supports streaming a .pls file, but the API is not best option & the documentation is not well. The life cycle diagram given in the official documentation gives valuable information but may be confusing at a first glance.
A sample code snippet:
MediaPlayer mp;
mp=MediaPlayer.create(getApplicationContext(),Uri.parse(url))
Example url of .pls file http://50.xx.xxx.xx:xx40/)
mp.start();
mp.pause();
mp.release() (or mp.reset() as applicable)
http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context, android.net.Uri)
There are listeners/callback available with MediaPlayer api but it is really problematic to a App developer working on audio streaming.
The static constructor used in the code snippet is suggested/better approach to create the media object with URI (like http url with host and port.) But the call back functions can't be used by developer because the prepare is called by the constructor itself.
The created object can be played/stopped in a asyn thread (AsyncTask api).
The android official documentation does not give any 'get method' to get the status of the mediaplayer.
I welcome android app developers to talk on this;
and I wish frame work developers provide more accurate documentation - how much it supports or what's importance & vision of Google Inc and Android framework development team on Mediaplayer API for audio streaming with urls.
If some need help from me or share their experience let's please talk on MediaPlayer api for audio streaming @ http://stackoverflow.com or android developers blog (android-developers.blogspot.com).
Regards Sree Ramakrishna Program Manager & Android developer @ New Mek Solutions,Hyderabad.
You can use the file streaming ability available on the MediaPlayer class to do the above.
I have not tested the following code, but it should be something on these lines:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(http://somedomain/some.pls);
mp.prepare();
mp.start();
But remember to do the following:
1. Surround each of the statements above with try
and catch
statements and print copiously to LogCat
. It will help debug easily.
2. Release the resources once you are done with it.
HTH,
Sriram
Man, try this two things:
1: the code
mediaPlayer = MediaPlayer.create(this, Uri.parse("http://vprbbc.streamguys.net:80/vprbbc24.mp3"));
mediaPlayer.start();
2: the .pls file
This URL is from BBC just as an example. It was an .pls file that on linux i downloaded with
wget http://foo.bar/file.pls
and then i opened with vim (use your favorite editor ;) and i've seen the real URLs inside this file. Unfortunately not all of the .pls are plain text like that.
have fun!