2013-12-17 34 views
0

我正在製作一個iphone應用程序,其中從url服務器播放視頻的效果很好,但我想讓我們說視頻是3分鐘或4分鐘用戶觀看視頻的時間它播放視頻1分鐘,並同樣停止。如何獲得在iOS應用程序中觀看的持續時間視頻

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

回答

1

我想,你可以提出MPMoviePlayerViewController的時間開始NSTimer,聽通知MPMoviePlayerPlaybackDidFinishNotificationMPMoviePlayerPlaybackDidFinishReasonUserInfoKey和計算時間。

編輯

最好的辦法是用MPMoviePlayerPlaybackDidFinishNotification通知

這會給你的實際時間來訪問MPMediaPlayback的currentPlaybackTime財產。 你的情況,你可以訪問此屬性爲

NSTimeInterval time = mp.moviePlayer.currentPlaybackTime; 
+0

如果什麼互聯網是緩慢和2 mintues視頻拿5 mintues發揮 – user3110080

+0

我編輯我的答案,請檢查並讓我知道。 – Suryakant

0

如果妳檢查的參考手冊MPMoviePlayerController符合MPMediaPlayback協議

所以MPMediaPlayback協議包含屬性

@property(nonatomic) NSTimeInterval currentPlaybackTime 

的當前位置播放頭。 (必填)

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

NSTimeInterval timeInterval = player.currentPlaybackTime; 

鏈接:

MPMoviePlayerController

MPMediaPlayback

希望這有助於!

+0

如何使用這個我正在使用這個但不工作 – user3110080

+0

我已經添加了一個例子。 – iCoder

0

可以使用通知中心:

1-論viewDidLoad中:

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

2-實現此方法(秒是int):

- (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(視頻開始) MPAVControllerNew StateParameter == 1(視頻停止) MPAVControllerNewStateParameter == 0(視頻結束或者按 「完成」)

3-最後實現此方法:

-(void) increaseSeconds { 
    seconds++; 
} 
相關問題