I am using a simple code to stream a radio from url like this:
player = new MediaPlayer();
try {
player.setDataSource("XYZ.com/streaming link");
player.prepare();
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
player.start();
ma.MyProgressdis();
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But when screen Orientation changes the media player stops for a while and start streaming again. I have tried using onConfigurationChanged method but I am confused about what could I do to continue the streaming without pausing .
Related
I'm loading audio sounds into a listview, I play them.When the audio playing is done, i want to change that specific sound's pause button to play button again.
here is my code to play sounds
soundChild.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
player.setDataSource(url);
// file.close();
player.prepare();
player.setLooping(false);
// player.start();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (player.isPlaying()) {
if (player != null) {
player.pause();
// Changing button image to play button
soundChild.setImageResource(R.drawable.playbutton);
}
} else {
// Resume song
if (player != null) {
player.start();
// Changing button image to pause button
soundChild.setImageResource(R.drawable.pausebutton);
}
}
}
});
player.setOnCompletionListener(new OnCompletionListener() {
#Override public void onCompletion(MediaPlayer mp) { // TODO
// Auto-generated method stub player.release(); player=null;
Toast.makeText(_context, "complete", Toast.LENGTH_LONG).show();
soundChild.setImageResource(R.drawable.playbutton);
player.reset();
} });
But on complete listner the imageview of player button is never changed back to play imageview again. I doin't know where i'm doing wrong, because if onComplete is being called then imageview background should also change.please help.
Usually You have to call requestLayout before:
soundChild.requestLayout();
soundChild.setImageResource(R.drawable.playbutton);
and then after changing the image
soundChild.invalidate();
What else could You try? If it doesn´t work, You can also try:
soundChild.setImageDrawable(context.getResources().getDrawable(R.drawable.playbutton));
Because, like described in the API for setImageResource:
This does Bitmap reading and decoding on the UI thread, which can
cause a latency hiccup
Also, what it is possible, You have to call it in on ui thread:
runOnUiThread(new Runnable() {
#Override
public void run() {
soundChild.setImageResource(R.drawable.playbutton);
}
});
and call this in onCompletion.
I have added two buttons to my app, one to play and one to start. As soon as i start the app the songs starts playing automatically. when i press on stops it stops but when i press on play it doesnt starts again
To stop I have used mediaplayer.stop() which is working fine
To start I have used
mediaplayer.start()
which is not working
According to flow diagram its given we need to use
prepare()
and then
OnPreparedListener.onPrepared()
and then
start()
I don't know how to use these functions. Please help me
Don't call stop() for stopping the mediaplayer use reset() instead. I dont have good reason for that. But, it'll work for sure.
// for stopping it call below statement
if(mediaPlayer.isPlaying())
mediaPlayer.reset();
//for playing it again
mediaPlayer.prepare();
mediaPlayer.start();
For starting media player again use following code.
if (mediaplayer != null) {
mediaplayer.start();
}
and one more thing, instead of using
mediaplayer.stop()
use
mediaplayer.pause();
so it will pause the current song instead of stop.
Just check the condition whether it is null or not.
Try following code on Play Button
mp.reset();
mp.setDataSource(song path);
mp.prepare();
mp.start();
mp.setDataSource(song path); not necessary if you playing same sound again
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
btnsound=(Button)findViewById(R.id.play_sound);
btnstopsound=(Button)findViewById(R.id.stop_sound);
mp=new MediaPlayer();
btnsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mp.setDataSource(Sound);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
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.setLooping(true);
}
});
btnstopsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.setLooping(false);
mp.stop();
}
});
}
I've a problem with seek bar, when i play the audio with mediaplayer the seekbar don't start with it.
i don't understand why the seekbar don't "refresh" progress.
thank you for help.
below i post the code, i don't understand when i wrong.
MediaPlayer player=new MediaPlayer();
SeekBar seekBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
AssetFileDescriptor afd = this.getResources().openRawResourceFd(R.raw.mymp3);
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
player.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Button pausa=(Button)findViewById(R.id.button2);
pausa.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
player.pause();
}
});
Button vai=(Button)findViewById(R.id.button3);
vai.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
player.start();
}
});
int tutto=player.getDuration();
seekBar = (SeekBar)findViewById(R.id.seekBar1);
seekBar.setMax(tutto);
seekBar.setProgress(player.getCurrentPosition() / 1000);
}
An easy way you can do it is to create a Timer object that fires every second.
Inside the code of the timer you call
seekBar.setProgress(player.getCurrentPosition() / 1000);
Additionally, you may want to implement setOnCompletionListener to get notified when the playback has reached it's end so you can stop the timer. The timer should be stopped either if the playback reached it's end, the applications finishes, or you stop the music manually (in response to a stop button, for example)
Because you set the progress in onCreate() and that gets called only once - you guessed it, when the Activity is created. I'm not very familiar with the MediaPlayer so I don't know if there is some kind of update listener, but you need some kind of timer that updates the progress every second or so.
I am making an app that uses sound effects. I would like to know how i can detect when a sound has finished playing and trigger an event .
i tried this :
player = new MediaPlayer();
if (player != null) {
player.reset();
player.release();
}
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
sonar.setBackgroundResource(R.drawable.sonar_off);
stopPlay();
}
});
if(state==1){
player = MediaPlayer.create(this, R.raw.sonar_slow);
}else if(state==2){
player = MediaPlayer.create(this, R.raw.sonar_medium);
}else if(state==3){
player = MediaPlayer.create(this, R.raw.sonar_fast);
}
try{
player.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();
You can set an OnCompleteListener() to the player object. The code for it is:
mPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp)
{
"your code comes here"
}
});
Here the mPlayer is the object of the MediaPlayer which is currently running.
There are multiple ways to play sounds, assuming you use the MediaPlayer class you could register a MediaPlayer.OnCompletionListener
I have a MediaPlayer object through which i am playing a sound on click of a button.the sound file is 1-2 second long only.When i am clicking the corresponding button one after another(quickly), it's not playing the sound twice(it is playing just once.I tried with some code like this :
public void onClick(View view) {
if(DrawSound.isPlaying()) {
DrawSound.stop();
DrawSound.prepareAsync();
}
DrawSound.start();
}
it's not wokring.Search a lot, but could make it work.Any help!!!!
Try using prepare instead of prepareAsync
This is because you are not releasing media player after its completed playing your sound ... use release and reset method in Media Players setOnCompletionListener Method...
you can use it like this ...
if(mp.isPlaying()) {
try {
mp.stop();
mp.reset();
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mp.start();
}
or you can use it ...
mp.stop();
mp.prepareAsync();
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
This is working fine for me.
Hey guys if someone can get help from this:
just solve my problem with a trick(after all trick can do all :p)
i took 2 MediaPlayer object and one MediaPlayer reference and wrote like this :
soundPlayer = DrawSound.isPlaying() ? DrawSound1 : DrawSound;
soundPlayer.start();
Try this and see if it works:
if(DrawSound.isPlaying()) {
DrawSound.stop();
DrawSound.prepareAsync();
DrawSound.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
DrawSound.start();
}
});
}