我在一个视图上有 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];
}
缺少一些重要的代码,但看起来好像theAudio
是您用来管理声音播放的全局变量。由于每当播放声音时都会销毁它,因此当前正在播放的任何内容都会停止以准备下一个声音。
有几种方法可以解决这个问题,其中一种是每种声音都有自己独特的AVAudioPlayer
实例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)