Skip to content

Commit

Permalink
Adding Dolby Atmos workaround.
Browse files Browse the repository at this point in the history
- Add and remove spatial audio remote command handler
- Whenever we play video, we must add this event handler that returns a success status for all remote command events
- Whenever we stop video or destroy the player, remove this event handler
- This is a workaround to get Dolby Atmos working on iOS 15 and above
  • Loading branch information
alexovaltech committed Jan 17, 2022
1 parent 12bb73a commit 3030ddd
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ios/Video/RCTVideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#import <React/UIView+React.h>
#include <MediaAccessibility/MediaAccessibility.h>
#include <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

static NSString *const statusKeyPath = @"status";
static NSString *const playbackLikelyToKeepUpKeyPath = @"playbackLikelyToKeepUp";
Expand Down Expand Up @@ -40,6 +41,9 @@ @implementation RCTVideo
NSDictionary *_drm;
AVAssetResourceLoadingRequest *_loadingRequest;

/* Workaround for Dolby Atmos on OS 15.2 and beyond */
id _remoteCommandHandlerForSpatialAudio;

/* Required to publish events */
RCTEventDispatcher *_eventDispatcher;
BOOL _playbackRateObserverRegistered;
Expand Down Expand Up @@ -217,10 +221,35 @@ - (void)dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self removePlayerLayer];
[self removePlayerItemObservers];
[self removeSpatialAudioRemoteCommandHandler];
[_player removeObserver:self forKeyPath:playbackRate context:nil];
[_player removeObserver:self forKeyPath:externalPlaybackActive context: nil];
}

#pragma mark - Spatial Audio / Dolby Atmos Workaround

/* These functions are a temporarily workaround to enable the rendering of Dolby Atmos on
* iOS 15 and above.
*/
- (void)addSpatialAudioRemoteCommandHandler
{
MPRemoteCommand *remoteCommand = [MPRemoteCommandCenter sharedCommandCenter].playCommand;

_remoteCommandHandlerForSpatialAudio = [remoteCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
}

- (void)removeSpatialAudioRemoteCommandHandler
{
MPRemoteCommand *remoteCommand = [MPRemoteCommandCenter sharedCommandCenter].playCommand;

if (_remoteCommandHandlerForSpatialAudio != nil) {
[remoteCommand removeTarget:_remoteCommandHandlerForSpatialAudio];
_remoteCommandHandlerForSpatialAudio = nil;
}
}

#pragma mark - App lifecycle handlers

- (void)applicationWillResignActive:(NSNotification *)notification
Expand Down Expand Up @@ -930,9 +959,11 @@ - (void)setPaused:(BOOL)paused
if (paused) {
[_player pause];
[_player setRate:0.0];
[self removeSpatialAudioRemoteCommandHandler];
} else {

[self configureAudio];
[self addSpatialAudioRemoteCommandHandler];

if (@available(iOS 10.0, *) && !_automaticallyWaitsToMinimizeStalling) {
[_player playImmediatelyAtRate:_rate];
Expand Down

0 comments on commit 3030ddd

Please sign in to comment.