1

我在使用扬声器和麦克风切换音频时遇到问题。首先,当应用程序启动音频播放到默认扬声器时。然后在按钮事件上,我将其更改为在麦克风中播放的麦克风。

但是当我尝试切换回扬声器时,它对我不起作用,它在麦克风中播放。

代码

 - (IBAction)setSpeakerPhoneEnabled:(UIButton *)sender
{

    AVAudioSession* session = [AVAudioSession sharedInstance];
    BOOL success;
    NSError* error;
    if (isHeadPhone) {

        [session setCategory:AVAudioSessionCategoryPlayAndRecord
                                 error:&error];

        UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

        AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);


    }
    else{
        success = [session setCategory:AVAudioSessionCategoryPlayback
                                 error:&error];
        UInt32 audioRouteOverride = kAudioSessionOutputRoute_Headphones;
        AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

    }


    isHeadPhone = !isHeadPhone;
    [self setTheSpekerImage];
}

当我做错时请告诉我

4

1 回答 1

0

我已经完成了以下代码

    - (IBAction)setSpeakerPhoneEnabled:(UIButton *)sender
{
    AudioSessionInitialize (NULL, NULL, NULL, NULL);




    if (isHeadPhone) {


        UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

        AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,sizeof (audioRouteOverride),&audioRouteOverride);

         [[AVAudioSession sharedInstance] setActive:YES error:nil];



    }
    else{

        UInt32 dataSize = sizeof(CFStringRef);
        CFStringRef currentRoute = NULL;
        OSStatus result = noErr;

        AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &dataSize, &currentRoute);

        // Set the category to use the speakers and microphone.
        UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
        result = AudioSessionSetProperty (
                                          kAudioSessionProperty_AudioCategory,
                                          sizeof (sessionCategory),
                                          &sessionCategory
                                          );
        assert(result == kAudioSessionNoError);

        Float64 sampleRate = 44100.0;
        dataSize = sizeof(sampleRate);
        result = AudioSessionSetProperty (
                                          kAudioSessionProperty_PreferredHardwareSampleRate,
                                          dataSize,
                                          &sampleRate
                                          );
        assert(result == kAudioSessionNoError);

        // Default to speakerphone if a headset isn't plugged in.
        // Overriding the output audio route

        UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
        dataSize = sizeof(audioRouteOverride);
        AudioSessionSetProperty(
                                kAudioSessionProperty_OverrideAudioRoute,
                                dataSize,
                                &audioRouteOverride);

        assert(result == kAudioSessionNoError);

        AudioSessionSetActive(YES);

//
    }


    isHeadPhone = !isHeadPhone;
    [self setTheSpekerImage];
}
于 2014-09-26T06:53:06.900 回答