From d9ef27d802c0b2a69189464dfbf014c11d376dc7 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 13 Apr 2017 13:43:09 -0400 Subject: [PATCH] Ignore hardware mute switch during video playback in messages view. // FREEBIE --- .../ViewControllers/MessagesViewController.m | 13 ++++++++-- .../src/ViewControllers/ViewControllerUtils.h | 2 ++ .../src/ViewControllers/ViewControllerUtils.m | 24 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Signal/src/ViewControllers/MessagesViewController.m b/Signal/src/ViewControllers/MessagesViewController.m index 6cdba6cbf..650201938 100644 --- a/Signal/src/ViewControllers/MessagesViewController.m +++ b/Signal/src/ViewControllers/MessagesViewController.m @@ -43,6 +43,7 @@ #import "UIUtil.h" #import "UIViewController+CameraPermissions.h" #import "UIViewController+OWS.h" +#import "ViewControllerUtils.h" #import #import #import @@ -1681,7 +1682,8 @@ typedef enum : NSUInteger { if ([messageMedia isVideo]) { if ([fileManager fileExistsAtPath:[attStream.mediaURL path]]) { [self dismissKeyBoard]; - _videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:attStream.mediaURL]; + self.videoPlayer = + [[MPMoviePlayerController alloc] initWithContentURL:attStream.mediaURL]; [_videoPlayer prepareToPlay]; [[NSNotificationCenter defaultCenter] addObserver:self @@ -1876,7 +1878,14 @@ typedef enum : NSUInteger { - (void)clearVideoPlayer { [_videoPlayer stop]; [_videoPlayer.view removeFromSuperview]; - _videoPlayer = nil; + self.videoPlayer = nil; +} + +- (void)setVideoPlayer:(MPMoviePlayerController *)videoPlayer +{ + _videoPlayer = videoPlayer; + + [ViewControllerUtils setAudioIgnoresHardwareMuteSwitch:videoPlayer != nil]; } - (void)collectionView:(JSQMessagesCollectionView *)collectionView diff --git a/Signal/src/ViewControllers/ViewControllerUtils.h b/Signal/src/ViewControllers/ViewControllerUtils.h index acaea323e..ccb96260f 100644 --- a/Signal/src/ViewControllers/ViewControllerUtils.h +++ b/Signal/src/ViewControllers/ViewControllerUtils.h @@ -16,4 +16,6 @@ replacementString:(NSString *)insertionText countryCode:(NSString *)countryCode; ++ (void)setAudioIgnoresHardwareMuteSwitch:(BOOL)shouldIgnore; + @end diff --git a/Signal/src/ViewControllers/ViewControllerUtils.m b/Signal/src/ViewControllers/ViewControllerUtils.m index 9f973c267..53553268e 100644 --- a/Signal/src/ViewControllers/ViewControllerUtils.m +++ b/Signal/src/ViewControllers/ViewControllerUtils.m @@ -5,6 +5,7 @@ #import "ViewControllerUtils.h" #import "PhoneNumber.h" #import "StringUtil.h" +#import #import NS_ASSUME_NONNULL_BEGIN @@ -68,6 +69,29 @@ NS_ASSUME_NONNULL_BEGIN [textField setSelectedTextRange:[textField textRangeFromPosition:pos toPosition:pos]]; } ++ (void)setAudioIgnoresHardwareMuteSwitch:(BOOL)shouldIgnore +{ + NSError *error = nil; + BOOL success = [[AVAudioSession sharedInstance] + setCategory:(shouldIgnore ? AVAudioSessionCategoryPlayback : AVAudioSessionCategoryPlayAndRecord)error:&error]; + OWSAssert(!error); + if (!success || error) { + DDLogError(@"%@ Error in setAudioIgnoresHardwareMuteSwitch: %d", self.tag, shouldIgnore); + } +} + +#pragma mark - Logging + ++ (NSString *)tag +{ + return [NSString stringWithFormat:@"[%@]", self.class]; +} + +- (NSString *)tag +{ + return self.class.tag; +} + @end NS_ASSUME_NONNULL_END