一次播放多个声音?
我在一个视图上有 6 个声音。
然而我想要它,这样我就可以一次播放多个声音,所以你点击声音 1(声音 1 正在播放),然后声音 2 播放。当声音 1 仍在播放时。
但此时我按声音1(声音1播放),按声音2(声音2播放,但声音1停止)
这是音频部分的代码。
- (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];
}
I have 6 sounds on one view.
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.
But at the moment I press sound 1 (sound 1 plays), press sound 2 (sound 2 plays, but sound 1 stops)
Here is the code for the audio part.
- (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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
缺少一些重要的代码,但看起来好像
theAudio
是您用来管理声音播放的全局变量。由于每当播放声音时都会破坏它,因此当前正在播放的任何内容都会停止以准备下一个声音。有多种方法可以解决此问题,其中一种是每个声音都有自己唯一的
AVAudioPlayer
实例。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.There are several ways to fix this, one being that each sound gets its own unique
AVAudioPlayer
instance.不要仅仅因为播放器存在就释放它。玩完后释放
您应该在应用程序委托或应用程序委托的属性中实现此功能,以便在弹出视图时委托引用仍然有效。
您不需要对每个声音都执行此操作。每个声音都会使用不同的文件名或 URL 来初始化播放器。你可以通过确保玩完后释放来获得这一点。另一件需要注意的事情是,如果玩家因为中断而没有完成,则释放玩家。
don't release the player just because it exists. release when finished playing
You should implement this in the app delegate or a property of the app delegate so that the delegate reference remains valid if you pop the view.
You should not need to do this for each voice. Each voice will initial the player with a different filename or URL. You gain this by making sure that you will release when finished playing. The other thing to take care of is releasing players if the player doesn't finish because of an interruption.
您可以声明并使用:
而不是仅释放和重用一个 AVAudioPlayer。
You can declare and use:
instead of releasing and reusing just one AVAudioPlayer.