一次播放多个声音? [英] Playing multiple sounds at once?

查看:123
本文介绍了一次播放多个声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个视图上有6种声音.

I have 6 sounds on one view.

但是我想要它一次播放多个,所以您点击声音1(正在播放声音1),然后播放声音2.当声音1仍在播放时.

However I want it so that I can play more than one at once, So you tap sound 1 (sound 1 is playing) then sound 2 plays. while sound 1 is still playing.

但是现在我按下声音1(播放声音1),按下声音2(播放声音2,但是声音1停止了)

But at the moment I press sound 1 (sound 1 plays), press sound 2 (sound 2 plays, but sound 1 stops)

这是音频部分的代码.

- (IBAction)oneSound:(id)sender; {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];
    if (theAudio) [theAudio release];
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];   
    volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateVolume) userInfo:nil repeats:YES];
}

- (IBAction)twoSound:(id)sender; {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"wav"];
    if (theAudio) [theAudio release];
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];   
    volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateVolume) userInfo:nil repeats:YES];
}

推荐答案

缺少一些重要的代码,但看起来theAudio是用于管理声音播放的全局代码.由于您在播放声音时都会销毁它,因此当前正在播放的内容都会停止,以准备下一个声音.

There is some important code missing, but it looks as if theAudio is a global you are using to manage the playing of your sounds. Since you destroy it whenever a sound is played whatever is currently playing will stop in preparation for the next sound.

有几种解决方法,一种是每种声音都有自己独特的AVAudioPlayer实例.

There are several ways to fix this, one being that each sound gets its own unique AVAudioPlayer instance.

这篇关于一次播放多个声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