我刚刚尝试了一种我认为对你很有效的技术。建立一个连接你的声音的音频文件。然后构建一些关于你的声音的元数据,如下所示:
@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"); }];