Alternar entre reproducir / pausar UIButtons

i have created two UIButtons and only play button is appearing on the UIToolbar. In the play method i want is when play button is pressed it shows pause button and if user presses pause button it pauses the audiofile and then shows the play button.

UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];

[playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];

playButton.frame = CGRectMake(0, 0, 50, 50);

UIImage *image = [UIImage imageNamed:@"play.png"];

[playButton setImage:image forState:UIControlStateNormal];

UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playButton];



UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];

 [pauseButton addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];

pauseButton.frame = CGRectMake(0, 0, 50, 50);

UIImage *imge = [UIImage imageNamed:@"pause.png"];

[pauseButton setImage:imge forState:UIControlStateNormal];

-(void)play:(id)sender 
{

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"theme" 
                                                     ofType:@"mp3"];

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];

audioPlayer = [[AVAudioPlayer alloc] 
              initWithContentsOfURL:fileURL error:nil];

audioPlayer.currentTime = 0;

[audioPlayer play];

[fileURL release];  

}

I need help in implementing this

In the play method

yo si

[audioPlayer play];

UIImage *imge = [UIImage imageNamed:@"pause.png"];

[pauseButton setImage:imge forState:UIControlStateNormal];

[audioPlayer pause];

UIImage *image = [UIImage imageNamed:@"play.png"];

[playButton setImage:image forState:UIControlStateNormal];

[audioPlayer Play];

I think i m doing wrong way

Por favor aconséjame .

Gracias por ayudar.

Muchas Gracias

preguntado el 01 de febrero de 12 a las 22:02

This is a little confusing, you have two buttons. Are you trying to have the play and pause replace each other ? ['>' changes to '||']? -

yes when play button is hit it should play the music and replace the play button with the pause button and when pause button is hit it should pause the music and and should show the play button -

3 Respuestas

Use only one button with 1 image for each state (Selected and Normal):

UIButton *mediaButton = [UIButton buttonWithType:UIButtonTypeCustom];
mediaButton.frame = CGRectMake(0, 0, 50, 50);

[mediaButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[mediaButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateSelected];

[playButton addTarget:self action:@selector(mediaAction:) forControlEvents:UIControlEventTouchUpInside];

-(void)mediaAction:(UIButton *)sender 
{
   if(sender.selected){
    // pause action
   } else {
    // play action
   }

    sender.selected = !sender.selected;
}

Respondido 02 Feb 12, 02:02

You do not have to create two separate buttons. Just create one button and one method. Just keep the state in a boolean variable or use player state. Do what you need to do after cheking the state in method.

However if you insist on creating two separate buttons, you can hide and show one of the buttons.

Respondido 02 Feb 12, 02:02

how i can keep the state in a boolean variable or use player state - user1120133

define a bool variable out of your method and invert it at the end of your method. myBoolVariable=!myBoolVariable; - erkanyildiz

Quieres decir:

if([audioPlayer isPlaying]) {
    UIImage *imge = [UIImage imageNamed:@"pause.png"];
    [pauseButton setImage:imge forState:UIControlStateNormal];
    [audioPlayer pause];
}
else {
    UIImage *image = [UIImage imageNamed:@"play.png"];
    [playButton setImage:image forState:UIControlStateNormal];
    [audioPlayer Play];
}

¿es correcto?

Respondido 02 Feb 12, 02:02

yes that is what i have but getting message like unused variable imge - user1120133

You get that warning beacuse you don't use it as you don't need. - erkanyildiz

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.