0

I have been using following code to detect the iPhone silent button status but it sometimes worked but usually dont work. I have searched alot but wont be able to figure out the solution yet.

-(void)playWordSoundWithFile:(NSString*)filePath {

audioPlayer = nil;



if (filePath) {

    NSArray *list = [filePath componentsSeparatedByString:@"."];
    if (list && [list isKindOfClass:[NSArray class]] && [list count] > 1) {

        //audioPlayer = [self audioPlayerWithFile:[list objectAtIndex:0] andExtension:@"mp3"];//[list objectAtIndex:1]
        NSString *soundFileIs=[NSString stringWithFormat:@"%@.%@",[list objectAtIndex:0],[list objectAtIndex:1]];
        NSString *paths = [[NSBundle mainBundle] resourcePath];
        NSString *audioFile = [paths stringByAppendingPathComponent:soundFileIs];
        NSData *cdata =[NSData dataWithContentsOfFile:audioFile];

        NSLog(@"Sound file %@",soundFileIs);

        audioPlayer = [[AVAudioPlayer alloc] initWithData:cdata error:nil];
        audioPlayer.delegate = self;

    }
}

if (audioPlayer) {

    [audioPlayer setVolume:1.0];
    playerId = 16;
    [audioPlayer play];

}

}

1 Answers1

0

Here just return your BOOL value.

-(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;

    }

Please also check How to programmatically sense the iPhone mute switch? All the methods to mute are provided here. Hope this helps.

Community
  • 1
  • 1
IronManGill
  • 7,222
  • 2
  • 31
  • 52