【问题标题】:how to track the time duration video played in ipad app如何跟踪在 ipad 应用中播放的视频时长
【发布时间】:2013-08-20 20:22:39
【问题描述】:

我有 ipad 应用程序,我可以在其中播放视频

这是我用来播放视频的代码。

            [[mp moviePlayer] prepareToPlay];
    [[mp moviePlayer] setUseApplicationAudioSession:NO];
    [[mp moviePlayer] setShouldAutoplay:YES];
    [[mp moviePlayer] setControlStyle:2];
    //[[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
    [self presentMoviePlayerViewControllerAnimated:mp];

【问题讨论】:

    标签: iphone ios video


    【解决方案1】:

    您可以记录开始日期和结束日期,然后比较日期以查看视频播放了多长时间。

    日期也包括时间。

    所以:

    1) 当用户按下播放时,你可以去:

    NSDate *startPlayDate = [NSDate date];
    

    2) 当用户停止播放视频时,你可以去:

    NSDate *stopPlayDate = [NSDate date];
    

    3) 现在比较两个日期的差异,您应该看到用户播放视频的秒数:

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    
    NSDateComponents* components = [gregorianCalendar components: NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
                                                        fromDate:startPlayDate 
                                                          toDate:stopPlayDate 
                                                         options:0] ;
    
    NSString *message = [[NSString alloc] initWithFormat:@"Video played for: %d hours, %d minutes, %d seconds", components.hour, components.minute, components.second];
    
    UIAlert *alert = [[UIAlertView alloc] initWithTitle:@"Duration"
                                                message:message
                                               delegate:self
                                           cancelButton:@"OK"
                                            otherButton:nil, nil];
    
    [alert show];
    

    这就是你要找的吗?

    【讨论】:

      【解决方案2】:

      看看MPMediaPlayback Protocol

      currentPlaybackTime:对于从服务器实时流式传输的内容,此值表示从播放列表开始首次加载的时间。

      【讨论】:

        【解决方案3】:

        MPMoviePlayerController 会生成通知,让您的应用了解电影播放的状态。

        1,当电影播放器​​开始播放、暂停或开始向前或向后搜索时 2、AirPlay播放开始或结束时。详情请click here

        使用通知并收集 currentPlaybackTime 属性和视频持续时间属性以进行进一步计算。

        - (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification
        {
            if ((player.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK)
            {
                NSLog(@"content play length is %g seconds", player.duration);
            }
        }

        【讨论】:

        • 我正在写这个答案。 +1
        • 我正在写这段代码,但它给出了错误 loadstate not found
        • 你能在这里编辑吗?我只是按照你给的方法调用这个方法
        • 确保你声明了一个类变量 player。它是 MPMoviePlayerController 的一个对象。
        【解决方案4】:

        MPMediaPlayback 协议中的 currentPlaybackTime 属性为您提供了该信息。

        查看MPMediaPlayback 协议的currentPlaybackTime 属性。 MPMoviePlayerController 遵循该协议,因此您可以直接在该类的任何实例上使用它。

        MPMoviePlayerController *player = [...];
        [...]
        NSLog(@"current time: %g", player.currentPlaybackTime);
        From the MPMediaPlayback Reference;
        

        当前播放时间 播放头的当前位置。

        @property(nonatomic) NSTimeInterval currentPlaybackTime
        

        【讨论】:

        • 我是这样使用的,但它给出了错误 NSLog(@"current time: %g", mp.currentPlaybackTime);
        • currenPlayBackTime 未找到对象 MPMoviePlayerViewController
        猜你喜欢
        • 2017-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-14
        • 2016-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多