Wouter De Ruyck
Wouter De Ruyck

Reputation: 59

iPhone 4, audio level issue after recording

I am developing an iOs app that needs to record and play audio. I noticed on iPhone 4 that the audio level was much lower after recording. This was fixed when I used the code i found here:iOS - iPhone sound level low after recording sound using AVFoundation

 if(!IS_IPHONE_5){
         UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
         AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
        UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
         AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
     }

This works great, but this causes the problem that if a user mute his phone, audio is still played (also other sounds in the app)

Is it somehow possible to detect if audio is muted? And if so, that we then not execute the code above.

Upvotes: 1

Views: 121

Answers (1)

D-eptdeveloper
D-eptdeveloper

Reputation: 2430

may be you can check it this way

CFStringRef state = nil;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
OSStatus status = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);

if (status == kAudioSessionNoError)
{
    return (CFStringGetLength(state) == 0);   // YES = silent
}
return NO;

and modified method is

-(BOOL)silenced {
     #if TARGET_IPHONE_SIMULATOR
         // return NO in simulator. Code causes crashes for some reason.
     return NO;
     #endif

    CFStringRef state;
    UInt32 propertySize = sizeof(CFStringRef);
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
    if(CFStringGetLength(state) > 0)
        return NO;
    else
        return YES;
    }

Upvotes: 1

Related Questions