3

我试图向我展示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");

    }

}
4

1 回答 1

3

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

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

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

让我知道它是否有效!

快乐编码!!!!

于 2013-08-05T07:37:53.070 回答