简体   繁体   English

如何将iPhone音频路由到蓝牙扬声器

[英]how to route iPhone audio to the bluetooth speaker

I am trying to get input from Mic and output it to my bluetooth device ti Speaker using audioRouteOverride . 我正在尝试使用audioRouteOverride从麦克风获得输入并将其输出到我的蓝牙设备ti Speaker。 But No luck. 但是,没有运气。 iPhone Mic output is still going to iPhone builtin speakers. iPhone Mic输出仍将传递给iPhone内置扬声器。 I expected kAudioSessionOutputRoute_BluetoothA2DP is the key here. 我期望kAudioSessionOutputRoute_BluetoothA2DP是这里的关键。 But it is not working as expected. 但是它没有按预期工作。

- (id) init {
    self = [super init];

    OSStatus status;

    // Describe audio component
    AudioComponentDescription desc;
    desc.componentType = kAudioUnitType_Output;
    desc.componentSubType = kAudioUnitSubType_RemoteIO;
    desc.componentFlags = 0;
    desc.componentFlagsMask = 0;
    desc.componentManufacturer = kAudioUnitManufacturer_Apple;

    // Get component
    AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc);

    // Get audio units
    status = AudioComponentInstanceNew(inputComponent, &audioUnit);
    checkStatus(status);

    // Enable IO for recording
    UInt32 flag = 1;
    status = AudioUnitSetProperty(audioUnit, 
                              kAudioOutputUnitProperty_EnableIO, 
                              kAudioUnitScope_Input, 
                              kInputBus,
                              &flag, 
                              sizeof(flag));
    checkStatus(status);

    // Enable IO for playback
    status = AudioUnitSetProperty(audioUnit, 
                              kAudioOutputUnitProperty_EnableIO, 
                              kAudioUnitScope_Output, 
                              kOutputBus,
                              &flag, 
                              sizeof(flag));
    checkStatus(status);

    // Describe format
    AudioStreamBasicDescription audioFormat;
    audioFormat.mSampleRate         = 44100.00;
    audioFormat.mFormatID           = kAudioFormatLinearPCM;
    audioFormat.mFormatFlags        = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    audioFormat.mFramesPerPacket    = 1;
    audioFormat.mChannelsPerFrame   = 1;
    audioFormat.mBitsPerChannel     = 16;
    audioFormat.mBytesPerPacket     = 2;
    audioFormat.mBytesPerFrame      = 2;

    // Apply format
    status = AudioUnitSetProperty(audioUnit, 
                              kAudioUnitProperty_StreamFormat, 
                              kAudioUnitScope_Output, 
                              kInputBus, 
                              &audioFormat, 
                              sizeof(audioFormat));
    checkStatus(status);
    status = AudioUnitSetProperty(audioUnit, 
                              kAudioUnitProperty_StreamFormat, 
                              kAudioUnitScope_Input, 
                              kOutputBus, 
                              &audioFormat, 
                              sizeof(audioFormat));
    checkStatus(status);


    // Set input callback
    AURenderCallbackStruct callbackStruct;
    callbackStruct.inputProc = recordingCallback;
    callbackStruct.inputProcRefCon = self;
    status = AudioUnitSetProperty(audioUnit, 
                              kAudioOutputUnitProperty_SetInputCallback, 
                              kAudioUnitScope_Global, 
                              kInputBus, 
                              &callbackStruct, 
                              sizeof(callbackStruct));
    checkStatus(status);

    // Set output callback
    callbackStruct.inputProc = playbackCallback;
    callbackStruct.inputProcRefCon = self;
    status = AudioUnitSetProperty(audioUnit, 
                              kAudioUnitProperty_SetRenderCallback, 
                              kAudioUnitScope_Global, 
                              kOutputBus,
                              &callbackStruct, 
                              sizeof(callbackStruct));
    checkStatus(status);

    // Disable buffer allocation for the recorder (optional - do this if we want to pass in our own)
    flag = 0;
    status = AudioUnitSetProperty(audioUnit, 
                              kAudioUnitProperty_ShouldAllocateBuffer,
                              kAudioUnitScope_Output, 
                              kInputBus,
                              &flag, 
                              sizeof(flag));

    NSLog(@"%ld",(long)status);
    // Allocate our own buffers (1 channel, 16 bits per sample, thus 16 bits per frame, thus 2 bytes per frame).
    // Practice learns the buffers used contain 512 frames, if this changes it will be fixed in processAudio.
    tempBuffer.mNumberChannels = 1;
    tempBuffer.mDataByteSize = 512 * 2;
    tempBuffer.mData = malloc( 512 * 2 );

    UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
    status = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory);
    NSLog(@"%ld",(long)status);


    UInt32 allowMixing = true;
    status = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);

    UInt32 audioRouteOverride = kAudioSessionOutputRoute_BluetoothA2DP; //kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

    status = AudioUnitInitialize(audioUnit);
    checkStatus(status);

    return self;
}

Try with this code. 尝试使用此代码。 This code is also working on iOS 7 and 8(tested). 此代码也可在iOS 7和8(已测试)上使用。

+ (void) startBTAudio
{
    UInt32 allowBluetoothInput = 1;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, sizeof(allowBluetoothInput), &allowBluetoothInput);  
}


+ (void) stopBTAudio
{
    UInt32 allowBluetoothInput = 0;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, sizeof(allowBluetoothInput), &allowBluetoothInput);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将音频从设备扬声器路由到蓝牙扬声器? - How to route audio from device speaker to bluetooth speaker? 将音频重新路由到蓝牙扬声器 - Reroute Audio to Bluetooth Speaker 如何在iPhone扬声器上强制播放音频? - How to force play audio on iPhone speaker? 如何获取可用的输出音频源列表(扬声器,耳机,蓝牙) - How to get list of available output audio source ( Speaker, earphone, bluetooth ) 在iOS中以编程方式将呼叫音频路由从蓝牙耳机更改为iPhone扬声器 - Change Call Audio routing from Bluetooth Headset to iPhone Speaker in iOS programatically 如何在不使用AudioSessionSetProperty的情况下将音频路由到扬声器? - How Do I Route Audio to Speaker without using AudioSessionSetProperty? 在扬声器、内置麦克风、蓝牙或无音频之间切换音频 - Switch Audio Between Speaker, Built in mic, Bluetooth or No Audio SKAction playSoundFileNamed通过iphone扬声器没有音频 - SKAction playSoundFileNamed no audio through iphone speaker 如何从一个iPhone麦克风发送和接收现场音频到另一个iPhone扬声器? - How to Send and Receive Live audio from one iPhone mic to the another iPhone speaker? MPMusicPlayerController杀死iPhone扬声器路线上的RemoteIO - MPMusicPlayerController kills RemoteIO on iPhone Speaker route
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM