2013-08-19 32 views
0

我有ipad應用程序在其中播放視頻我想當用戶播放視頻,如果它查看視頻2秒鐘,那麼它應該顯示警報時間多少時間用戶觀看視頻或持續時間如何跟蹤持續時間視頻播放在ipad應用程序

這裏是我用來播放視頻的代碼。

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

回答

0

您可以記錄開始日期和結束日期,然後比較日期以查看視頻播放了多長時間。

日期也包括時間。

所以:

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

這是你在找什麼?

0

MPMediaPlayback協議中的currentPlaybackTime屬性爲您提供了該信息。

看看MPMediaPlayback協議的currentPlaybackTime屬性。 MPMoviePlayerController遵守該協議,因此您可以直接在該類的任何實例上使用它。

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

currentPlaybackTime 播放頭的當前位置。

@property(nonatomic) NSTimeInterval currentPlaybackTime 
+0

我正在使用像這樣,但它給錯誤NSLog(@「當前時間:%g」,mp.currentPlaybackTime); –

+0

currenPlayBackTime未找到對象MPMoviePlayerViewController –

1

MPMoviePlayerController會生成通知,以通知您電影播放的狀態。

1,當電影播放器​​開始播放,暫停或開始尋找前進或後退 2,當AirPlay播放開始或結束時。詳情請點擊click here

使用通知並收集currentPlaybackTime屬性和video duration屬性以進一步計算。

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

我正在寫這個答案。+1 –

+0

我正在寫這段代碼,但它給出錯誤的loadstate沒有找到 –

+0

這是文檔http://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/Reference/Reference.html#// apple_ref/occ/instp/MPMoviePlayerController/loadState –

0

看看MPMediaPlayback Protocol

currentPlaybackTime:對於內容從服務器流式傳輸直播,該值表示從播放列表的開始的時間是第一次加載時。

相關問題