在 iOS 设备上同时播放声音
我在一个视图中有 10 个声音。它们都会播放,但一次只能播放一种声音。我想要它,这样你就可以点击你想要播放的声音,并且它们都会同时播放。但当你按下一个声音时,它会播放,然后你再按下另一个声音,声音就会停止。
这是我用于声音的代码
- (IBAction)oneSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];
if (theAudio) [theAudio release];
NSError *error = nil;
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(@"%@",[error localizedDescription]);
theAudio.delegate = self;
[theAudio play];
}
- (IBAction)twoSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"wav"];
if (theAudio) [theAudio release];
NSError *error = nil;
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(@"%@",[error localizedDescription]);
theAudio.delegate = self;
[theAudio play];
}
- (IBAction)threeSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"wav"];
if (theAudio) [theAudio release];
NSError *error = nil;
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(@"%@",[error localizedDescription]);
theAudio.delegate = self;
[theAudio play];
}
谢谢!
I have 10 sounds in one view. And they all play, however only one sound can play at a time. I want it so you can tap the sounds you want to play, and they all play at the same time. But at the moment when you press a sound it plays, then you go to press another sound and the sound previously stops.
Here is the code which I have used for the sounds
- (IBAction)oneSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];
if (theAudio) [theAudio release];
NSError *error = nil;
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(@"%@",[error localizedDescription]);
theAudio.delegate = self;
[theAudio play];
}
- (IBAction)twoSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"wav"];
if (theAudio) [theAudio release];
NSError *error = nil;
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(@"%@",[error localizedDescription]);
theAudio.delegate = self;
[theAudio play];
}
- (IBAction)threeSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"wav"];
if (theAudio) [theAudio release];
NSError *error = nil;
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(@"%@",[error localizedDescription]);
theAudio.delegate = self;
[theAudio play];
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能会导致问题:
AVAudioPlayer 释放后无法继续播放。
基本上会发生什么:
- 点击按钮
- 音频已初始化
- 音频开始播放
- 单击另一个按钮
- 音频被释放并因此停止播放
- theAudio 用另一种声音初始化
- 音频开始播放
This probably causes the problem:
The AVAudioPlayer cannot continue playing after it's released.
What basically happens:
- Button click
- theAudio is initialized
- theAudio starts playing
- Another button click
- theAudio is released and therefor stops playing
- theAudio is initialized with another sound
- theAudio starts playing
看起来你的 theAudio 是全局的,你在每个方法中再次实例化它。将其转换为局部变量或定义 3(或您需要的数量)。
Seems like your theAudio is global and you instantiate it again in each method. Transform it in a local variable or define 3 (or how many you need).