如果插入耳机,则播放音频

时间:2012-10-15 14:01:39

标签: iphone ios avfoundation avaudioplayer

  

可能重复:
  How can I find out if an external headset is connected to an iPhone?

我正在开发音频播放器应用程序,并且在一种模式(播放模式)中我想检测耳机的状态,如果插入插孔,它应该播放其他不应该。

编辑以下是我的代码中的一个片段,也有关于我尝试做什么的解释。我在SO上找到了其他线程的大部分代码,但仍然无法按照我的意愿运行。

- (void)isHeadsetPluggedIn {

    UInt32 routeSize = sizeof (CFStringRef);
    CFStringRef route;

    AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
                             &routeSize,
                             &route);

    NSString* routeStr = (NSString*)route;

    NSRange headsetRange = [routeStr rangeOfString : @"Headset"];
    NSRange receiverRange = [routeStr rangeOfString : @"Receiver"];

    if(headsetRange.location != NSNotFound) {
        // Don't change the route if the headset is plugged in.
        NSLog(@"headphone is plugged in ");
          //here is the place i want to play music
          /* AVAudioPlayer *mySound;

    mySound =[[AVAudioPlayer alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"apple_SIMToolkitPositiveACK" withExtension:@"mp3" ]error:nil];
    [mySound play];

//here is the place where i must receive the info about plug-in(headset)
*/
    }
    else if (receiverRange.location != NSNotFound) {
        // Change to play on the speaker
        NSLog(@"play on the speaker");

    }
    else {
        NSLog(@"Unknown audio route.");

    }
}

0 个答案:

没有答案