2012-12-19 143 views
0

我正在開發一個需要使用Xcode 4.5.2和iOS 6.0播放視頻的應用程序。MPMoviePlayer控制器不播放視頻

我正在使用MPMoviePlayerController來播放視頻。我遵循this tutorial。但是,我仍然無法在模擬器中播放視頻。

這裏是我的代碼:

- (IBAction)playVideo:(id)sender { 
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"Movie-1" ofType:@"mp4"]; 
NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
moviePlayerController.shouldAutoplay =YES ; 
moviePlayerController.controlStyle = MPMovieControlStyleDefault ; 
moviePlayerController.scalingMode = MPMovieScalingModeFill ; 
[moviePlayerController.view setFrame:CGRectMake(0, 0, 360, 200)]; 
[self.view addSubview:moviePlayerController.view]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(moviePlaybackComplete:) 
              name:MPMoviePlayerPlaybackDidFinishNotification 
              object:moviePlayerController]; 
[moviePlayerController play ] ; 

} 

- (void)moviePlaybackComplete:(NSNotification *)notification 
{ 
    MPMoviePlayerController *moviePlayerController = [notification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:moviePlayerController]; 

    [moviePlayerController.view removeFromSuperview]; 
} 

我已經看了很多做題,但它們都沒有解決我的問題。我還試圖創建一個containerView並將我的MPMoviePlayerController添加到我的containerView中。我沒有讓它工作。我也試過MPMoviePlayerViewController,到目前爲止我能夠使它工作。它的另一個問題,我不能setFrameMPMoviePlayerViewController。但是,我想將幀設置爲我的視頻播放器。它的大小不可能是全屏。因爲我需要在我的視頻播放器下方顯示一些按鈕。

你們有什麼想法,我該如何做到這一點?

編輯:控制檯輸出

2012-12-19 16:29:21.554 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay for pause 
2012-12-19 16:29:21.555 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay 
2012-12-19 16:29:21.565 Coverzzz[4952:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2012-12-19 16:29:27.230 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay for pause 
2012-12-19 16:29:27.231 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay 
2012-12-19 16:29:27.241 Coverzzz[4952:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2012-12-19 16:29:27.706 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay for pause 
2012-12-19 16:29:27.707 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay 
2012-12-19 16:29:27.718 Coverzzz[4952:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2012-12-19 16:29:27.919 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay for pause 
2012-12-19 16:29:27.920 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay 
2012-12-19 16:29:27.930 Coverzzz[4952:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2012-12-19 16:29:28.077 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay for pause 
2012-12-19 16:29:28.077 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay 
2012-12-19 16:29:28.088 Coverzzz[4952:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2012-12-19 16:29:28.219 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay for pause 
2012-12-19 16:29:28.220 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay 
2012-12-19 16:29:28.230 Coverzzz[4952:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2012-12-19 16:29:28.368 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay for pause 
2012-12-19 16:29:28.368 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay 
2012-12-19 16:29:28.382 Coverzzz[4952:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2012-12-19 16:29:28.511 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay for pause 
2012-12-19 16:29:28.512 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay 
2012-12-19 16:29:28.521 Coverzzz[4952:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2012-12-19 16:29:28.692 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay for pause 
2012-12-19 16:29:28.692 Coverzzz[4952:c07] [MPAVController] Autoplay: Disabling autoplay 
2012-12-19 16:29:28.702 Coverzzz[4952:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
+0

add [moviePlayerController prepareToPlay];之前[moviePlayerController播放];如果它不工作檢查文件的路徑 –

+0

我已經嘗試了prepareToPlay方法,它沒有效果,只是兩次控制檯輸出。文件的路徑也是正確的。 – limon

回答

3

我終於完成了這項工作。在iOS 5.0之後,需要將MPMoviePlayerController聲明爲屬性。所以,我補充說,作爲一個財產,它的工作。

+0

你可以標記自己的答案爲正確的 – danh

+0

沒有聲明的屬性,你可以使用:) – Rushabh

0

難道你試試這個代碼:

MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:_urlToLoad]; 
[self presentModalViewController:mp animated:YES]; 

代替:

[self.view addSubview:moviePlayerController.view]; 

希望這會有所幫助。乾杯。

+0

這是不可能的,如果我這樣做,應用程序將崩潰,因爲你不能給moviePlayerController作爲presentModalViewController的參數。測試過,崩潰 – limon

+0

哦,我明白了,因爲你使用了MPMoviePlayerController,而不是MPMoviePlayerViewController,爲什麼它沒有爲我的答案工作。我會編輯我的答案。最好。 – IKQ

+0

你可以嘗試使用MPMoviewPlayerController嗎?我使用這個類來處理youtube視頻,併爲我工作得很好。希望它可以幫助。 – IKQ