iphone - 将 MBProgressHud 添加到 MPMoviePlayerController

标签 iphone ios objective-c

我正在尝试向 MPMoviePlayerController 显示 MBProgressHud,因为我正在观察 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 之外的加载状态通知。所以在这里你可以做的是......就像下面......

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

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

快乐编码!!!!

关于iphone - 将 MBProgressHud 添加到 MPMoviePlayerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18051318/

相关文章:

iphone - 滚动 UITableView 时单元格图像发生变化

ios - AlamofireObjectMapper/ObjectMapper是否支持struct类型映射

iphone - 在 UItableview 中添加一行

objective-c - 将 Objective C 自引用结构转换为 Swift

ios - 为什么不同swift类中的函数没有被调用?

iphone - 用于 iPhone 编程的物理库

iphone - CoreBluetooth 与 Lego NXT 配对

ios - Swift:按整数加载级别类

ios - sqlite3_step 在将数据插入 sqlite 时返回错误代码 5

ios - 如何重新创建支持静态单元格的 UITableViewController?