2010-11-04 69 views
39

我試圖加載視頻文件到我的iPad應用作爲AVURLAsset,使用異步加載的東西來等待它準備就緒。問題是,當我運行它時,我得到一個完全通用的「失敗」錯誤消息,我不知道該如何處理。該視頻的作品如果我把它交給MPMoviePlayerController,但AVURLAsset似乎拒絕與它有任何關係。AVURLAsset拒絕加載視頻

代碼:

asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:[docPath stringByAppendingPathComponent:@"video.mov"]] options:nil]; 
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self composeIfReady]; 
    }); 
}]; 

...

- (void)composeIfReady 
{ 
    NSError *error = nil; 
    if([asset statusOfValueForKey:@"tracks" error:&error] == AVKeyValueStatusFailed) 
     NSLog(@"error loading: %@", [error description]); 
    if(error == nil) 
     NSLog(@"okay awesome"); 
} 

輸出:

error loading: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation couldn’t be completed. (AVFoundationErrorDomain error -11800.)" UserInfo=0x1696f0 {NSUnderlyingError=0x169a40 "The operation couldn’t be completed. (OSStatus error -12936.)"} 

-11800,順便說一句,是 「未知錯誤」 的錯誤代碼。一種死衚衕。有任何想法嗎?在我嘗試加載資產之前是否應該設置一些內容?

回答

110

已解決。訣竅:使用fileURLWithPath:,而不是URLWithString:。顯然,這種差異真的很重要。

+27

我正在使用fileURLWithPath,仍然得到相同的錯誤。 – gngrwzrd 2012-01-18 17:59:19

+0

+1 This work for me..Thanks much .. – 2012-05-04 06:14:29

+1

謝謝你verryy多!!!我現在意識到差異很重要,因爲它需要一個絕對的URL到URL,所以URLWithString給出的相對URL(/ var/mobile/...)不起作用。 – 2012-05-22 10:08:59

6

如果任何人在使用fileURLWithPath之後仍然有問題,請嘗試在time數組中請求NSTimeIntervals(如果使用的是int)。

這不起作用:

NSMutableArray *playbackTimes = [NSMutableArray array]; 
    for (int i = 0; i <= 10; i ++) { 
     [playbackTimes addObject:@(i)]; 
    } 

    [self.videoPlayer requestThumbnailImagesAtTimes:playbackTimes timeOption:MPMovieTimeOptionNearestKeyFrame]; 

這工作:

NSMutableArray *playbackTimes = [NSMutableArray array]; 
    for (int i = 0; i <= 10; i ++) { 
     [playbackTimes addObject:@((NSTimeInterval)i)]; 
    } 

    [self.videoPlayer requestThumbnailImagesAtTimes:playbackTimes timeOption:MPMovieTimeOptionNearestKeyFrame]; 
+0

這是我的問題謝謝, Edwin – 2014-06-16 11:01:17

+0

你可以做一個這樣的快速版本嗎?請 – rottenoats 2016-08-22 15:15:14

5

如果使用-[NSURL fileURLWithPath:]創建網址,仍然得到同樣的錯誤。

檢查AVURLAssetReferenceRestrictionsKey, 如果本地m3u8包含遠程資源,則可能無法播放。

設置值爲@(AVAssetReferenceRestrictionForbidNone)應該解決問題。

+1

你在哪裏設置? – CodyMace 2016-12-08 20:26:28

+0

@CodyMace這是'AVURLAsset'的選項鍵,參見' - [AVURLAsset URLAssetWithURL:options:]' – naituw 2017-05-10 05:33:51