From d003c87b578f3938772d75c60eb0d1796efb284b Mon Sep 17 00:00:00 2001 From: Hampton Maxwell Date: Thu, 12 Jul 2018 21:48:58 -0700 Subject: [PATCH 1/3] Add onAudioBecomingNoisy for iOS --- README.md | 8 ++++++++ Video.js | 2 ++ ios/RCTVideo.m | 16 ++++++++++++++++ ios/RCTVideoManager.m | 1 + 4 files changed, 27 insertions(+) diff --git a/README.md b/README.md index 19b4371d50..cac87ad2ee 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,7 @@ var styles = StyleSheet.create({ * [volume](#volume) ### Event props +* [onAudioBecomingNoisy](#onaudiobecomingnoisy) * [onLoad](#onload) * [onLoadStart](#onloadstart) * [onProgress](#onprogress) @@ -452,6 +453,13 @@ Platforms: all ### Event props +#### onAudioBecomingNoisy +Callback function that is called when the audio is about to become 'noisy' due to a change in audio outputs. Typically this is called when audio output is being switched from an external source like headphones back to the internal speaker. It's a good idea to pause the media when this happens so the speaker doesn't start blasting sound. + +Payload: none + +Platforms: Android ExoPlayer, iOS + #### onLoad Callback function that is called when the media is loaded and ready to play. diff --git a/Video.js b/Video.js index 7a0d7a3996..dda530d312 100644 --- a/Video.js +++ b/Video.js @@ -235,6 +235,7 @@ export default class Video extends Component { onVideoEnd: this._onEnd, onVideoBuffer: this._onBuffer, onTimedMetadata: this._onTimedMetadata, + onVideoAudioBecomingNoisy: this._onAudioBecomingNoisy, onVideoFullscreenPlayerWillPresent: this._onFullscreenPlayerWillPresent, onVideoFullscreenPlayerDidPresent: this._onFullscreenPlayerDidPresent, onVideoFullscreenPlayerWillDismiss: this._onFullscreenPlayerWillDismiss, @@ -296,6 +297,7 @@ Video.propTypes = { onVideoSeek: PropTypes.func, onVideoEnd: PropTypes.func, onTimedMetadata: PropTypes.func, + onVideoAudioBecomingNoisy: PropTypes.func, onVideoFullscreenPlayerWillPresent: PropTypes.func, onVideoFullscreenPlayerDidPresent: PropTypes.func, onVideoFullscreenPlayerWillDismiss: PropTypes.func, diff --git a/ios/RCTVideo.m b/ios/RCTVideo.m index e155e7d257..c1bf66dda8 100644 --- a/ios/RCTVideo.m +++ b/ios/RCTVideo.m @@ -91,6 +91,11 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(audioRouteChanged:) + name:AVAudioSessionRouteChangeNotification + object:nil]; } return self; @@ -190,6 +195,17 @@ - (void)applicationWillEnterForeground:(NSNotification *)notification } } +#pragma mark - Audio events + +- (void)audioRouteChanged:(NSNotification *)notification +{ + NSNumber *reason = [[notification userInfo] objectForKey:AVAudioSessionRouteChangeReasonKey]; + NSNumber *previousRoute = [[notification userInfo] objectForKey:AVAudioSessionRouteChangePreviousRouteKey]; + if (reason.unsignedIntValue == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) { + self.onVideoAudioBecomingNoisy(@{@"target": self.reactTag}); + } +} + #pragma mark - Progress - (void)sendProgressUpdate diff --git a/ios/RCTVideoManager.m b/ios/RCTVideoManager.m index 308d49e810..8e566602dd 100644 --- a/ios/RCTVideoManager.m +++ b/ios/RCTVideoManager.m @@ -46,6 +46,7 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_VIEW_PROPERTY(onVideoSeek, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoEnd, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onTimedMetadata, RCTBubblingEventBlock); +RCT_EXPORT_VIEW_PROPERTY(onVideoAudioBecomingNoisy, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoFullscreenPlayerWillPresent, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoFullscreenPlayerDidPresent, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoFullscreenPlayerWillDismiss, RCTBubblingEventBlock); From 2d5ef8cfd266b62a91ebc843cee159f2b611d72a Mon Sep 17 00:00:00 2001 From: Hampton Maxwell Date: Thu, 12 Jul 2018 21:49:14 -0700 Subject: [PATCH 2/3] Update name of onAudioBecomingNoisy --- .../main/java/com/brentvatne/exoplayer/VideoEventEmitter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/VideoEventEmitter.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/VideoEventEmitter.java index a447a73446..ff1fc5638a 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/VideoEventEmitter.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/VideoEventEmitter.java @@ -42,7 +42,7 @@ class VideoEventEmitter { private static final String EVENT_BUFFER = "onVideoBuffer"; private static final String EVENT_IDLE = "onVideoIdle"; private static final String EVENT_TIMED_METADATA = "onTimedMetadata"; - private static final String EVENT_AUDIO_BECOMING_NOISY = "onAudioBecomingNoisy"; + private static final String EVENT_AUDIO_BECOMING_NOISY = "onVideoAudioBecomingNoisy"; private static final String EVENT_AUDIO_FOCUS_CHANGE = "onAudioFocusChanged"; private static final String EVENT_PLAYBACK_RATE_CHANGE = "onPlaybackRateChange"; From de68244bc6a72f5bbfdf9c0160015eac0ff4f304 Mon Sep 17 00:00:00 2001 From: Hampton Maxwell Date: Mon, 16 Jul 2018 09:47:27 -0700 Subject: [PATCH 3/3] Add onVideoAudioBecomingNoisy --- ios/RCTVideo.h | 1 + 1 file changed, 1 insertion(+) diff --git a/ios/RCTVideo.h b/ios/RCTVideo.h index 38d2ab4e92..1c471236b9 100644 --- a/ios/RCTVideo.h +++ b/ios/RCTVideo.h @@ -17,6 +17,7 @@ @property (nonatomic, copy) RCTBubblingEventBlock onVideoSeek; @property (nonatomic, copy) RCTBubblingEventBlock onVideoEnd; @property (nonatomic, copy) RCTBubblingEventBlock onTimedMetadata; +@property (nonatomic, copy) RCTBubblingEventBlock onVideoAudioBecomingNoisy; @property (nonatomic, copy) RCTBubblingEventBlock onVideoFullscreenPlayerWillPresent; @property (nonatomic, copy) RCTBubblingEventBlock onVideoFullscreenPlayerDidPresent; @property (nonatomic, copy) RCTBubblingEventBlock onVideoFullscreenPlayerWillDismiss;