2011-07-30 30 views
2

我從ALAssetsLibrary(基本上是從本機相機應用程序記錄的所有內容)提取所有視頻資產。我然後在每個視頻的資產,這樣做是爲了每個視頻運行枚舉:AVAsset從ALAsset URL創建時沒有軌道或持續時間

// The end of the enumeration is signaled by asset == nil. 
      if (alAsset) { 

       //Get the URL location of the video 
       ALAssetRepresentation *representation = [alAsset defaultRepresentation]; 
       NSURL *url = [representation url]; 

       //Create an AVAsset from the given URL 
       NSDictionary *asset_options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey]; 
       AVAsset *avAsset = [[AVURLAsset alloc] initWithURL:url options:asset_options];//[AVURLAsset URLAssetWithURL:url options:asset_options]; 

       //Here is the problem 
       NSLog([NSString stringWithFormat:@"%i", [avAsset.tracks count]]); 
       NSLog([NSString stringWithFormat:@"%f", CMTimeGetSeconds(avAsset.duration)]); 
      } 

的NSLog報道說,我已經從我的ALAsset得到的AVAsset有0磁道,並具有0.0秒的持續時間。我檢查了網址,它是「assets-library://asset/asset.MOV?id = 9F482CF8-B4F6-40C2-A687-0D05F5F25529 & ext = MOV」,這似乎是正確的。我知道alAsset實際上是一個視頻和正確的視頻,因爲我已經顯示了alAsset.thumbnail,並且它顯示了視頻的正確縮略圖。

這一切都讓我相信在avAsset的初始化過程中出現了問題,但對於我的生活,我無法弄清楚什麼是錯誤的。誰能幫我?

更新:

我想我已經證實,該網址被ALAssetRepresentation給我是錯誤的,這是奇怪的,因爲它給了我正確的縮略圖。我添加了這個代碼:

NSLog([NSString stringWithFormat:@"%i", [url checkResourceIsReachableAndReturnError:&error]]); 
NSLog([NSString stringWithFormat:@"%@", error]); 

它給了我這樣的:

0 
Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x19df60 {} 

我仍然不知道什麼原因這一點。我唯一注意到的是url,它是「assets-library://asset/asset.MOV?id = 9F482CF8-B4F6-40C2-A687-0D05F5F25529 & ext = MOV」與我所見過的不同因爲我一直在爲此尋找。我在別處看到的那個看起來更像是「assets-library://asset/asset.MOV?id = 1000000394 & ext = MOV」,用數字代替字母數字,短劃線分隔的名稱。

如果有幫助,我使用的是XCode 4.2 Beta和iOS5。請讓我知道你是否可以想到任何事情。謝謝。

回答

0

好吧,看起來這是iOS5 beta v1中的一個錯誤。我升級到最新,它的工作。感謝那些看過我的問題的人。

+0

在iOS 7中可以嗎?我在ios7中遇到了問題。 –