0

我一直在使用以下代码来检测 iPhone 静音按钮状态,但它有时会起作用,但通常不起作用。我已经搜索了很多,但还没有找到解决方案。

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

}

}

4

1 回答 1

0

这里只返回您的 BOOL 值。

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

    }

另请检查如何以编程方式感应 iPhone 静音开关?此处提供了所有静音方法。希望这可以帮助。

于 2013-08-20T08:02:14.473 回答