Search code examples
iphoneiosios4

Still trying to get video to play in my app


Ok, so I failed to post my code properly last time ... I am the newbie. I altered things a bit and I am trying to get the video to play out of a uiTableView. I don't get a crash, I just get a black screen for about 20 seconds and then the simulator or iPhone returns to the uiTableView. When I select "option 1", I get the alert as expected. I am using Xcode 4.2 developing for iOS 4.0.

I have been searching and banging my head for a few days and any help is appreciated.

Here is my .h

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

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

Here is my .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];
    }
}

I am pretty sure I added the framework proplerly, but JIK here is the screenshot. enter image description here


Solution

  • I have tested your code and found that your filePath string is nil. Try this :

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