[英]AVAudioPlayer not playing sounds simultaneously
I am trying to play a couple of sounds files at the same time for the AVAudioPlayer. 我正在尝试同时播放AVAudioPlayer的几个声音文件。 I read a thread explaining how to do it @ AVAudioPlayer Help: Playing multiple sounds simultaneously, stopping them all at once, and working around Automatic Reference Counting . 我读了一个线程解释其操作方法。@ AVAudioPlayer帮助:同时播放多种声音,一次停止所有声音,并解决自动引用计数问题 。 It seems to be a weird issue as I when i try to only play one file at a time (when self.options.on is ON), it works, but when I try creating multiple instances (the else statements) and play them nothing plays. 当我尝试一次只播放一个文件(self.options.on为ON)时,这似乎很奇怪,但是我尝试创建多个实例(else语句)却不播放它们时播放。 I checked to see if the outputfile URL is correct and it is, so what is the reason that it isn't working? 我检查了输出文件URL是否正确,这是什么,所以它不起作用的原因是什么?
Thanks! 谢谢!
- (void)playTapped:(id)sender
{
if (!recorder.recording)
{
if(self.options.on)
{
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
[NSString stringWithFormat:@"sound%i.m4a",last],
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:nil];
NSLog(@"%@",player.url);
[player setDelegate:self];
[player play];
}
else
{
// trying to play all sounds at once
for (NSString *str in self.soundsCreated){
NSLog(@"%@",str);
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
[NSString stringWithFormat:@"sound%@.m4a",str],
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:nil];
NSLog(@"%@",audioPlayer.url);
[audioPlayer prepareToPlay];
[audioPlayer setDelegate:self];
[audioPlayer play];
}
}
}
}
NOTE I tried initializing with NSError
check but i'm not getting any errors: 注意我尝试使用NSError
检查进行初始化,但未收到任何错误:
NSError *sessionError = nil;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:&sessionError];
if (sessionError)
{
NSLog(@"Error in audioPlayer: %@",[sessionError localizedDescription]);
}
SOLUTION It seemed to be an issue with the initialization of my NSMutableArray
s. 解决方案我的NSMutableArray
的初始化似乎是一个问题。
I changed them from: 我将它们从:
self.soundsCreated = [[NSMutableArray alloc]init];
self.playerArray = [[NSMutableArray alloc]init];
to 至
self.soundsCreated = [NSMutableArray new];
self.playerArray = [NSMutableArray new];
and that fixed it 然后修复它
I have faced the same while played a small file: 播放小文件时,我也面临相同的问题:
Here is my solution (example) 这是我的解决方案(示例)
- (void)dropSoundWhileRefresh
{
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
[[AVAudioSession sharedInstance] setDelegate:self];
NSURL *musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"waterDrops" ofType:@"mp3"]];
dropSound = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[dropSound setDelegate:self];
[dropSound setVolume:0.10];
[dropSound setNumberOfLoops:0];
[dropSound play];
}
Instead of passing error as nil try this and check if initialization is working correct. 而不是通过nil传递错误,请尝试此操作并检查初始化是否正确。
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error)
{
DebugLog(@"Error in audioPlayer: %@",[error localizedDescription]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.