在 iOS 设备上同时播放声音

发布于 2024-11-01 05:29:09 字数 1409 浏览 0 评论 0原文

我在一个视图中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷不放纵 2024-11-08 05:29:09

这可能会导致问题:

if (theAudio) [theAudio release];

AVAudioPlayer 释放后无法继续播放。

基本上会发生什么:
- 点击按钮
- 音频已初始化
- 音频开始播放
- 单击另一个按钮
- 音频被释放并因此停止播放
- theAudio 用另一种声音初始化
- 音频开始播放

This probably causes the problem:

if (theAudio) [theAudio release];

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

鸠魁 2024-11-08 05:29:09

看起来你的 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文