2011-09-07 143 views
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]; 
    } 
} 

有人能幫我弄清楚爲什麼它會花費這麼多時間在加載?

感謝
潘卡

回答

0

我用手機上的互聯網連接假設它顯然不是一個問題。您是否嘗試從您的應用和另一本機應用(例如YouTube或Safari)上加載手機上的相同視頻,以查看是否有區別?

看着我們所做的類似代碼,我想知道需要明確調用[mp prepareToPlay];方法。當play被調用時它會自動調用,所以我們沒有明確這樣做。你能否在沒有這種方法的情況下嘗試一下,看看是否有區別?

希望這會有所幫助!

+0

嗨Madhumal,感謝您的回覆我已按照您的建議,並刪除[mp prepareToPlay]線。但在播放視頻時仍需要近28秒。我的網絡連接沒問題,播放流式視頻通常不需要太多時間。 – pankaj

+0

嗨pankaj,只是爲了在類似條件下測試它,你嘗試從同一個手機(不是在你的桌面瀏覽器或任何東西)從Safari加載相同的URL?如果這會導致加載時間明顯加快,那麼可以認爲這可能是您的應用程序的問題。 – Madhu

+0

28秒遠遠超過常規應用程序/流所需的時間。通常我們正在說3-10秒。 – Till

相關問題