views:

85

answers:

1

Hi,

I'm converting my iOS3 app to iOS4 and having problems with the movie player! I have followed the example here: http://is.gd/eowgi (I don't need it to work in iOS3 anymore!) and subclassed UIViewController with the following code:

#import "moviePlayer.h"

@implementation moviePlayer
@synthesize mainAppDelegate;
@synthesize mp;

- (id)initWithMovie:(NSString *)moviePath delegate:(id)delegate {
    self.mainAppDelegate = delegate;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:mainAppDelegate selector:@selector(movieFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    self.mp =  [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
    [mp setControlStyle:MPMovieControlStyleFullscreen];
    [mp setShouldAutoplay:YES];

    return self;
}

- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {

    NSLog(@"moviePlayerLoadStateChanged");

    // Unless state is unknown, start playback
    if ([mp loadState] != MPMovieLoadStateUnknown) {

        // Remove observer
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

        //Set Landscape and Rotate
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
        [[self view] setBounds:CGRectMake(0, 0, 480, 320)];
        [[self view] setCenter:CGPointMake(160, 240)];
        [[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];

        //Set frame, add view and play
        [[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
        [[self view] addSubview:[mp view]];
        [mp play];

        //Tell main app that it worked!
        [mainAppDelegate performSelector:@selector(movieLoaded:) withObject:mp];

    } else if ([mp loadState] == MPMovieLoadStateUnknown) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
        [mainAppDelegate performSelector:@selector(movieLoaded:) withObject:nil];
    }

}

- (void)dealloc {
    [mainAppDelegate release];
    [mp release];
    [super dealloc];
}

@end

moviePath is a local file that has been downloaded and saved onto the device.

The problem is that moviePlayerLoadStateChanged doesn't get called sometimes. If the file is downloaded then played from the device it works everytime, if I try to play the file from its saved location it doesn't get called - I can hear the movie sound, but can't see it as there is no view for it to play in!

I've used NSLog(@"%@", moviePath); at the top of init to see the difference and there is none - the URL (local path) is the same whichever?!

Any ideas???

Thanks

Ben.

A: 

if the URL content is corrupted or UN supported video files the MPMoviePlayerLoadStateDidChangeNotification is not called check out this fine article about trouble shooting of MPMoviecontroller in iso4 http://cocoabugs.blogspot.com/2010/08/troubleshoting-iphone.html

jeeva
Thanks for that, but MPMoviePlayerPlaybackDidFinishNotification isn't called either. I can hear the movie so it is playing, just can't display it correctly as MPMoviePlayerLoadStateDidChangeNotification isn't called!
Ben Robinson