Skip to content

Commit

Permalink
(ios): fix background audio. fix swipe gestures.
Browse files Browse the repository at this point in the history
  • Loading branch information
shamilovtim committed Oct 9, 2018
1 parent e8a1fd9 commit 67c1ddb
Showing 1 changed file with 118 additions and 31 deletions.
149 changes: 118 additions & 31 deletions src/ios/StreamingMedia.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ @implementation StreamingMedia {
BOOL initFullscreen;
NSString *mOrientation;
NSString *videoType;
AVPlayer *movie;
}

NSString * const TYPE_VIDEO = @"VIDEO";
Expand Down Expand Up @@ -79,6 +80,7 @@ -(void)parseOptions:(NSDictionary *)options type:(NSString *) type {
}

-(void)play:(CDVInvokedUrlCommand *) command type:(NSString *) type {
NSLog(@"play called");
callbackId = command.callbackId;
NSString *mediaUrl = [command.arguments objectAtIndex:0];
[self parseOptions:[command.arguments objectAtIndex:1] type:type];
Expand All @@ -87,18 +89,21 @@ -(void)play:(CDVInvokedUrlCommand *) command type:(NSString *) type {
}

-(void)stop:(CDVInvokedUrlCommand *) command type:(NSString *) type {
NSLog(@"stop called");
callbackId = command.callbackId;
if (moviePlayer.player) {
[moviePlayer.player pause];
}
}

-(void)playVideo:(CDVInvokedUrlCommand *) command {
NSLog(@"playvideo called");
[self ignoreMute];
[self play:command type:[NSString stringWithString:TYPE_VIDEO]];
}

-(void)playAudio:(CDVInvokedUrlCommand *) command {
NSLog(@"playaudio called");
[self ignoreMute];
[self play:command type:[NSString stringWithString:TYPE_AUDIO]];
}
Expand All @@ -114,6 +119,7 @@ -(void)ignoreMute {
}

-(void) setBackgroundColor:(NSString *)color {
NSLog(@"setbackgroundcolor called");
if ([color hasPrefix:@"#"]) {
// HEX value
unsigned rgbValue = 0;
Expand All @@ -134,6 +140,7 @@ -(void) setBackgroundColor:(NSString *)color {
}

-(UIImage*)getImage: (NSString *)imageName {
NSLog(@"getimage called");
UIImage *image = nil;
if (imageName != (id)[NSNull null]) {
if ([imageName hasPrefix:@"http"]) {
Expand All @@ -159,6 +166,7 @@ -(UIImage*)getImage: (NSString *)imageName {
}

- (void)orientationChanged:(NSNotification *)notification {
NSLog(@"orientationchanged called");
if (imageView != nil) {
// adjust imageView for rotation
imageView.bounds = moviePlayer.contentOverlayView.bounds;
Expand All @@ -167,22 +175,23 @@ - (void)orientationChanged:(NSNotification *)notification {
}

-(void)setImage:(NSString*)imagePath withScaleType:(NSString*)imageScaleType {
NSLog(@"setimage called");
imageView = [[UIImageView alloc] initWithFrame:self.viewController.view.bounds];

if (imageScaleType == nil) {
NSLog(@"imagescaletype was NIL");
imageScaleType = DEFAULT_IMAGE_SCALE;
}

if ([imageScaleType isEqualToString:@"stretch"]){
// Stretches image to fill all available background space, disregarding aspect ratio
imageView.contentMode = UIViewContentModeScaleToFill;

} else if ([imageScaleType isEqualToString:@"fit"]) {
// fits entire image perfectly
imageView.contentMode = UIViewContentModeScaleAspectFit;
} else if ([imageScaleType isEqualToString:@"aspectStretch"]) {
// Stretches image to fill all possible space while retaining aspect ratio
imageView.contentMode = UIViewContentModeScaleAspectFill;

} else {
// Places image in the center of the screen
imageView.contentMode = UIViewContentModeCenter;
Expand All @@ -193,27 +202,55 @@ -(void)setImage:(NSString*)imagePath withScaleType:(NSString*)imageScaleType {
}

-(void)startPlayer:(NSString*)uri {

NSLog(@"startplayer called");
NSURL *url = [NSURL URLWithString:uri];
AVPlayer *movie = [AVPlayer playerWithURL:url];
if ([mOrientation isEqualToString:@"landscape"]) {
moviePlayer = [[LandscapeAVPlayerViewController alloc] init];
} else if ([mOrientation isEqualToString:@"portrait"]) {
moviePlayer = [[PortraitAVPlayerViewController alloc] init];
} else {
moviePlayer = [[AVPlayerViewController alloc] init];
}
movie = [AVPlayer playerWithURL:url];

// handle orientation
[self handleOrientation];

// handle gestures
[self handleGestures];

[moviePlayer setPlayer:movie];
[moviePlayer setShowsPlaybackControls:YES];
[moviePlayer setUpdatesNowPlayingInfoCenter:YES];

if(@available(iOS 11.0, *)) { [moviePlayer setEntersFullScreenWhenPlaybackBegins:YES]; }

//present modally so we get a close button
// present modally so we get a close button
[self.viewController presentViewController:moviePlayer animated:YES completion:^(void){
//let's start this bitch.
[moviePlayer.player play];
}];

// add audio image and background color
if ([videoType isEqualToString:TYPE_AUDIO]) {
if (imageView != nil) {
[moviePlayer.contentOverlayView setAutoresizesSubviews:YES];
[moviePlayer.contentOverlayView addSubview:imageView];
}
moviePlayer.contentOverlayView.backgroundColor = backgroundColor;
[self.viewController.view addSubview:moviePlayer.view];
}

// setup listners
[self handleListeners];
}

- (void) handleListeners {

// Listen for re-maximize
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];

// Listen for minimize
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];

// Listen for playback finishing
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
Expand All @@ -226,30 +263,80 @@ -(void)startPlayer:(NSString*)uri {
name:AVPlayerItemFailedToPlayToEndTimeNotification
object:moviePlayer.player.currentItem];

if ([videoType isEqualToString:TYPE_AUDIO]) {
if (imageView != nil) {
[moviePlayer.contentOverlayView setAutoresizesSubviews:YES];
[moviePlayer.contentOverlayView addSubview:imageView];
}
moviePlayer.contentOverlayView.backgroundColor = backgroundColor;
[self.viewController.view addSubview:moviePlayer.view];
}

// Listen for click on the "Done" button

// Deprecated.. AVPlayerController doesn't offer a "Done" listener... thanks apple. We'll listen for an error when playback finishes
// [[NSNotificationCenter defaultCenter] addObserver:self
// selector:@selector(doneButtonClick:)
// name:MPMoviePlayerWillExitFullscreenNotification
// object:nil];

// Listen for orientation change
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];

/* Listen for click on the "Done" button
// Deprecated.. AVPlayerController doesn't offer a "Done" listener... thanks apple. We'll listen for an error when playback finishes
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:nil];
*/
}

- (void) handleGestures {
// Get buried nested view
UIView *contentView = [moviePlayer.view valueForKey:@"contentView"];

// loop through gestures, remove swipes
for (UIGestureRecognizer *recognizer in contentView.gestureRecognizers) {
NSLog(@"gesture loop ");
NSLog(@"%@", recognizer);
if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
[contentView removeGestureRecognizer:recognizer];
}
if ([recognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
[contentView removeGestureRecognizer:recognizer];
}
if ([recognizer isKindOfClass:[UIRotationGestureRecognizer class]]) {
[contentView removeGestureRecognizer:recognizer];
}
if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
[contentView removeGestureRecognizer:recognizer];
}
if ([recognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
[contentView removeGestureRecognizer:recognizer];
}
if ([recognizer isKindOfClass:[UISwipeGestureRecognizer class]]) {
[contentView removeGestureRecognizer:recognizer];
}
}
}

- (void) handleOrientation {
// hnadle the subclassing of the view based on the orientation variable
if ([mOrientation isEqualToString:@"landscape"]) {
moviePlayer = [[LandscapeAVPlayerViewController alloc] init];
} else if ([mOrientation isEqualToString:@"portrait"]) {
moviePlayer = [[PortraitAVPlayerViewController alloc] init];
} else {
moviePlayer = [[AVPlayerViewController alloc] init];
}
}

- (void) appDidEnterBackground:(NSNotification*)notification {
NSLog(@"appDidEnterBackground");

if (moviePlayer && movie && videoType == TYPE_AUDIO)
{
NSLog(@"did set player layer to nil");
[moviePlayer setPlayer: nil];
}
}

- (void) appDidBecomeActive:(NSNotification*)notification {
NSLog(@"appDidBecomeActive");

if (moviePlayer && movie && videoType == TYPE_AUDIO)
{
NSLog(@"did reinstate playerlayer");
[moviePlayer setPlayer:movie];
}
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {
Expand Down Expand Up @@ -281,7 +368,7 @@ - (void) moviePlayBackDidFinish:(NSNotification*)notification {


- (void)cleanup {
NSLog(@"Clean up");
NSLog(@"Clean up called");
imageView = nil;
initFullscreen = false;
backgroundColor = nil;
Expand Down

0 comments on commit 67c1ddb

Please sign in to comment.