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();
Related
I want to play Mp3 URL, i have used Media player for it. it is working but it's taking too much time to be prepare and start, i have referred to many sites but yet i had not got any satisfied solution.
I have stuck into this, So please guys help to solve it!!
My code look like as below
try {
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(mArrayList.get(0).MUSIC_URL);
player.prepare();
player.start();
seekbar.setMax(player.getDuration());
seekUpdation();
} catch (Exception e) {
// TODO: handle exception
}
Try to use
mMediaPlayer.prepareAsync();
if you see the doc, explanation will be like this
* Prepares the player for playback, asynchronously.
*
* After setting the datasource and the display surface, you need to either
* call prepare() or prepareAsync(). For streams, you should call prepareAsync(),
* which returns immediately, rather than blocking until enough data has been
* buffered.
In your case you are streaming data, so need to use prepareAsync()
Hope it helps
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}
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
i want to play sound when my condition is matched, but i don't how can i play sound or music in andengine?
help me to find my solution.
your answer will be awarded.
You can obviously use the MediaPlayer in AndEngine too, it works fine as it does in Android, because AndEngine is also a part of Android itself.
You can keep your file in res/raw/anyfile.mp3
MediaPlayer mediaPlayer = MediaPlayer
.create(getApplicationContext(), R.raw.anyfile);
try {
mediaPlayer.start();
mediaPlayer.setLooping(true);
} catch (Exception e) {
e.printStackTrace();
}
though i haven't perform a sound task, but hope this tutorial link will help u,,
http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html
I'm building an application that play streaming m3u file from web.
I'm using mediaPlayer class and it works.
Here's the code :
String test_path = "http://cast.idvps.com:8000/djwirya.m3u";
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(test_path);
mediaPlayer.prepareAsync();
} catch (IOException e) {Log.e("Error", "No Stream");}
mediaPlayer.start();
It was working perfectly. But, after a whie I compiled it again, there's no sound.
pls help.... THX
You need to call mediaPlayer.setOnPreparedListener(this) before the prepareAsync(). This assumes that your activity or whatever has implemented the OnPreparedListener interface. Then you need a callback called onPrepared() in which you can call mediaPlayer.start().
The other thing you need to do is make sure you call mediaPlayer.release() somewhere when your app is ending. Inside of onPause() is probably a good idea.
This is a solution.
Sorry I'm french but i'm think that should be ok with google translation.
Link for a solution