Toggle between play/pause UIButtons - ios

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

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;
}

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.

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?

Related

UIButton placed as a UIBarButttonItem at the the Navigation bar won't trigger method when tapped

I have created a custom UIButton and placed it at the right hand side of the navigation controller. But when I tapped on it, it doesn't trigger the buttonTapped method and printout Testing 123, what have I done wrong?
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIImage *btnImage = [[UIImage alloc] init];
BOOL unlike = NO;
if (unlike){
btnImage = [UIImage imageNamed:#"ic_favorite_border_48pt.png"];
}else{
btnImage = [UIImage imageNamed:#"ic_favorite_48pt.png"];
}
UIButton *heart = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect listButtonFrame = heart.frame;
listButtonFrame.size = btnImage.size;
heart.frame = listButtonFrame;
[heart setImage:btnImage forState:UIControlStateNormal];
[heart addTarget:self.navigationController.parentViewController
action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton = [[UIBarButtonItem alloc] initWithCustomView:heart];
self.navigationItem.rightBarButtonItem = jobsButton;
}
-(void)buttonTapped:(UIButton*)heart{
NSLog(#"Testing 123");
}
#end
[heart addTarget:self.navigationController.parentViewController
action:#selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
Probably needs to be
[heart addTarget:self
action:#selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];

button on contentOverlayView not responding

I cannot work this out - it should be simple.
I have:
ShowAVPlayer method
AVPlayerItem *video = [[AVPlayerItem alloc] initWithURL:url];
AVQueuePlayer *queue = [[AVQueuePlayer alloc] initWithItems:#[video]];
video = [[AVPlayerItem alloc] initWithURL:url];
[queue insertItem:video afterItem:nil];
AVPlayer *player = [AVPlayer playerWithURL:url];
self.avmovieplayer = [AVPlayerViewController new];
I want to add a button to my overlay - but the button is not responding :
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:#"Do stuff" forState:UIControlStateNormal];
btn.frame = CGRectMake(0, 330, 320, 40);
[btn addTarget:self action:#selector(DoSpeech:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:true];
[self.avmovieplayer.contentOverlayView addSubview:btn];
Here is my DoSpeech which is never being called:
- (void) DoSpeech:(UIButton *)button {
//DoSpeech
}
Try this,
[btn setUserInteractionEnabled:YES];
[self.avmovieplayer.contentOverlayView bringSubviewToFront:btn];
Hope it will help.

Getting white flash animation effect upon clicking play and pause buttons

I have a single button toggling between play and pause for audio playback. When I was using the standard play/pause buttons (e.g., initWithBarButtonSystemItem:UIBarButtonSystemItemPlay), I got a nice white flash animation effect over the buttons as they toggled back and forth. But now that I am using custom images, I no longer get that effect. How can I get it back?
Also would very much appreciate any tips to simplify this as it seems heavyweight. I saw https://stackoverflow.com/a/9104587/1462372 but am not clear whether that is applicable in this case (in button bar).
Here is the code:
- (void) setAsPlaying:(BOOL)isPlaying
{
self.rootViewController.playing = isPlaying;
// we need to change which of play/pause buttons are showing, if the one to
// reverse current action isn't showing
if ((isPlaying && !self.pauseButton) || (!isPlaying && !self.playButton))
{
UIBarButtonItem *buttonToRemove = nil;
UIBarButtonItem *buttonToAdd = nil;
if (isPlaying)
{
buttonToRemove = self.playButton;
self.playButton = nil;
UIImage *pauseButtonImage = [UIImage imageNamed:#"icon_pause.png"];
UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pauseButton addTarget:self action:#selector(pauseAudio:) forControlEvents:UIControlEventTouchUpInside];
pauseButton.bounds = CGRectMake(0, 0, pauseButtonImage.size.width, pauseButtonImage.size.height);
[pauseButton setImage:pauseButtonImage forState:UIControlStateNormal];
self.pauseButton = [[UIBarButtonItem alloc] initWithCustomView:pauseButton];
buttonToAdd = self.pauseButton;
}
else
{
buttonToRemove = self.pauseButton;
self.pauseButton = nil;
// this was the way I was doing it before:
// self.playButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(playAudio:)];
UIImage *playButtonImage = [UIImage imageNamed:#"icon_play.png"];
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton addTarget:self action:#selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
playButton.bounds = CGRectMake(0, 0, playButtonImage.size.width, playButtonImage.size.height);
[playButton setImage:playButtonImage forState:UIControlStateNormal];
self.playButton = [[UIBarButtonItem alloc] initWithCustomView:playButton];
buttonToAdd = self.playButton;
}
// Get the reference to the current toolbar buttons
NSMutableArray *toolbarButtons = [[self.toolbar items] mutableCopy];
// Remove a button from the toolbar and add the other one
if (buttonToRemove)
[toolbarButtons removeObject:buttonToRemove];
if (![toolbarButtons containsObject:buttonToAdd])
[toolbarButtons insertObject:buttonToAdd atIndex:5];
[self.toolbar setItems:toolbarButtons];
}
}
Try this code might be helpful to you...
UIImage* image = [UIImage imageNamed:#"facebook.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height-10);
UIButton *rightbtnItem = [[UIButton alloc] initWithFrame:frame];
[rightbtnItem setBackgroundImage:image forState:UIControlStateNormal];
[rightbtnItem setShowsTouchWhenHighlighted:YES];
Copy and paste it in your viewDidload and see glow effect on touch.
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"ar.png"] forState:UIControlStateNormal];
[button setFrame:CGRectMake(0, 0, 30, 30)];
button.center=self.view.center;
[button setBackgroundImage:[UIImage imageNamed:#"RS2kQ.png"] forState:UIControlStateHighlighted];
Get Image from here ar.png and RS2kQ.png

UIButton won't change picture when HighLighted, will only set picture to grey

I want the picture to turn yellow the second the user touches it. Right now on first tap the image turns a bit grey and on release turns yellow.
Second tap the picture would go back to red instantly. How can i fix this?
-(void)bMethod:(UIButton*)sender
{
sender.selected = !sender.selected;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
UIImage *buttonImageSelected = [UIImage imageNamed:#"yellow.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchDown];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImageSelected forState:UIControlStateSelected];
[myButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
I ran your code and it's working fine mate. Just double check the image names!
EDIT 1:
replace your code with the followings:
-(void)bMethod:(UIButton*)sender
{
if (sender.imageView.image != [UIImage imageNamed:#"red.png"]){
[sender setImage:[UIImage imageNamed:#"yellow.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:#"yellow.png"] forState:UIControlStateNormal];
}
else{
[sender setImage:[UIImage imageNamed:#"red.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:#"red.png"] forState:UIControlStateNormal];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchDown];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];
}
EDIT 2:
-(void)bMethod:(UIButton*)sender
{
sender.selected = !sender.selected;
sender.highlighted = !sender.highlighted;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
UIImage *buttonImageSelected = [UIImage imageNamed:#"yellow.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchUpInside];
myButton.adjustsImageWhenHighlighted = NO;
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImageSelected forState:UIControlStateSelected];
[myButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
}

iOS: change png in a bar button item

I put in a tolbar a bar button item, and I set it with a png (first.png) and when I push it I want to change its png in "second.png"
This code doesn't work fine:
UIImage *first = [UIImage imageNamed:#"first.png"];
UIImage *second = [UIImage imageNamed:#"second.png"];
if ([sender isSelected])
{
[sender setImage:first forState:UIControlStateNormal];
[sender setSelected:NO];
}
else
{
[sender setImage:second forState:UIControlStateSelected];
[sender setSelected:YES];
}
The current answerers do not realize that UIBarButtonItem does NOT inherit from UIButton, so setImage:forState: will most definitely not work. UIBarButtonItems cannot be set for different states. You can, however, utilize something like this (declared in the UIBarItem docs):
sender.image = [UIImage imageNamed:#"first.png"];
I'm not clear on why you have an if block in there. Can't you initialize both state images as a one-off initialization step? I.e.
[sender setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateSelected];
In fact, this can probably be done in Interface Builder if you don't need to do it programmatically.
Just write after creation of button this lines:
UIImage *first = [UIImage imageNamed:#"first.png"];
UIImage *second = [UIImage imageNamed:#"second.png"];
[sender setImage:first forState:UIControlStateNormal];
[sender setImage:second forState:UIControlStateSelected];
You don't need if-else statement.
Updated:
CGRect myFrame;
UIButton *myButton = [[UIButton alloc] initWithFrame:myFrame];
[myButton setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateSelected];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[myButton release];

Resources