0
我想從服務器使用MPMoviePlayerController和NSURL播放視頻。視頻播放完美,但需要大量時間加載。以下是我的代碼:視頻需要很長時間在加載iphone
- (void) readyPlayer {
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if ([mp respondsToSelector:@selector(loadState)]) {
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
else{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
[lblActivity removeFromSuperview];
[activity stopAnimating];
// Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
{
// Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
// Rotate the view for landscape playback
[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI/2)];
// Set frame of movieplayer
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
// Add movie player as subview
[[self view] addSubview:[mp view]];
// Play the movie
[mp play];
}
}
有人能幫我弄清楚爲什麼它會花費這麼多時間在加載?
感謝
潘卡
嗨Madhumal,感謝您的回覆我已按照您的建議,並刪除[mp prepareToPlay]線。但在播放視頻時仍需要近28秒。我的網絡連接沒問題,播放流式視頻通常不需要太多時間。 – pankaj
嗨pankaj,只是爲了在類似條件下測試它,你嘗試從同一個手機(不是在你的桌面瀏覽器或任何東西)從Safari加載相同的URL?如果這會導致加載時間明顯加快,那麼可以認爲這可能是您的應用程序的問題。 – Madhu
28秒遠遠超過常規應用程序/流所需的時間。通常我們正在說3-10秒。 – Till