【问题标题】:how to get the time duration video watched in iOS app如何获取在 iOS 应用中观看的持续时间视频
【发布时间】:2013-12-17 10:40:15
【问题描述】:

我正在制作一个 iphone 应用程序,在该应用程序中从 url 服务器播放视频效果很好,但我想说视频是 3 分钟或 4 分钟,用户观看视频的时间是多少,就像播放视频 1 分钟一样停止。

NSURL *url = [NSURL URLWithString:newString];
NSLog(@"New File name is %@",newString);            
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setUseApplicationAudioSession:NO];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
//[[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
[self presentMoviePlayerViewControllerAnimated:mp];

【问题讨论】:

    标签: ios iphone mpmediaplayercontroller


    【解决方案1】:

    我想,你可以在呈现MPMoviePlayerViewController的时候启动NSTimer,然后收听MPMoviePlayerPlaybackDidFinishNotificationMPMoviePlayerPlaybackDidFinishReasonUserInfoKey的通知并计算时间。

    编辑

    最好的方法是使用MPMoviePlayerPlaybackDidFinishNotification通知访问MPMediaPlayback的currentPlaybackTime属性

    这将为您提供实际时间。 在您的情况下,您可以以

    的身份访问此属性
      NSTimeInterval time =  mp.moviePlayer.currentPlaybackTime;
    

    【讨论】:

    • 如果网速很慢,2 分钟的视频需要 5 分钟才能播放
    【解决方案2】:

    您可以使用通知中心:

    1- 在 vi​​ewDidLoad 上:

     [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackStateDidChange:)
                                              name:@"MPAVControllerPlaybackStateChangedNotification"
                                               object:nil];
    

    2- 实现这个方法(秒是一个整数):

    - (void)playbackStateDidChange:(NSNotification *)note {
        NSLog(@"note.name=%@ state=%d", note.name, [[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue]);
    
    if ([[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue] == 2) {
        timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(increaseSeconds) userInfo:nil repeats:YES];
        NSLog(@"seconds: %i", seconds);
    } else if([[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue] == 1){
        [timer invalidate];
        NSLog(@"seconds: %i", seconds);
    } else if([[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue] == 0){
        NSLog(@"Total watched: %i", seconds);
    
        [self dismissMoviePlayerViewControllerAnimated];
    }   
    

    }

    其中 MPAVControllerNewStateParameter == 2(视频开始) MPAVControllerNewStateParameter == 1(视频停止) MPAVControllerNewStateParameter == 0(视频完成或按下“完成”)

    3- 最后实现这个方法:

    -(void) increaseSeconds {
        seconds++;
    }
    

    【讨论】:

      【解决方案3】:

      如果您检查参考手册MPMoviePlayerController 符合MPMediaPlayback 协议

      所以MPMediaPlayback 协议包含该属性

      @property(nonatomic) NSTimeInterval currentPlaybackTime
      

      播放头的当前位置。 (必填)

      示例

      MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
      ...
      
      NSTimeInterval timeInterval = player.currentPlaybackTime;
      

      链接:

      MPMoviePlayerController

      MPMediaPlayback

      希望这会有所帮助!

      【讨论】:

      • 我添加了一个例子。
      猜你喜欢
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      相关资源
      最近更新 更多