这是我的第一个简单的应用程序,我正在使用Xcode 5 iOS7。
我的问题是当我按下按钮时正在播放声音,但是如果我在声音从头开始播放声音时按下按钮。我不知道如何解决这个问题,禁用按钮或者如果我可以编写这样的代码:
@implementation ViewController
AVAudioPlayer *audioPlayer;
BOOL playing;
if([audioPlayer isPlaying]){
(playing = NO);
}
else if(playing == NO){
[audioPlayer play];
(playing = YES);
}
但这不起作用:)
这是我现在使用的代码。
#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
感谢您的帮助!
答案 0 :(得分:0)
您可以在IBAction中调用[yourButton setEnabled:NO];
播放音频,然后在委托方法[yourButton setEnabled:YES];
中调用audioPlayerDidFinishPlaying:successfully:
。首先,您必须为AVAudioPlayer设置委托,并在头文件中采用该协议,但这应该有效。