我想也许最快的方式就是使用Sound Services。这是非常有效的,但我需要按顺序播放声音,而不是重叠。因此我使用回调方法来检查声音何时结束。该循环产生大约0.3秒的滞后。我知道这听起来非常严格,但它基本上是程序的主轴。
编辑:我现在尝试使用AVAudioPlayer
,但是我不能在不使用audioPlayerDidFinishPlaying
的情况下播放序列中的声音,因为这会使我处于与SoundServices的回调方法相同的情况。 / p>
EDIT2:我想如果我能以某种方式加入我想要播放到大文件中的部分声音,我可以让整个音频文件连续发声。
EDIT3:我认为这样可行,但音频重叠:
waitTime = player.deviceCurrentTime;
for (int k = 0; k < [colores count]; k++)
{
player.currentTime = 0;
[player playAtTime:waitTime];
waitTime += player.duration;
}
由于
答案 0 :(得分:4)
我刚尝试了一种技术,我认为这种方法对您有用。使用连接的声音构建音频文件。然后构建一些关于你的声音的元数据:
@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"); }];
答案 1 :(得分:0)
我假设您有时想要更改序列或省略声音。 (否则你只需要连续三个声音来构建资产并播放它。)
可能有一个更好的想法,但为了让事情变得非常紧张,您可以考虑生成连锁资产,预先加载它 - 将所有延迟提升到一个负载,然后寻找它来改变声音