0
我想在特定時間段內運行3個音頻文件,然後停止它們。我可以同時播放所有3個audop文件,不用擔心,但是當我嘗試阻止它們時,我收到了這個錯誤。AVAudioPlayer在呼叫停止時崩潰
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController stopMusic:]: unrecognized selector sent to instance
這就是我的代碼看起來像播放文件。
- (void)backgroundTap {
[customerNameTextField resignFirstResponder];
[customerEmailTextField resignFirstResponder];
if ((audioPlayer) || (audioPlayer2) || (audioPlayer3)) {
NSLog(@"still playing");
} else {
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(stopMusic:)
userInfo:nil
repeats:NO];
// create audio session
[[AVAudioSession sharedInstance] setDelegate: self];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
NSLog(@"Error setting category! %@", setCategoryError);
// path
NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"tuiSong" ofType:@"mp3"];
NSURL *soundURL = [NSURL URLWithString:soundPath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer setVolume: 0.1];
// path
NSString *soundPath2 =[[NSBundle mainBundle] pathForResource:@"beachSong" ofType:@"mp3"];
NSURL *soundURL2 = [NSURL URLWithString:soundPath2];
NSError *error2;
audioPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL2 error:&error2];
audioPlayer2.numberOfLoops = 0;
[audioPlayer2 setVolume: 0.06];
// path
NSString *soundPath3 =[[NSBundle mainBundle] pathForResource:@"CicadaSong" ofType:@"mp3"];
NSURL *soundURL3 = [NSURL URLWithString:soundPath3];
NSError *error3;
audioPlayer3 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL3 error:&error3];
audioPlayer3.numberOfLoops = 5;
[audioPlayer3 setVolume: 0.05];
// play bells
if (!audioPlayer) {
NSLog(@"localizedDescription : %@", [error localizedDescription]);
} else {
if (![audioPlayer isPlaying]) {
[audioPlayer play];
}
if (![audioPlayer2 isPlaying]) {
[audioPlayer2 play];
}
if (![audioPlayer isPlaying]) {
[audioPlayer3 play];
}
}
}
}
然後這就是我所做的停止音頻。
- (void)stopMusic {
if ([audioPlayer isPlaying]) {
[audioPlayer stop];
audioPlayer = nil;
}
if ([audioPlayer2 isPlaying]) {
[audioPlayer2 stop];
audioPlayer2 = nil;
}
if ([audioPlayer3 isPlaying]) {
[audioPlayer3 stop];
audioPlayer3 = nil;
}
}
任何幫助將不勝感激。
哎呦......那真的很愚蠢......我想我需要去吃飯。大聲笑對不起,感謝你的幫助 – HurkNburkS