@@ -921,8 +921,16 @@ - (void)contentKeyWithSPCData:(NSData * _Nonnull)spcData completionHandler:(void
921
921
922
922
- (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary <NSString *, id> *)change context : (void *)context
923
923
{
924
- if (_playerView != nil && [object isEqual: _playerView.pictureInPictureController] && [keyPath isEqualToString: @" isPictureInPicturePossible" ]) {
925
-
924
+ if (_playerView != nil || _playerViewController != nil ) {
925
+ if ([keyPath isEqualToString: @" playbackLikelyToKeepUp" ]) {
926
+ if (_playerView != nil ) {
927
+ [_playerView.player play ];
928
+ } else if (_playerViewController != nil ) {
929
+ [_playerViewController.player play ];
930
+ }
931
+ } else if (_playerView != nil && [object isEqual: _playerView.pictureInPictureController] && [keyPath isEqualToString: @" isPictureInPicturePossible" ]) {
932
+
933
+ }
926
934
}
927
935
}
928
936
@@ -1143,6 +1151,8 @@ - (void)jwplayer:(id<JWPlayer>)player didLoadPlaylistItem:(JWPlayerItem *)item a
1143
1151
1144
1152
self.onPlaylistItem (@{@" playlistItem" : [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding], @" index" : [NSNumber numberWithInteger: index ]});
1145
1153
}
1154
+
1155
+ [item addObserver: self forKeyPath: @" playbackLikelyToKeepUp" options: NSKeyValueObservingOptionNew context: nil ];
1146
1156
}
1147
1157
1148
1158
- (void )jwplayer : (id <JWPlayer>)player didLoadPlaylist : (NSArray <JWPlayerItem *> *)playlist
@@ -1468,46 +1478,75 @@ - (void)initializeAudioSession
1468
1478
{
1469
1479
AVAudioSession *audioSession = [AVAudioSession sharedInstance ];
1470
1480
1481
+ [[NSNotificationCenter defaultCenter ] addObserver: self
1482
+ selector: @selector (handleMediaServicesReset )
1483
+ name: AVAudioSessionMediaServicesWereResetNotification
1484
+ object: audioSession];
1485
+
1471
1486
[[NSNotificationCenter defaultCenter ] addObserver: self
1472
1487
selector: @selector (audioSessionInterrupted: )
1473
1488
name: AVAudioSessionInterruptionNotification
1474
1489
object: audioSession];
1475
1490
1476
- NSError *setCategoryError = nil ;
1477
- BOOL success = [audioSession setCategory: AVAudioSessionCategoryPlayback withOptions: AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionDefaultToSpeaker error: &setCategoryError];
1491
+ NSError *categoryError = nil ;
1492
+ BOOL success = [audioSession setCategory: AVAudioSessionCategoryPlayback error: &categoryError];
1493
+
1494
+ NSError *modeError = nil ;
1495
+ [audioSession setMode: AVAudioSessionModeDefault error: &modeError];
1478
1496
1479
1497
NSError *activationError = nil ;
1480
- success = [audioSession setActive: YES withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error: &activationError];
1498
+ success = [audioSession setActive: YES error: &activationError];
1481
1499
1482
1500
[[NSNotificationCenter defaultCenter ] addObserver: self
1483
1501
selector: @selector (applicationWillResignActive: )
1484
1502
name: UIApplicationWillResignActiveNotification object: nil ];
1503
+
1504
+ [[NSNotificationCenter defaultCenter ] addObserver: self
1505
+ selector: @selector (applicationWillEnterForeground: )
1506
+ name: UIApplicationWillEnterForegroundNotification object: nil ];
1485
1507
}
1486
1508
1509
+ // Interupted
1510
+
1487
1511
-(void )audioSessionInterrupted : (NSNotification *)note
1488
1512
{
1489
- if ([note.name isEqualToString: AVAudioSessionInterruptionNotification]) {
1490
- NSLog (@" Interruption notification" );
1491
-
1492
- if ([[note.userInfo valueForKey: AVAudioSessionInterruptionTypeKey] isEqualToNumber: [NSNumber numberWithInt: AVAudioSessionInterruptionTypeBegan]]) {
1493
- [self audioInterruptionsStarted: note];
1494
- } else {
1495
- [self audioInterruptionsEnded: note];
1496
- }
1513
+ NSNumber *interruptionType = [[note userInfo ] objectForKey: AVAudioSessionInterruptionTypeKey];
1514
+ NSNumber *interruptionOption = [[note userInfo ] objectForKey: AVAudioSessionInterruptionOptionKey];
1515
+
1516
+ switch (interruptionType.unsignedIntegerValue ) {
1517
+ case AVAudioSessionInterruptionTypeBegan: {
1518
+ _wasInterrupted = YES ;
1519
+
1520
+ if (_playerView != nil ) {
1521
+ [_playerView.player pause ];
1522
+ } else if (_playerViewController != nil ) {
1523
+ [_playerViewController.player pause ];
1524
+ }
1525
+ } break ;
1526
+ case AVAudioSessionInterruptionTypeEnded: {
1527
+ if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume || (!_userPaused && _backgroundAudioEnabled)) {
1528
+ if (_playerView != nil ) {
1529
+ [self ->_playerView.player play ];
1530
+ } else if (_playerViewController != nil ) {
1531
+ [self ->_playerViewController.player play ];
1532
+ }
1533
+ }
1534
+ } break ;
1535
+ default :
1536
+ break ;
1497
1537
}
1498
1538
}
1499
1539
1500
- -(void )audioInterruptionsStarted : (NSNotification *)note {
1501
- _wasInterrupted = YES ;
1502
-
1503
- if (_playerView != nil ) {
1504
- [_playerView.player pause ];
1505
- } else if (_playerViewController != nil ) {
1506
- [_playerViewController.player pause ];
1507
- }
1540
+ // Service reset
1541
+
1542
+ -(void )handleMediaServicesReset
1543
+ {
1544
+ // • Handle this notification by fully reconfiguring audio
1508
1545
}
1509
1546
1510
- -(void )audioInterruptionsEnded : (NSNotification *)note {
1547
+ // Inactive
1548
+ // Hack for ios 14 stopping audio when going to background
1549
+ -(void )applicationWillResignActive : (NSNotification *)notification {
1511
1550
if (!_userPaused && _backgroundAudioEnabled) {
1512
1551
if (_playerView != nil ) {
1513
1552
[_playerView.player play ];
@@ -1517,9 +1556,9 @@ -(void)audioInterruptionsEnded:(NSNotification *)note {
1517
1556
}
1518
1557
}
1519
1558
1520
- // Inactive
1559
+ // Active
1521
1560
1522
- -(void )applicationWillResignActive : (NSNotification *)notification {
1561
+ -(void )applicationWillEnterForeground : (NSNotification *)notification {
1523
1562
if (!_userPaused && _backgroundAudioEnabled) {
1524
1563
if (_playerView != nil ) {
1525
1564
[_playerView.player play ];
0 commit comments