仍在尝试在我的应用中播放视频

时间:2012-01-24 23:09:18

标签: iphone ios ios4

好的,所以上次我没能正确发布我的代码......我是新手。我改变了一些东西,我试图让视频从uiTableView中播放出来。我没有崩溃,我只是黑屏大约20秒,然后模拟器或iPhone返回到uiTableView。当我选择“选项1”时,我会按预期收到警报。我正在使用Xcode 4.2开发iOS 4.0。

我一直在寻找和敲打我的头几天,感谢任何帮助。

这是我的.h

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h> 

@interface faq2 : UITableViewController 
{
    NSArray *faqList;
}
@property (nonatomic, strong) NSArray *faqList;
-(IBAction)playMovie;
@end

这是我的.m

-(IBAction)playMovie 
{  
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"WhatFinalTake" ofType:@"mp4"];  
    NSURL    *fileURL = [NSURL fileURLWithPath:filepath];  
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];  

    [[NSNotificationCenter defaultCenter] addObserver:self  
                                             selector:@selector(moviePlaybackComplete:)  
                                                 name:MPMoviePlayerPlaybackDidFinishNotification  
                                               object:moviePlayerController];  

    [self.view addSubview:moviePlayerController.view];  
    moviePlayerController.fullscreen = YES;  
    [moviePlayerController play];   
}

- (void)moviePlaybackComplete:(NSNotification *)notification  
{  
    MPMoviePlayerController *moviePlayerController = [notification object];  
    [[NSNotificationCenter defaultCenter] removeObserver:self  
                                                    name:MPMoviePlayerPlaybackDidFinishNotification  
                                                  object:moviePlayerController];  

    [moviePlayerController.view removeFromSuperview];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.faqList = [[NSArray alloc] initWithObjects:
                    @"Play Movie",
                    @"Option 1", nil];

    self.title = @"Frequently Asked Questions";
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.faqList = nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [faqList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell.
    cell.textLabel.text = [self.faqList objectAtIndex: [indexPath row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        [self playMovie];
    }
    else if (indexPath.row == 1)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Option 1" message:@"Option 1" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil, nil];
        [alert show];
    }
}

我很确定我已经添加了框架,但是这里的JIK是截图。 enter image description here

2 个答案:

答案 0 :(得分:0)

我测试了您的代码,发现您的filePath字符串为nil。试试这个:

NSString *filepath = [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] pathForResource:@"WhatFinalTake" ofType:@"mp4"]];  
NSLog(@"%@",filepath);
NSURL    *fileURL = [NSURL fileURLWithPath:filepath];  

答案 1 :(得分:0)

您是否已将“WhatFinalTake.mp4”文件添加到项目中?

我发现以下内容对播放视频很有帮助,您可以从tableView中调用它:

http://iosdevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html

我发现注册通知LoadStateChanged或PreloadDidFinish(取决于iOS版本)是有用的,并从那里进行实际播放以确保视频已准备好。您可以从上面的链接中找到如何执行此操作。由于他已经为你完成了所有工作,我建议只使用提供的代码。