好的,所以我试图查找它,所以它不是重复的,但我可能错过了一些东西。无论如何,在我的应用程序中,我有一首歌曲应该在我录制时开始播放。所以当我点击录音时, AVAudioRecorder 开始录音,我已经初始化的 AVAudioPlayer 开始播放歌曲。然而这首歌的音量变得非常安静。我知道这不是歌曲,因为如果我只是播放歌曲而不尝试同时录制,它会以最大音量播放。有帮助吗?谢谢。
我如何初始化:
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44010.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:"Rang De"
ofType:@"mp3"]];
audioPlayerForPreloadedMusic = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else
{
audioPlayerForPreloadedMusic.delegate = self;
[audioPlayerForPreloadedMusic prepareToPlay];
}
我怎么玩:
-(void) recordAudio
{
if (!audioRecorder.recording)
{
playButton.enabled = NO;
stopButton.enabled = YES;
[audioRecorder record];
if(!audioPlayerForPreloadedMusic.playing)
[audioPlayerForPreloadedMusic play];
else {
NSLog(@"music is playing, so won't play");
}
}
}