Radio streaming doesnot match the original streaming in android? - android

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?

Related

All MP3 media from an array is not playing properly: Android Studio

I have created an activity, in which different mp3 sounds have to be played onFling a TextView. I created an array with all media required. Sometimes this plays all the media properly but most of the time it stop playing in between.
private int[] voice ={R.raw.media1,R.raw.media2,R.raw.media3,R.raw.media4,R.raw.media5,R.raw.media6,R.raw.media7,R.raw.media8};
media = MediaPlayer.create(BaseActivity.this, voice[num]);
media.start();
Updating here as I have got the solution. I was facing such issues as I was not releasing the recourses. After using below code, issue resolved:
private int[] voice ={R.raw.media1,R.raw.media2,R.raw.media3,R.raw.media4,R.raw.media5,R.raw.media6,R.raw.media7,R.raw.media8};
if(media!=null){
media.stop();
media.release();
media=null;
}
media = MediaPlayer.create(BaseActivity.this, voice[num]);
media.start();

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.

Stream radio on Android

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.

sounds not playing with mediaplayer after playing some sounds

I'm developing an android application which is collection of 100 sound effects.
After I play for instance 25 of the sounds, I can't play anymore and I have to close the application and wait for some minutes then open the application and now I can play other sounds.
final MediaPlayer mp81 = MediaPlayer.create(MainActivity.this, R.raw.a81);
I play the sound using the below code:
mp81.start();
I stop the playing sound using the below code:
mp81.seekTo(0);
I also used stop() method but the problem were still existing.
is there any other method i have to add?
Please note: consider using SoundPool for playing short sounds.
Regarding your use-case: you initialize your MediaPlayer instance using the static create() method which means you create a new MediaPlayer object for each sound instead of reusing an existing instance and just changing the data source. This might negatively affect the performance of your app. I suggest that you create an array of paths to your sounds and then instantiate the MediaPlayer like this:
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(yourArray[x]);
mp.prepare();
mp.start();
} catch(Exception e){
e.printStackTrace();
}
Consult the MediaPlayer Document for more information.

One Android MediaPlayer Object plays several sounds at once

I have ONE object MediaPlayer mediaplayer. I use it to play different sounds, one after another.
mediaplayer = MediaPlayer.create(context, ResIdMusicONE);
mediaplayer.start();
// some user input
mediaplayer.release();
mediaplayer = null;
// some other user input
mediaplayer = MediaPlayer.create(context, ResIdMusicTWO);
mediaplayer.start();
// some user input
mediaplayer.release();
mediaplayer = null;
Sometimes is works fine. But sometimes the two sounds are played at the same time. And at positions, where mediaplayer should already have been released and be equal null.
Thanks for the help.
If I were you I would use the SoundPool class for this. With SoundPool you can set the number of streams to play at the same time, so by setting that to 1 you can just call play() over and over and the most recent call to play() will be the only sound that you hear.
Take a look at my post a while back. It has an example of the SoundPool class in the question.
Edit:
Have you tried creating a new instance and calling the prepare() every time you want to start a new sound?
mediaplayer = new MediaPlayer();
mediaplayer.setDataSource(path);
mediaplayer.prepare();
mediaplayer.start();
Though actually now that I think about it, I'm pretty sure you only need to do that if you are using a file from the sdcard not from your resources... Hmmm.

Categories

Resources