簡體   English   中英

播放從iOS中ALAsset檢索到的URL中的視頻

[英]play video from URL retrieved from ALAsset in iOS

我對Obj-C和塊的想法還是比較新的。 我讀過這篇關於從ALAssets中檢索到的URL顯示圖像的文章: 從iPhone中的ALAsset檢索到的URL顯示圖像

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

可以用MPMoviePlayerController完成嗎?

從上面的那篇文章中我收集到了我需要在此函數中的代碼來獲取視頻文件的資產URL:

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;
}

這是我的第一篇帖子,所以如果我發錯了,我很抱歉。 謝謝你的幫助

好的,我發現可以使用此塊來吐出資產庫地址的日志:

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...

現在我只想弄清楚如何動態地從ALAsset對象中獲取該URL字符串,這不應該太難以理解..希望

更容易

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

您可以使用以下方式獲取asseturl

[result defaultRepresentation] url];

@ 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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM