2010-10-05 50 views
3

我對Obj-C和塊的想法還是比較新的。我看到這篇文章有關顯示從URL從ALAssets檢索到的圖像:display image from URL retrieved from ALAsset in iPhone從iOS中的ALAsset中檢索的URL播放視頻

除了能夠使用ALAsset框架來查看一個UITableView圖片文件,我希望能夠播放存儲在圖片文件夾中的視頻好。視頻資源就像一張圖片一樣顯示,所以我希望能夠在表格中點擊該視頻列表並讓它播放。 (換句話說,我想用Assets Library Framework播放視頻)

這可以用MPMoviePlayerController來完成嗎?

從那個帖子上面我收集我需要這個代碼函數內部獲得的資產網址的視頻文件:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) 
    { 
    }; 

如果這是正確的,我需要什麼樣的代碼放在該函數內部以便我可以通過MPMoviePlayerController成功播放視頻?

僅供參考,我的一個UITableView顯示資產代碼是在這裏:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
NSString *CellIdentifier = @"id"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 


[[cell imageView] setImage:[UIImage imageWithCGImage:[[assets objectAtIndex:indexPath.row] thumbnail]]]; 
[[cell textLabel] setText:[NSString stringWithFormat:@"Item %d", indexPath.row+1]]; 

    return cell; 
} 

這是我的第一篇文章,所以我很抱歉,如果我將它張貼錯了。感謝您的幫助

回答

3

好吧,我發現了一個可以使用該塊地吐出一個日誌資產庫地址:

void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) 
{ 

    if(result != nil) { 
     NSLog(@"See Asset: %@", result); 
     [assets addObject:result]; 

    } 
}; 

它會吐出像

"See Asset: ALAsset - Type:Video, URLs:{ 
    "com.apple.m4v-video" = "assets-library://asset/asset.m4v?id=100&ext=m4v"; " 

所以這是我可以將此資產庫地址發送到MPMoviePlayerController,它將起作用!

像這樣:

NSString *urlAddress = @"assets-library://asset/asset.m4v?id=100&ext=m4v"; 
NSURL *theURL = [NSURL URLWithString:urlAddress]; 
mp = [[MPMoviePlayerController alloc] initWithContentURL:theURL]; 
etc... 

現在我得弄清楚如何獲取URL字符串出ALAsset對象的動態,這應該不是太難搞清楚..希望

3

更容易

MPMoviePlayerViewController *moviePlayerVC =[[MPMoviePlayerViewController alloc] initWithContentURL:[[asset defaultRepresentation] url]]; 
+5

這對我不起作用。該URL包含類似'assets-library://asset/asset.MOV?id = 12345&ext = mov'。 MoviePlayerController不顯示任何內容。 – 2013-01-16 12:44:17

0

您可以通過使用獲得的asseturl

[result defaultRepresentation] url]; 
0

@ go2答案是正確的,但MoviePlayerController不顯示任何內容。

所以你還需要添加moviePlayerController.movieSourceType=MPMovieSourceTypeFile; 看下面的代碼來玩這個。

MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
[moviePlayerController.view setFrame:self.view.frame]; 
moviePlayerController.movieSourceType=MPMovieSourceTypeFile; 
moviePlayerController.fullscreen = YES; 
[moviePlayerController play];