我正在制作一个需要录制来自蓝牙耳机的声音并在 iPhone 扬声器(而不是蓝牙耳机扬声器)上播放的应用程序。我试过了:
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setDelegate:self];
[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
[audioSession setActive: YES error: nil];
// set up for bluetooth microphone input
UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,sizeof (allowBluetoothInput),&allowBluetoothInput);
// problem if this is not zero
// check the audio route
UInt32 size = sizeof(CFStringRef);
CFStringRef route;
OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
NSLog(@"route = %@", route);
之后我想将音乐文件播放到 iPhone 扬声器,我使用了代码:
- (IBAction)playAudio:(id)sender {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
UInt32 size = sizeof(CFStringRef);
CFStringRef route;
OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
NSLog(@"route = %@", route);
// NSLog(@"route = %@", route);
//[self playAudiokam];
if (!_audioRecorder.recording)
{
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:_audioRecorder.url
error:&error];
_audioPlayer.delegate = self;
if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
[_audioPlayer play];
}
}
我的代码在这里工作,但不是同时工作。应用程序需要扬声器继续播放音乐并从蓝牙耳机录制声音。