同时播放两个音乐
同时播放两个音乐,多线程控制的,适合一个做背景一个前景的混合。
- (void)viewDidLoad
{
[superviewDidLoad];
NSData *sound1 = [NSDatadataWithContentsOfFile:[[[NSBundlemainBundle] bundlePath] stringByAppendingPathComponent:@"01.mp3"]];
NSData *sound2 = [NSDatadataWithContentsOfFile:[[[NSBundlemainBundle] bundlePath] stringByAppendingPathComponent:@"01.mp3"]];
NSOperationQueue *queue = [[NSOperationQueuealloc] init];
NSError *error = nil;
AVAudioPlayer *player1 = [[AVAudioPlayeralloc] initWithData:sound1 error:&error];
AVAudioPlayer *player2 = [[AVAudioPlayeralloc] initWithData:sound2 error:&error];
NSInvocationOperation *op1 = [[[NSInvocationOperationalloc] initWithTarget:selfselector:@selector(playSounds:) object:player1] autorelease];
NSInvocationOperation *op2 = [[[NSInvocationOperationalloc] initWithTarget:selfselector:@selector(playSounds:) object:player2] autorelease];
[queue addOperation:op1];
[queue addOperation:op2];
}
- (void)playSounds:(id)data
{
AVAudioPlayer *player = (AVAudioPlayer *)data;
[player prepareToPlay];
[player play];
}