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

时间:2012-11-01 18:54:17

标签: ios objective-c uitableview video mpmovieplayercontroller

我想在表格视图中选择特定项目时使用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 个答案:

答案 0 :(得分:0)

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

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