这是我的第一个简单应用,我正在使用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
感谢您的任何帮助!
您可以在IBACTION中调用[yourButton setEnabled:NO];
播放音频,然后在委托方法中调用[yourButton setEnabled:YES];
audioPlayerDidFinishPlaying:successfully:
。首先,您必须为Avaudioplayer设置委托,并在标题文件中采用协议,但这应该有效。