我想也许最快的方法是使用Sound Services。它非常高效,但我需要按顺序播放声音,而不是重叠。因此,我使用了一个回调方法来检查声音何时结束。这个周期会产生大约0.3秒的延迟。我知道这听起来很严格,但它基本上是程序的主轴。
编辑:我现在尝试使用AVAudioPlayer
,但如果不使用audioPlayerDidFinishPlaying
,我将无法按顺序播放声音,因为这将使我处于与使用SoundServices的回调方法相同的情况。
EDIT2:我认为,如果我能以某种方式将我想要播放的声音的各个部分连接到一个大文件中,我就可以让整个音频文件连续发出声音。
EDIT3:我认为这会起作用,但音频重叠:
waitTime = player.deviceCurrentTime;
for (int k = 0; k < [colores count]; k++)
{
player.currentTime = 0;
[player playAtTime:waitTime];
waitTime += player.duration;
}
谢谢
发布于 2012-06-26 03:12:27
我刚刚尝试了一种我认为对你很有效的技术。创建一个声音连接在一起的音频文件。然后构建一些关于你的声音的元数据,如下所示:
@property (strong, nonatomic) NSMutableDictionary *soundData;
@synthesize soundData=_soundData;
- (void)viewDidLoad {
[super viewDidLoad];
_soundData = [NSMutableDictionary dictionary];
NSArray *sound = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.0], [NSNumber numberWithFloat:0.5], nil];
[self.soundData setValue:sound forKey:@"soundA"];
sound = [NSArray arrayWithObjects:[NSNumber numberWithFloat:6.0], [NSNumber numberWithFloat:0.5], nil];
[self.soundData setValue:sound forKey:@"soundB"];
sound = [NSArray arrayWithObjects:[NSNumber numberWithFloat:7.0], [NSNumber numberWithFloat:0.5], nil];
[self.soundData setValue:sound forKey:@"soundC"];
}
第一个数字是文件中声音的偏移量,第二个是持续时间。然后让你的球员准备好像这样打球。
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog(@"%@", [error description]);
else {
[audioPlayer prepareToPlay];
}
}
然后你可以像这样构建一个低级的声音播放方法...
- (void)playSound:(NSString *)name withCompletion:(void (^)(void))completion {
NSArray *sound = [self.soundData valueForKey:name];
if (!sound) return;
NSTimeInterval offset = [[sound objectAtIndex:0] floatValue];
NSTimeInterval duration = [[sound objectAtIndex:1] floatValue];
audioPlayer.currentTime = offset;
[audioPlayer play];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
[audioPlayer pause];
completion();
});
}
你可以像这样快速组合播放声音。
- (IBAction)playAB:(id)sender {
[self playSound:@"soundA" withCompletion:^{
[self playSound:@"soundB" withCompletion:^{}];
}];
}
与嵌套块不同,您可以构建一个更高级别的方法,该方法接受声音名称的列表并逐个播放它们,如下所示:
- (void)playSoundList:(NSArray *)soundNames withCompletion:(void (^)(void))completion {
if (![soundNames count]) return completion();
NSString *firstSound = [soundNames objectAtIndex:0];
NSRange remainingRange = NSMakeRange(1, [soundNames count]-1);
NSArray *remainingSounds = [soundNames subarrayWithRange:remainingRange];
[self playSound:firstSound withCompletion:^{
[self playSoundList:remainingSounds withCompletion:completion];
}];
}
就像这样叫它...
NSArray *list = [NSArray arrayWithObjects:@"soundB", @"soundC", @"soundA", nil];
[self playSoundList:list withCompletion:^{ NSLog(@"done"); }];
发布于 2012-06-19 00:05:28
我假设你有时想要改变顺序或者省略发音。(否则,您将使用所有三种声音在一行中构建资源并播放该资源)。
可能有一个更好的想法,但为了让事情变得非常紧凑,你可以考虑生成连接的资产,预加载它-将所有延迟移到那个加载,然后绕过它来改变声音。
https://stackoverflow.com/questions/11092657
复制相似问题