简体   繁体   English

在播放声音时禁用UIButton

[英]Disable a UIButton when a sound is playing AVAudioplayer

this is my first simple app, i am using Xcode 5 iOS7. 这是我的第一个简单应用程序,我正在使用Xcode 5 iOS7。

My problem is when i press the button a sound is playing but if i press the button when the sound is playing the sound starting from the beginning. 我的问题是,当我按下按钮时,声音正在播放,但是如果我在播放声音时,按钮则从头开始播放。 I don't know how to fix this, disable the button or if i can code something like this: 我不知道如何解决此问题,禁用按钮或是否可以编写类似以下内容的代码:

@implementation ViewController
AVAudioPlayer *audioPlayer;
BOOL playing;


if([audioPlayer isPlaying]){
  (playing = NO);
}

else if(playing == NO){
    [audioPlayer play];
    (playing = YES);
    }

but this doesn't work :) 但这不起作用:)

Here is the code that i using now. 这是我现在使用的代码。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
AVAudioPlayer *audioPlayer;


- (void)viewDidLoad

{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)playAudio:(id)sender {
NSURL *audioFile;
audioFile = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"docka" ofType:@"mp3"]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFile error:nil];
[audioPlayer play];
}
@end

Thanks for any help! 谢谢你的帮助!

You could call [yourButton setEnabled:NO]; 您可以调用[yourButton setEnabled:NO]; in your IBAction to play the audio, then call [yourButton setEnabled:YES]; 在您的IBAction中播放音频,然后调用[yourButton setEnabled:YES]; in the delegate method audioPlayerDidFinishPlaying:successfully: . 在委托方法audioPlayerDidFinishPlaying:successfully: First you'd have to set the delegate for your AVAudioPlayer, and adopt the protocol in your header file, but that should work. 首先,您必须为AVAudioPlayer设置委托,并在头文件中采用协议,但这应该可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM