0

我需要以编程方式单击一个按钮:[play sendActionsForControlEvents:UIControlEventTouchUpInside]; 但是当我这样做时,void 被调用了两次!我需要启动一个 AVPlayer(在 ViewDidLoad 中),但音乐也开始了两次。请帮忙 :)。

播放代码:

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

1 回答 1

1

怎么样:

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

现在无需以编程方式点击按钮,只需调用 playMusic 或 pauseMusic。

于 2013-10-04T20:20:08.470 回答