问题描述
我有一个项目,我必须录制来自蓝牙耳机的声音并使用默认的 iPhone 扬声器播放.我搜索了很多,得到了这个代码.
I have a project in which I have to record the voice coming from bluetooth headset and play with default iPhone speaker. I have searched a lot and got this code.
UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
sizeof (allowBluetoothInput),
&allowBluetoothInput
);
------------ 录音机启动和停止的代码 ------------
------------ CODE FOR AUDIO RECORDER START AND STOP ------------
- (IBAction)Record: (id)sender
{
UIButton *btn = (UIButton *)sender;
if([btn isSelected])
{
[audioRecorder stop];
[btn setSelected:NO];
[btn setTitle:@"Start Recording" forState:UIControlStateNormal];
}
else
{
[audioRecorder record];
[btn setSelected:YES];
[btn setTitle:@"Stop Recording" forState:UIControlStateNormal];
}
}
并且我在此之后使用avaudiorecorder.这里似乎还有一些我遗漏的东西.
and i am using avaudiorecorder after this. There seems to be something else I am missing in here.
--------录音机代码---------
-------- Code for audio recorder ---------
NSURL *soundFileURL = [NSURL fileURLWithPath:AUDIO_FILE];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
我想我遗漏了一些需要在这里添加的东西.我只想要蓝牙耳机输入音频.任何帮助将不胜感激.
I think i am missing something else which needs to be added here. I just want the bluetooth headset input audio. Any help would be appreciated.
提前致谢!!
推荐答案
我刚刚查看了您的问题并得到了很好的答案,您想尝试使用波纹管代码:-
i just review your issue and got nice answer something you want try with Bellow code:-
// create and set up the audio session
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;
OSStatus stat = AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
sizeof (allowBluetoothInput),
&allowBluetoothInput
);
NSLog(@"status = %x", stat); // 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);
// if bluetooth headset connected, should be "HeadsetBT"
// if not connected, will be "ReceiverAndMicrophone"
// now, play a quick sound we put in the bundle (bomb.wav)
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
SystemSoundID soundFileObject;
soundFileURLRef = CFBundleCopyResourceURL (mainBundle,CFSTR ("bomb"),CFSTR ("wav"),NULL);
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURLRef
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
归功于这个
这篇关于记录来自 iPhone 中蓝牙耳机的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!