Reputation: 3234
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
i did
[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
Please advice .
Thanks for help.
Thanks
Upvotes: 0
Views: 2118
Reputation: 5210
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;
}
Upvotes: 3
Reputation: 13224
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.
Upvotes: 1
Reputation: 2840
do you mean:
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];
}
is correct?
Upvotes: 0