从表视图选择中播放视频 URL

Posted

技术标签:

【中文标题】从表视图选择中播放视频 URL【英文标题】:Playing Video URL from Table View Selection 【发布时间】:2012-11-01 18:54:16 【问题描述】:

当在表格视图中选择特定项目时,我想使用 MediaPlayer 框架在 xcode 中播放视频。我已将我的代码放在下面,但不确定从这里开始。具体来说,我不确定如何将列表视图项与 url 相关联。

//  ViewController.m
//  Test
//


#import "ViewController.h"

@implementation ViewController
@synthesize tableData;


- (void)viewDidLoad


    tableData = [[NSArray alloc] initWithObjects:@"Bacon", @"Banana", @"Big", @"Billious", nil];

    [super viewDidLoad];


#pragma mark - TableView Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView
    numberOfRowsInSection:(NSInteger)section

    return [tableData count];


- (UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath


    UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

    if (cell == nil)
    
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
    

    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

    return cell;



#pragma mark - TableView Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath



    NSString *moviePath = @"http://specified URL";
    MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:moviePath]];
    [theMovie play];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:moviePath]];
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];





- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


@end

【问题讨论】:

【参考方案1】:

objectAtIndex:indexPath.row 不包含 urlString,因为您的数组 tabledata 没有任何 url。尝试获取特定元素的url,然后在moviePath中使用它。

NSURL *url= [NSURL URLWithString:@"http://www.xyz.com/video/x109z02"];
theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
theMoviPlayer.scalingMode = MPMovieScalingModeFill;
theMoviPlayer.view.frame = CGRectMake(0, 60, 320, 350);
[self.view addSubview:theMoviPlayer.view];
[self.theMoviPlayer play];

还添加框架,然后将其导入头文件。

【讨论】:

以上是关于从表视图选择中播放视频 URL的主要内容,如果未能解决你的问题,请参考以下文章

如何在 android 的 videoview 中播放 .mp4 视频?

iOS SwiftUI 视频自动播放

使用 AVPlayer 从远程 URL 播放视频

无法在videoview android中播放来自url的视频

如何在 Ipad 中播放来自 URL 的视频?

如何用vlc播放器的网络串流