我正在为iPhones制作一个报警应用程序,并希望继续循环音频,直到按钮再次被按下。到目前为止,它所做的只是播放音频一次时,按下。下面是代码:
-(IBAction)PlayAudioButton:(id)sender {
AudioServicesPlaySystemSound(PlaySoundID);
}
- (void)viewDidLoad {
NSURL *SoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, &PlaySoundID);
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}有什么建议吗?
发布于 2015-03-31 20:49:12
使用AVAudioPlayer播放声音。您必须将AVFoundation.framework添加到您的项目中,才能使其工作。首先声明一个AVAudioPlayer对象。必须将其声明为具有strong属性的属性。
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;或作为具有__strong属性的实例变量
@interface Class : SuperClass //or @implementation Class
{
AVAudioPlayer __strong *audioPlayer;
}然后,为了加载和播放文件,
- (void)viewDidLoad
{
NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"wav"];
NSURL *audioFileURL = [NSURL fileURLWithString:audioFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];
audioPlayer.numberOfLoops = -1; //plays indefinitely
[audioPlayer prepareToPlay];
}
- (IBAction)PlayAudioButton:(id)sender
{
if ([audioPlayer isPlaying])
[audioPlayer pause]; //or "[audioPlayer stop];", depending on what you want
else
[audioPlayer play];
}当你想停止播放声音时,打电话
[audioPlayer stop];https://stackoverflow.com/questions/29377393
复制相似问题