浏览代码

vpc: pause only when headphones are unplugged

We don't want to pause when switching to HDMI or USB for example.
Thomas Guillem 8 年之前
父节点
当前提交
bcc9ffec3e
共有 1 个文件被更改,包括 11 次插入2 次删除
  1. 11 2
      Sources/VLCPlaybackController.m

+ 11 - 2
Sources/VLCPlaybackController.m

@@ -69,6 +69,7 @@ VLCMediaDelegate>
     BOOL _mediaWasJustStarted;
     BOOL _mediaWasJustStarted;
     BOOL _recheckForExistingThumbnail;
     BOOL _recheckForExistingThumbnail;
     BOOL _activeSession;
     BOOL _activeSession;
+    BOOL _headphonesWasPlugged;
 
 
     NSLock *_playbackSessionManagementLock;
     NSLock *_playbackSessionManagementLock;
 
 
@@ -103,6 +104,7 @@ VLCMediaDelegate>
 {
 {
     self = [super init];
     self = [super init];
     if (self) {
     if (self) {
+        _headphonesWasPlugged = [self areHeadphonesPlugged];
         NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
         NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
         [defaultCenter addObserver:self selector:@selector(audioSessionRouteChange:)
         [defaultCenter addObserver:self selector:@selector(audioSessionRouteChange:)
                               name:AVAudioSessionRouteChangeNotification object:nil];
                               name:AVAudioSessionRouteChangeNotification object:nil];
@@ -822,18 +824,25 @@ VLCMediaDelegate>
     }
     }
 }
 }
 
 
-- (void)audioSessionRouteChange:(NSNotification *)notification
+- (BOOL)areHeadphonesPlugged
 {
 {
     NSArray *outputs = [[AVAudioSession sharedInstance] currentRoute].outputs;
     NSArray *outputs = [[AVAudioSession sharedInstance] currentRoute].outputs;
     NSString *portName = [[outputs firstObject] portName];
     NSString *portName = [[outputs firstObject] portName];
+    return [portName isEqualToString:@"Headphones"];
+}
+
+- (void)audioSessionRouteChange:(NSNotification *)notification
+{
+    BOOL headphonesPlugged = [self areHeadphonesPlugged];
 
 
-    if (![portName isEqualToString:@"Headphones"] && [_mediaPlayer isPlaying]) {
+    if (_headphonesWasPlugged && !headphonesPlugged && [_mediaPlayer isPlaying]) {
         [_mediaPlayer pause];
         [_mediaPlayer pause];
 #if TARGET_OS_IOS
 #if TARGET_OS_IOS
         [self _savePlaybackState];
         [self _savePlaybackState];
 #endif
 #endif
         [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackControllerPlaybackDidPause object:self];
         [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackControllerPlaybackDidPause object:self];
     }
     }
+    _headphonesWasPlugged = headphonesPlugged;
 }
 }
 
 
 #pragma mark - Managing the media item
 #pragma mark - Managing the media item