2013-08-20 50 views
0

我一直在使用以下代碼來檢測iPhone無聲按鈕狀態,但它有時可以正常工作,但通常不工作。我搜索了很多,但不能找出解決方案。如何在iOS6中檢測iPhone無聲按鈕狀態

- (空)playWordSoundWithFile:(的NSString *)文件路徑{

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

} 

}

+0

你想實現什麼? – IronManGill

+0

其實問題是當我播放聲音,然後切換iPhone靜音聲音繼續播放它不會去靜音這就是爲什麼馬試圖這樣做。 –

+0

我已經發布了用於播放聲音的代碼。 –

回答

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; 

    } 

還請檢查How to programmatically sense the iPhone mute switch?所有的靜音方法都在這裏提供。希望這可以幫助。

+1

它總是返回No. –

相關問題