I'm developing internet radio using android MediaPlayer. I need to update UI when the song is over and new song begins. How to find, that song is over?
This is my current code. mMediaPlayer.start(); calls when user tap the button.
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mMediaPlayer.setDataSource(streamURL);
mMediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
Radio streaming is endless so the streaming is never going to end, however if what you can do is track the metadata in the radio streaming for the currently playing song, and if you detect that something has changed, then do something.
Something more important that I want to point is that MediaPlayer is awful slow for radio streaming, I developed a radio streaming app myself so I strongly reccomend you to use this library for this, for two reasons, one is faster and has a bigger buffer than the MediaPlayer (MediaPlayer class has at some point a hardcoded byte[] buffer = new byte[4096]; buffer that'll bring you some issues) and second because this library lets you handle the metadata in a more confortable way.
Related
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.
I have to 2 songs.I used radio buttons for both songs so when select first it should play me the first song and when i click on second. Second song should be played.I have used Play,pause and stop button so when i select first song and click play first song should be played.how to use media player for 2 songs.Previously i used 2 media players for different songs. How to use one media player.
Previously I have used this statements for two songs
mediaPlayer = MediaPlayer.create(getApplicationContext(),R.drawable.inno);
mediaPlayer1 = MediaPlayer.create(getApplicationContext(),R.drawable.rocky);
My question is i want to use only one media player for both songs
You better stop being lazy and search for the solution on your own aswell. #1Up pretty much answered your question. For your second question: This is Uri
Keep using two mediaplayers, in cases like this there is no problem doing that. Using only one reference would mean you'd have to recreate it each time you want to change clip, or stopping it and calling setDataSource(context, URI).
If you use only one reference to mediaplayer the user will have to wait for the clip to be ready each time it has to be played, while in your implementation both sound clips are ready to be played at any time.
Anyways, here is a setDataSource example:
MediaPlayer mp = MediaPlayer.create(context, firstSongUriOrRes);
public void play(int clip)
{
if(mp.isPlaying()) //Stop the mediaplayer if it's already playing
mp.stop();
switch(clip) //Choose the clip to be played
{
case 0:
mp.setDataSource(context, firstSongUriOrRes);
break;
case 1:
mp.setDataSource(context, secondSongUriOrRes);
break;
}
mp.prepare();
mp.start(); //Start the mediaplayer
}
Another way to use setDataSource is to place the audio files inside the asset directory and use this code:
AssetFileDescriptor fd = context.getAssets().openFd("pathInsideAssets/fileName");
mp.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength());
I am using simple MediaPlayer to stream a radio streaming..
try {
MediaPlayer media = new MediaPlayer();
media.setAudioStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE);
media.setDataSource("http://indiespectrum.com:9000");
media.prepareAsync();
media.start();
}
catch(Exception e)
{
//Getting Exception
}
This works fine but the audio played is somewhat slow then the original audio streamed i.e if a song is originally at 2:15 the mediaplayer is playing at 2:05 .There is around 10 sec difference .Why is there a difference and what can be done to remove this?
Due to login confusion, i'm repeating this question. If any moderator sees this, i'd like to keep this one current, as I no longer can access my former login.
A bit of a problem has presented itself to me. I am trying to play a sound continously looping in my android app.
With MediaPlayer: MP3 plays alright, but there is a gap at the end which does not exist in the file. I read it had to do with the decoder, and that ogg should work. Tried using ogg, but still get the gap, which is definitely not on the file.
With SoundPool classes and ogg (using this fellow's interesting class: http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html),
the sound starts, and a fraction of a second later, it restarts. so I get a stutering half a second of the beginning of every file, without advancing further, because it is always going back to the beginning.
Is there something really wrong with media player and it's ability to loop audio? How about the freakishy stuttering soundpool?
Thank you very much for any assistance!
Note: Karthi_Heno suggested I do this:
MediaPlayer mediaPlayer;
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = new MediaPlayer();
try {
AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor = assetManager.openFd("music.ogg");
mediaPlayer.setDataSource(descriptor.getFileDescriptor(),
descriptor.getStartOffset(), descriptor.getLength());
mediaPlayer.prepare();
mediaPlayer.setLooping(true);
} catch (IOException e) {
textView.setText("Couldn't load music file, " + e.getMessage());
mediaPlayer = null;
}
However, when i do this, getassets gives filenotfound, even though there IS a filein assets. any thoughts either on thisassets problem, or my audio loop question?
thanks. Yep, i'm an android newb alright, can't even get sound to loop alright.
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();