Skip to content

Commit 211d14b

Browse files
authored
Merge pull request #1131 from react-native-community/feature/ios-becoming-noisy
Document onAudioBecomingNoisy & add iOS support
2 parents 5178601 + de68244 commit 211d14b

File tree

6 files changed

+29
-1
lines changed

6 files changed

+29
-1
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ var styles = StyleSheet.create({
239239
* [volume](#volume)
240240

241241
### Event props
242+
* [onAudioBecomingNoisy](#onaudiobecomingnoisy)
242243
* [onLoad](#onload)
243244
* [onLoadStart](#onloadstart)
244245
* [onProgress](#onprogress)
@@ -452,6 +453,13 @@ Platforms: all
452453

453454
### Event props
454455

456+
#### onAudioBecomingNoisy
457+
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.
458+
459+
Payload: none
460+
461+
Platforms: Android ExoPlayer, iOS
462+
455463
#### onLoad
456464
Callback function that is called when the media is loaded and ready to play.
457465

Video.js

+2
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ export default class Video extends Component {
235235
onVideoEnd: this._onEnd,
236236
onVideoBuffer: this._onBuffer,
237237
onTimedMetadata: this._onTimedMetadata,
238+
onVideoAudioBecomingNoisy: this._onAudioBecomingNoisy,
238239
onVideoFullscreenPlayerWillPresent: this._onFullscreenPlayerWillPresent,
239240
onVideoFullscreenPlayerDidPresent: this._onFullscreenPlayerDidPresent,
240241
onVideoFullscreenPlayerWillDismiss: this._onFullscreenPlayerWillDismiss,
@@ -296,6 +297,7 @@ Video.propTypes = {
296297
onVideoSeek: PropTypes.func,
297298
onVideoEnd: PropTypes.func,
298299
onTimedMetadata: PropTypes.func,
300+
onVideoAudioBecomingNoisy: PropTypes.func,
299301
onVideoFullscreenPlayerWillPresent: PropTypes.func,
300302
onVideoFullscreenPlayerDidPresent: PropTypes.func,
301303
onVideoFullscreenPlayerWillDismiss: PropTypes.func,

android-exoplayer/src/main/java/com/brentvatne/exoplayer/VideoEventEmitter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class VideoEventEmitter {
4242
private static final String EVENT_BUFFER = "onVideoBuffer";
4343
private static final String EVENT_IDLE = "onVideoIdle";
4444
private static final String EVENT_TIMED_METADATA = "onTimedMetadata";
45-
private static final String EVENT_AUDIO_BECOMING_NOISY = "onAudioBecomingNoisy";
45+
private static final String EVENT_AUDIO_BECOMING_NOISY = "onVideoAudioBecomingNoisy";
4646
private static final String EVENT_AUDIO_FOCUS_CHANGE = "onAudioFocusChanged";
4747
private static final String EVENT_PLAYBACK_RATE_CHANGE = "onPlaybackRateChange";
4848

ios/RCTVideo.h

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@property (nonatomic, copy) RCTBubblingEventBlock onVideoSeek;
1818
@property (nonatomic, copy) RCTBubblingEventBlock onVideoEnd;
1919
@property (nonatomic, copy) RCTBubblingEventBlock onTimedMetadata;
20+
@property (nonatomic, copy) RCTBubblingEventBlock onVideoAudioBecomingNoisy;
2021
@property (nonatomic, copy) RCTBubblingEventBlock onVideoFullscreenPlayerWillPresent;
2122
@property (nonatomic, copy) RCTBubblingEventBlock onVideoFullscreenPlayerDidPresent;
2223
@property (nonatomic, copy) RCTBubblingEventBlock onVideoFullscreenPlayerWillDismiss;

ios/RCTVideo.m

+16
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
9191
selector:@selector(applicationWillEnterForeground:)
9292
name:UIApplicationWillEnterForegroundNotification
9393
object:nil];
94+
95+
[[NSNotificationCenter defaultCenter] addObserver:self
96+
selector:@selector(audioRouteChanged:)
97+
name:AVAudioSessionRouteChangeNotification
98+
object:nil];
9499
}
95100

96101
return self;
@@ -190,6 +195,17 @@ - (void)applicationWillEnterForeground:(NSNotification *)notification
190195
}
191196
}
192197

198+
#pragma mark - Audio events
199+
200+
- (void)audioRouteChanged:(NSNotification *)notification
201+
{
202+
NSNumber *reason = [[notification userInfo] objectForKey:AVAudioSessionRouteChangeReasonKey];
203+
NSNumber *previousRoute = [[notification userInfo] objectForKey:AVAudioSessionRouteChangePreviousRouteKey];
204+
if (reason.unsignedIntValue == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
205+
self.onVideoAudioBecomingNoisy(@{@"target": self.reactTag});
206+
}
207+
}
208+
193209
#pragma mark - Progress
194210

195211
- (void)sendProgressUpdate

ios/RCTVideoManager.m

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ - (dispatch_queue_t)methodQueue
4646
RCT_EXPORT_VIEW_PROPERTY(onVideoSeek, RCTBubblingEventBlock);
4747
RCT_EXPORT_VIEW_PROPERTY(onVideoEnd, RCTBubblingEventBlock);
4848
RCT_EXPORT_VIEW_PROPERTY(onTimedMetadata, RCTBubblingEventBlock);
49+
RCT_EXPORT_VIEW_PROPERTY(onVideoAudioBecomingNoisy, RCTBubblingEventBlock);
4950
RCT_EXPORT_VIEW_PROPERTY(onVideoFullscreenPlayerWillPresent, RCTBubblingEventBlock);
5051
RCT_EXPORT_VIEW_PROPERTY(onVideoFullscreenPlayerDidPresent, RCTBubblingEventBlock);
5152
RCT_EXPORT_VIEW_PROPERTY(onVideoFullscreenPlayerWillDismiss, RCTBubblingEventBlock);

0 commit comments

Comments
 (0)