matthijs2704
matthijs2704

Reputation: 110

UIButton gets clicked twice

I need to programmatically click a button: [play sendActionsForControlEvents:UIControlEventTouchUpInside]; But when I do that the void was called twice! I need to start a AVPlayer (in ViewDidLoad), but the music started twice too. Please help :).

The play code:

- (IBAction)playStream:(id)sender  {
    NSLog(@"%@", @"PlayStream clicked: ", sender);
        if(clicked == 0) {
        clicked = 1;
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
        [play setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
        [playerr play];
    }
    else {
        [playerr stop];
        [play setImage:[UIImage imageNamed:@"Start"] forState:UIControlStateNormal];
        [audio setActive:NO error:nil];
        clicked = 0;
    }
} 

Upvotes: 0

Views: 563

Answers (1)

Mercurial
Mercurial

Reputation: 2165

How about:

@implementation MyClassName{
     BOOL musicPlaying;
}

- (IBAction)playStream:(id)sender  {
    if(musicPlaying){
        [self pauseMusic];
    }else{
        [self playMusic];
    }
}

-(void)playMusic{
    musicPlaying= YES;
    [play setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    [playerr play];
}

-(void)pauseMusic{
    musicPlaying= NO;
    [playerr stop];
    [play setImage:[UIImage imageNamed:@"Start"] forState:UIControlStateNormal];
    [audio setActive:NO error:nil];
}

Now instead of tapping the button programmatically, just call playMusic or pauseMusic.

Upvotes: 1

Related Questions