Pausing issue while looping a music for x number of times - android

Everything works perfectly, only problem is pausing. I wanted to repeat some long media for 5 times and also I wanted to pause the media in-between whenever I need.
But when I press pause/play button the playsound(); method is called again and again so the music plays more than 5 time.
This is my code
ImageButton bv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mp.isPlaying()){
mp.pause();
bv.setImageResource(R.drawable.play);
} else {
bv.setImageResource(R.drawable.pause);
playsound();
}
}
});
public void playsound() {
mp.setOnCompletionListener(new OnCompletionListener() {
int count = 1;
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
if(count < 5) {
count++;
mp.start();
mp.seekTo(0);
}
}
}); mp.start();
}
Any idea how to fix this?

Related

repeat the play of the music certain x number of time using android media player

In my application user selects the count like 1, 21, 51, 101, 108,like when he selected using buttons or radio button, the media player has to be repeated at number of time. If he selects 21, media player should repeat the playing of the music by 21 times. In my code, if I use while loop inside the for loop its blocking the UI, not allowing to stop or pause the play, if I did not use the while loop it plays only once. Please I need to solve it any one will be appreciated thanks in advance. Below I am posting my entire code
public class MainActivity extends Activity implements OnClickListener{
private MediaPlayer mp;
int count;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
findViewById(R.id.button_1).setOnClickListener(this);
findViewById(R.id.button_2).setOnClickListener(this);
findViewById(R.id.button_3).setOnClickListener(this);
findViewById(R.id.button_4).setOnClickListener(this);
findViewById(R.id.button_5).setOnClickListener(this);
findViewById(R.id.pause).setOnClickListener(this);
findViewById(R.id.stop).setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_1:
if (mp!=null) {
mp.release();
}
player(3);
break;
case R.id.button_2:
if (mp!=null) {
mp.release();
}
player(21);
break;
case R.id.button_3:
if (mp!=null) {
mp.release();
}
player(51);
break;
case R.id.button_4:
if (mp!=null) {
mp.release();
}
player(108);
break;
case R.id.button_5:
if (mp!=null) {
mp.release();
}
break;
case R.id.pause:
mp.pause();
break;
case R.id.stop:
mp.stop();
break;
}
}
private void player(int count) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(this, R.raw.a);
for (int i=1; i<=count ; i++){
mp.start();
while(mp.isPlaying());
}
// mp.release();
}
#Override
protected void onDestroy() {
if(null!=mp){
mp.release();
}
super.onDestroy();
}
}
you can achieve this by setting a yourCount integer that has the numbers you are going to repeat your sound and than simply do this
int count = 0;
mp.setOnCompletionListener(new OnCompletionListener()
#Override
public void onCompletion(MediaPlayer mp) {
if (count < yourCount) {
mp.start();
count++;
}
}
});
mp.start();
try this, Hope it helps
mp.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
// Count here and restart if required.
}});

How to detect the playpause button click on mediacontroller in android

1.What steps I will reproduce the problem?
I tried to detect the playpause button in android default media controller, I can able detect the seek bar changes (get the seeking video position) using
vv.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
// TODO Auto-generated method stub
long cuntPost=mp.getCurrentPosition();
}
});
}
});
2. I also tried to implement the Mediacontroller.MediaPlayerControll interface this is also not work, how can I listen the playpause button in media controller which listener I can use?
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
//play next // repeat // shuffle?
}
});
Button lButton = new Button(getApplicationContext());
lButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//play or pause logic
if(mMediaPlayer.isPlaying())
{
mMediaPlayer.pause();
}
else
{
mMediaPlayer.start();
}
}
});
//call to check current state
mMediaPlayer.isPlaying();
If you want to handle the UI not by yourself, you can refer to MediaController api.
http://developer.android.com/reference/android/widget/MediaController.html
Which gives you a UI and a Control object to control the MediaPlayer.
http://developer.android.com/reference/android/widget/MediaController.MediaPlayerControl.html

How to avoid IllegalStateException with MediaPlayer

I have a stop button and a play button. When I click the play button, the audio plays fine and when I press stop, the audio stops. However, if I accidently hit the stop button twice in a row, an IllegalStateException gets thrown and force closes. How can I keep this force close from happening. Essentially what I want is that if someone accidently hits the stop button twice, the program does nothing. Here is the code:
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alphabet);
Button play = (Button) findViewById(R.id.button1);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getBaseContext(), R.raw.e_1100);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
});
Button stop = (Button) findViewById(R.id.button2);
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mp != null && mp.isPlaying()) {
mp.stop();
mp.release();
}
else if (mp.isPlaying() == false) {
// What do I put here???
}
}
});
}
}
try this
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}

How to play audio and wait to finish to countinue to play again?

