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