正在将MBProgressHud添加到MPMoviePlayerController



我试图显示MBProgressHudMPMoviePlayerController,因为我正在观察MPMoviePlayer的负载状态通知,但不知何故,方法观察通知从未观察到除MPMovieLoadStatePlayable之外的负载状态的通知。当视频开始流媒体播放时,我会显示MBProgressHud,但播放后它不起作用,然后暂停下载视频。因此,在加载视频时,我无法吸引用户。如果有人有更好的方法,请提及它,或者如果以下代码中有任何问题,请告诉我。

    -(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");
    }
}

好的。。我遇到了你的问题,问题是除了MPMovieLoadStatePlayable之外,你没有收到加载状态的通知。所以在这里你可以做的是…就像下面。。。

在视图中写下以下通知didload

[[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];

在ViewDidLoad中定义它之后,实现下面这样的函数。。。。

 - (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");
}

让我知道它是否有效!!!

快乐编码!!!!

相关内容

  • 没有找到相关文章

最新更新