2

ios设备上有一部互动电影。当电影开始时(点击),视频开头的人会问你插入耳机,如果插入,那么视频应该自动跳转到故事(直接进入视频故事)。我应该怎么办?以及如何编写代码?

4

3 回答 3

3

首先,您必须注册 AudioRoute Changes :-

AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,
                                     audioRouteChangeListenerCallback,
                                 self);

在这里您可以描述更改路线的原因:-

CFDictionaryRef routeChangeDictionary = inPropertyValue;

  CFNumberRef routeChangeReasonRef =
  CFDictionaryGetValue (routeChangeDictionary,
                        CFSTR (kAudioSession_AudioRouteChangeKey_Reason));

  SInt32 routeChangeReason;

      CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

  if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) 
  {
       // your statements for headset unplugged

  }
  if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
  {
       // your statements for headset plugged                             
  }
于 2012-05-21T13:04:22.183 回答
0

这可能是另一种方式:

CFStringRef newRoute;
size = sizeof(CFStringRef);
XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute), "couldn't get new audio route");
if (newRoute)
{
    CFShow(newRoute);
    if (CFStringCompare(newRoute, CFSTR("HeadsetInOut"), NULL) == kCFCompareEqualTo) // headset plugged in
          {
...
          }
    else if (CFStringCompare(newRoute, CFSTR("SpeakerAndMicrophone"), NULL) == kCFCompareEqualTo){
     ....
     }
}
于 2012-09-13T20:42:06.187 回答
0

首先检查设备是否连接到任何耳机。

+(BOOL)isHeadsetPluggedIn {

AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
for (AVAudioSessionPortDescription* desc in [route outputs]) {
    if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
        return YES;
}
return NO;
}

然后根据bool值,写出自己的条件。像下面的东西..

if (isHeadphonesConnected) {
    //Write your own code here
}else{

}

您还可以注册一个通知,以防您想知道当您在屏幕中时耳机是否被移除。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(audioRoutingListenerCallback:)
                                        name:AVAudioSessionRouteChangeNotification
                                           object:nil];

- (void)audioRoutingListenerCallback:(NSNotification*)notification
{
    NSDictionary *interuptionDict = notification.userInfo;

    NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];

    switch (routeChangeReason) {

        case AVAudioSessionRouteChangeReasonNewDeviceAvailable:

            NSLog(@"Headphone/Line plugged in");

            /*Write your own condition.*/

            break;

        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:

            NSLog(@"Headphone/Line was pulled.");

            /*Write your own condition.*/                
            break;

        case AVAudioSessionRouteChangeReasonCategoryChange:
            // called at start - also when other audio wants to play

            break;
    }
}
于 2017-06-15T13:24:31.320 回答