Reputation: 45
I am trying to show MBProgressHud
to MPMoviePlayerController
, for that I am observing notifications for load states of MPMoviePlayer
, but somehow Method observing notification never observes notifications for load states other then MPMovieLoadStatePlayable
. I show MBProgressHud
when video starts streaming but it does not work after it plays and then to pause to download video, Due to this I am unable to engage user while video is loading, If anyone has a better method please mention it or if there is any problem in the following code then let me know.
-(void)movieLoadStateDidChange:(NSNotification*)notification{
MPMoviePlayerController *player = [notification object];
if ((player.loadState & MPMovieLoadStatePlayable) == MPMovieLoadStatePlayable) {
NSLog(@"Load state Playable");
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}else if ((player.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK){
NSLog(@"Load state Playing");
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}else if ((player.loadState & MPMovieLoadStateStalled) == MPMovieLoadStateStalled){
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSLog(@"Load state stalled");
}else if ((player.loadState & MPMovieLoadStateUnknown) == MPMovieLoadStateUnknown){
NSLog(@"Load State unknown");
}
}
Upvotes: 3
Views: 756
Reputation: 3260
Ok .. i got your problem and the problem is that you are not getting notification for load states except MPMovieLoadStatePlayable. so here what you can do is...like below...
write down below notifiations in viewdidload
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
After defining it in ViewDidLoad , implement those functions like below....
- (void) moviePlayerPlaybackDidFinish:(NSNotification *)notification
{
//your code....
MPMovieFinishReason finishReason = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
NSError *error = notification.userInfo[XCDMoviePlayerPlaybackDidFinishErrorUserInfoKey];
NSString *reason = @"Unknown";
switch (finishReason)
{
case MPMovieFinishReasonPlaybackEnded:
reason = @"Playback Ended";
break;
case MPMovieFinishReasonPlaybackError:
reason = @"Playback Error";
break;
case MPMovieFinishReasonUserExited:
reason = @"User Exited";
break;
}
NSLog(@"Finish Reason: %@%@", reason, error ? [@"\n" stringByAppendingString:[error description]] : @"");
}
- (void) moviePlayerPlaybackStateDidChange:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = notification.object;
NSString *playbackState = @"Unknown";
switch (moviePlayerController.playbackState)
{
case MPMoviePlaybackStateStopped:
playbackState = @"Stopped";
break;
case MPMoviePlaybackStatePlaying:
playbackState = @"Playing";
break;
case MPMoviePlaybackStatePaused:
playbackState = @"Paused";
break;
case MPMoviePlaybackStateInterrupted:
playbackState = @"Interrupted";
break;
case MPMoviePlaybackStateSeekingForward:
playbackState = @"Seeking Forward";
break;
case MPMoviePlaybackStateSeekingBackward:
playbackState = @"Seeking Backward";
break;
}
NSLog(@"Playback State: %@", playbackState);
}
- (void) moviePlayerLoadStateDidChange:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = notification.object;
NSMutableString *loadState = [NSMutableString new];
MPMovieLoadState state = moviePlayerController.loadState;
if (state & MPMovieLoadStatePlayable)
[loadState appendString:@" | Playable"];
if (state & MPMovieLoadStatePlaythroughOK)
[loadState appendString:@" | Playthrough OK"];
if (state & MPMovieLoadStateStalled)
[loadState appendString:@" | Stalled"];
NSLog(@"Load State: %@", loadState.length > 0 ? [loadState substringFromIndex:3] : @"N/A");
}
let me know it is working or not!!!
Happy Coding!!!!
Upvotes: 3