2013-01-07 63 views
0

我一直在嘗試使用MPMoviePlayerController播放視頻,播放大約一週,但收效甚微!我終於把它加載到它加載視頻的階段(或者至少我認爲它的確如此,因爲它可以告訴我視頻的持續時間)。MPMoviePlayer加載視頻,但永遠不會進入readyToPlay狀態

但是,我的視頻永遠無法播放,因此我只是呈現一個黑色方塊。

我的代碼如下:

- (void)loadMovieWithURL:(NSURL *)movieURL 
{ 
    NSLog(@"Video URL = '%@'", [movieURL path]); 
    NSData *data = [NSData dataWithContentsOfURL:movieURL]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil]; 

    MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] init]; 
    self.moviePlayer = mp; 
    [self.moviePlayer.view setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
    [self addSubview:self.moviePlayer.view]; 
    [self.moviePlayer setContentURL:movieURL]; 
    self.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 

    [self.moviePlayer setFullscreen:NO]; 
    [self.moviePlayer setScalingMode:MPMovieScalingModeFill]; 
    [self.moviePlayer setControlStyle:MPMovieControlStyleNone]; 

    //[self.moviePlayer setShouldAutoplay:YES]; 

    [self.moviePlayer prepareToPlay]; 
    [self.moviePlayer play]; 

} 

我打印了很多的值的NSLog的只是爲了檢查他們,所以想我應該提供以下也只是他們給什麼更好的想法,我「M看到:

2013-01-07 15:33:39.858 WildMap_iOS[9887:16703] Video URL = '/Users/elliott/Library/Application Support/iPhone Simulator/6.0/Applications/A233395A-3E8F-4E35-BF42-09D9B743EA33/test.3gp' 
    2013-01-07 15:33:40.100 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Disabling autoplay for pause 
    2013-01-07 15:33:40.101 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Disabling autoplay 
    2013-01-07 15:33:40.176 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Enabling autoplay 
    2013-01-07 15:33:40.179 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Likely to keep up or full buffer: 0 
    2013-01-07 15:33:40.180 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up. 
    2013-01-07 15:33:40.181 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Likely to keep up or full buffer: 0 
    2013-01-07 15:33:40.182 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up. 
    2013-01-07 15:33:40.259 WildMap_iOS[9887:16703] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0 
    2013-01-07 15:33:40.401 WildMap_iOS[9887:16703] [MPAVController] Autoplay: Enabling autoplay 
    2013-01-07 15:33:41.344 WildMap_iOS[9887:16703] Is ready to display 0 
    2013-01-07 15:33:41.344 WildMap_iOS[9887:16703] Duration: 45.267000 

我做了設置的通知檢查MPMoviePlayerLoadStateDidChangeNotification但它永遠不會改變。有沒有人有類似的問題,或知道什麼可能是錯誤的我的代碼?如果您需要更多信息,請隨時提問。

EDIT 1(測試DANH的代碼)

當使用下列內容:

NSLog(@"Video URL = '%@'", [movieURL path]); 
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
self.moviePlayer.view.frame = self.bounds; 
[self addSubview:self.moviePlayer.view]; 
[self.moviePlayer prepareToPlay]; 

//NSData *data = [NSData dataWithContentsOfURL:movieURL]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(moviePlayerLoadStateChanged:) 
              name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; 

我的日誌:

2013-01-08 12:08:16.105 WildMap_iOS[705:16703] [MPAVController] Autoplay: Disabling autoplay for pause 
2013-01-08 12:08:16.105 WildMap_iOS[705:16703] [MPAVController] Autoplay: Disabling autoplay 
2013-01-08 12:08:16.138 WildMap_iOS[705:16703] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 
2013-01-08 12:08:16.144 WildMap_iOS[705:16703] [MPAVController] Autoplay: Enabling autoplay 
2013-01-08 12:08:16.153 WildMap_iOS[705:16703] [MPAVController] Autoplay: Likely to keep up or full buffer: 0 
2013-01-08 12:08:16.153 WildMap_iOS[705:16703] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up. 
2013-01-08 12:08:16.157 WildMap_iOS[705:16703] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0 
2013-01-08 12:08:16.177 WildMap_iOS[705:16703] [MPAVController] Autoplay: Enabling autoplay 
2013-01-08 12:08:17.244 WildMap_iOS[705:16703] Is ready to display 0 
2013-01-08 12:08:17.244 WildMap_iOS[705:16703] Duration: 45.267000 

所以它似乎是一樣的原代碼,其中從不調用moviePlayerLoadStateChanged。但是,我仍然對MPMoviePlayerController如何無法加載它可以獲取視頻的持續時間感到困惑?這可能是一個編解碼器問題?下載後有什麼方法可以檢查視頻是否可以工作?

編輯2:

好吧,這看起來像一個可能的編解碼器的問題。我現在已經用2個類似的URL(1 mov/1 3gp)嘗試過了,mov一個會播放聲音,但沒有視頻,而3gp在視頻屏幕上簡短地顯示「加載」,但當消失時不會更改爲加載狀態。任何人都可以提出一個方法來獲得這個工作,或支持更多類型的另一個電影播放器​​?

回答

0

嘗試用這種替代所有的設置代碼:

// get the url as moveURL 
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
self.moviePlayer.view.frame = self.view.bounds; 
[self.view addSubview:self.moviePlayer.view]; 
[self.moviePlayer prepareToPlay]; 

// notice what notification we subscribe to... 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(moviePlayerLoadStateChanged:) 
              name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; 

登錄狀態的變化moviePlayerLoadStateChanged :.如果這不起作用,那可能是導致問題的網址。

+0

嗨丹,感謝您的回覆。我知道MPMoviePlayerLoadStateDidChangeNotification是用來檢查加載狀態改變的,我拿出代碼,因爲它顯示沒有狀態改變沒有發生。如果URL存在問題,是否有任何理由能夠檢測電影長度等? –

+0

請參閱上面的edit1。 –

+0

如果沒有已知的,好的視頻源,很難說出這裏發生的事情。我在網上有一個參考文件,但我不能發佈這個網址,因爲它不屬於我。但我認爲你對編碼的猜測是正確的 - 我很確定我提供的代碼將播放有效的電影。 – danh

相關問題