[英]Disable a UIButton when a sound is playing AVAudioplayer
這是我的第一個簡單應用程序,我正在使用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
謝謝你的幫助!
您可以調用[yourButton setEnabled:NO];
在您的IBAction中播放音頻,然后調用[yourButton setEnabled:YES];
在委托方法audioPlayerDidFinishPlaying:successfully:
。 首先,您必須為AVAudioPlayer設置委托,並在頭文件中采用協議,但這應該可行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.