How to play audio and wait to finish to countinue to play again?? . In this i can play so many time :D
ivSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(context, "Hello", 1000).show();
mMediaPlayer = MediaPlayer.create(context, sound);
if (mMediaPlayer.isPlaying() == false) {
mMediaPlayer.start();
mMediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
};
});
}
}
});
The code is working fine for every button click. If you wish the audio to be repeated automatically, use
mMediaPlayer.setLooping(true);
If you wish that when Ist sound is completely played then, second one should start on a button click,
try disabling ur button click while the first sound is playing and then enable the click onCompleteListener... Some thing like this:
ivSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(context, "Hello", 1000).show();
mMediaPlayer = MediaPlayer.create(context, sound);
ivSelect.setClickable(false); //Disabling the button click
mMediaPlayer.start();
}
Enable click on Audio completion
mMediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
ivSelect.setClickable(true);
}
});
Try this
if(mMediaPlayer.isPlaying()){
mMediaPlayer.stop();
mMediaPlayer.release();
}

Android button click to play music, click again to stop sound

I have a button, when I click it plays music, how to do it, when I click second time, to stop the music?
Button two = (Button)this.findViewById(R.id.button2);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.two);
two.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp2.start();
}
});
Ok, this one works:
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.n);
one.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp2.pause();
}
else {
mp2.start();
}
;
}});
The one with Pause above it works, but If I want to stop the music, it does not work.
Following not working:
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp2.stop();
}
else {
mp2.start();
}
;
}});
I get error: start called in state 0
error (-38, 0)
According to http://developer.android.com/reference/android/media/MediaPlayer.html, I suppose you could do something like this:
Button two = (Button)this.findViewById(R.id.button2);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.two);
two.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// If the music is playing
if(mp2.isPlaying() == true)
// Pause the music player
mp2.pause();
// If it's not playing
else
// Resume the music player
mp2.start();
}
});
You can actually write just
if(mp2.isPlaying())
instead of
if(mp2.isPlaying() == true)
It's just for the sake of understanding what is going on.
You could have a boolean check to see if it is started (and set the boolean to false) and if so stop the music, if not start it (and set the boolean to true). Something like:
public void onClick(View v)
{
if(musicPlaying == false)
{
mp2.start();
musicPlaying = true;
}
else
{
mp2.stop();
musicPlaying = false;
}
}
if(mp2.isPlaying()) {
mp2.pause();
} else {
mp2.start();
}
This is what I used to start and stop the music, you must prepare the MediaPlayer for playback. Note: this will start the music from where it stopped.Mediaplayer prepare() methodIf you are streaming music you should use prepareAsync ()
musicButton = (Button) findViewById(R.id.btn_dj_player);
musicButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mp.isPlaying()) {//check if a song is already playing
mp.stop();
try {
mp.prepare();//get the mediaplayer reeady for playback
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
mp.start();
}
}
});
If you have only one sound and you want it to start or stop with only one button than this may help .
Button button_name = (Button)this.findViewById(R.id.Your_button_id_Here);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.YOur_audio_File_name_here);
button_name.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(mp.isPlaying())
{
mp.pause();
}
else
{
mp.start();
}
}
});
Have you tried using the seekTo() function?
Something like:
if (mp.isPlaying()) {
mp.stop();
mp.prepareAsync();
mp.seekTo(0);
} else {
mp.start();
}
}
I am just guessing here, but might be worth looking into. :-)
I accomplished this as follows:
I declared a boolean variable:
boolean isButtonClicked = false;
Then I set up an if else in a method like so:
initPlaySound()
{
if(isButtonClicked)
{
player.start();
} else{
player.stop();
}
Lastly I set up an onClick of the button within the method:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
isButtonClicked = !isButtonClicked;
initPlaySound();
}
});
Basically, when the button is first clicked the boolean is set to the opposite of its' current state (false to true) and the code within the if executes. Once the button is clicked again the boolean is set to the opposite again (true to false) and the sound stops.
I solved this problem in a manual way. If sound is playing, i wrapped sound into beginning(seekTo(0)), then paused the sound. If sound is already paused, only seekTo(0) is called.
public void onClick(View v) {
if(mp.isPlaying()){
mp.seekTo(0);
mp.pause();
}
else{
mp.seekTo(0);
}
} ;
Add a global variable boolean flag=false;
if(flag==false)
{
mp=MediaPlayer.create(this, R.raw.abc);
mp.start();
playbutton.setText("Pause");
flag=true;
}
else if(mp.isPlaying()&&flag==true)
{
mp.pause();
playbutton.setText("Play");
flag=false;
}
This code will work if you want to use the same button as PLAY/PAUSE in your app. Hope this helps. Add this code in the button onClick() function which you are using to play or pause.

Categories

Resources