1

当在表格视图中选择特定项目时,我想使用 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
4

1 回答 1

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

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

于 2013-05-29T06:47:23.490 回答