I make one app in android, my problem is that when i play the stream it is playing well but when i click on stop button and then click on play button then song is not playing, how to do that ? please help me if anybody know. below is code that i write :-
// method for play stream after stop it.
public void startradio(View v) {
try{
if(mp.isPlaying()){
return;
}
mp.start();
} catch(IllegalStateException ex){
ex.printStackTrace();
}
}
// method for stop stream.
public void stopradio(View v) {
if(mp.isPlaying()){
mp.stop();
}
mp.release();
}
#Eluvatar's comment is pretty close but no cigar.
If you do that, your app still would be in the wrong state, and would need to call prepare() before calling start() again. You should look at the event cycle for MediaPlayer.
In other words a possible change would be
// method for play stream after stop it.
public void startradio(View v) {
try{
if(mp.isPlaying()){
return;
}
mp.prepare();
mp.start();
} catch(IllegalStateException ex){
ex.printStackTrace();
}
}
// method for stop stream.
public void stopradio(View v) {
if(mp.isPlaying()){
mp.stop();
}
}
Related
I am getting list of mp3 files(as a ArrayList(Path)) from local by choosing one by one but I could not able to make them to play one by one play if finished move to next and if finished all list loop again. any help?
I solved issue thansk to : http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-i
ofcourse I did a little bit modification, like:
private void playSong() {
try {
mp.reset();
mp.setDataSource(playlist.get(currentPosition));
mp.prepare();
mp.start();
// Setup listener so next song starts automatically
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
nextSong();
}
});
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
private void nextSong() {
if (++currentPosition >= playlist.size()) {
// Last song, just reset currentPosition
currentPosition = 0;
} else {
// Play next song
playSong(/*MEDIA_PATH + playlist.get(currentPosition)*/);
}
}
I have my code which plays music from the internet. My problem is it has a few delay and causes lag after clicking the button.
I have tried to use the prepareAsync() but no luck. Can anyone please let me know the issue.
#Override
public void onClick(View v) {
if (v.getId() == R.id.ButtonTestPlayPause) {
/** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
try {
mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer.
} catch (Exception e) {
e.printStackTrace();
}
mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
buttonPlayPause.setImageResource(R.drawable.button_pause);
} else {
mediaPlayer.pause();
buttonPlayPause.setImageResource(R.drawable.button_play);
}
primarySeekBarProgressUpdater();
}
}
I am building an app which plays several videos, and I have two different user scenarios :
Scenario 1. While video 'A' is playing, if user clicks next button, then it stops and play the next video 'B'.
Scenario 2. Play video 'A', and if it's done, user clicks next button and it plays video 'B'.
For the first scenario, I used mediaPlayer.isPlaying() method to detect if it is in Started state and it works fine. However, if I use the same code for the second scenario, isPlaying() throws IllegalStateException.
Here's my code for playing videos :
private void playVideos(SurfaceHolder holder) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(holder);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + video_files[mCounter]);
mediaPlayer.setDataSource(this, uri);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (mCounter <= 8) {
onVideoCompletion(mediaPlayer);
} else {
mediaPlayer.stop();
mediaPlayer.release();
}
}
});
} catch (IOException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalStateException e) {
}
}
Also, here's my button listener to play next video :
nextBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
mCounter += 1;
if (mCounter <= 8) {
playVideos(holder);
}
}
});
One way that I tried to hack this issue was using a boolean variable instead of isPlaying() method. For example,
boolean mIsPlaying = false;
...
// in button listener
if(mIsPlaying) {
mediaPlayer.stop();
mediaPlayer.release();
}
...
// in playVideos() function
mediaPlayer.start();
mIsPlaying = true;
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (mCounter <= 8) {
onVideoCompletion(mediaPlayer);
} else {
mediaPlayer.stop();
mediaPlayer.release();
}
}
});
That works for my both of scenario, but I'm not sure if it's the correct way to do it. Isn't there any way to detect whether mediaPlayer is in Stopped state?
I took a look at Google's Documentation which you can find here. You can only know if the player isPlaying(); or isLooping(); ... So no, there is not an "easy" or "short" way to achieve what you want. Hope it helped.
So I have mediaplayer which plays audio files from the phone. To play them i use this code for the Play button
try {
if (mp2.isPlaying()) {
mp2.reset();
}
mp2.setDataSource(s1); //using path here
mp2.prepare();
mp2.start();
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method
mp.reset();
}
});
} catch (Exception e) {
}
I want to pause the song and continue to play it with the same code, I tried mp2.pause();
but if i click on play button nothing will happen. I dont wanna to remove if (mp2.isPlaying()) {
mp2.reset();
}
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();
}
});
}