That's exactly what I did.  I have a brand new project with just a
ViewController with 3 views: 1 to hold the MPMoviePlayerController's
view, 1 button for my first movie, and 1 button for my second movie.
When the buttons are clicked, I create NSURLs from the appropriate
files.  I've included my controller.  I just set it as the window's
rootViewController in my appDelegate.  Any comments you have are
greatly appreciated!

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


@interface MoviePlayerViewController : UIViewController {

}

@end

@interface MoviePlayerViewController()

@property (nonatomic, retain) UIView *moviePlayerHolderView;
@property (nonatomic, retain) MPMoviePlayerController *moviePlayerController;

- (void)loadBigVideoFromFile;
- (void)loadBigAudioFromFile;
- (void)loadAssetFromFileUrl: (NSURL *) fileUrl;

- (void)moviePlayerLoadStateDidChange;
- (void)moviePlayerPlaybackStateDidChange;

@end

@implementation MoviePlayerViewController

@synthesize moviePlayerHolderView = _moviePlayerHolderView;
@synthesize moviePlayerController = _moviePlayerController;

#pragma mark -
#pragma mark init/dealloc methods

- (id)init {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc {
        self.moviePlayerHolderView = nil;
        self.moviePlayerController = nil;
        
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        
    [super dealloc];
}

#pragma mark -
#pragma mark UIViewController

- (void)loadView {
    [super loadView];
        
        self.moviePlayerHolderView = [[[UIView alloc]
initWithFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
                                                                                
                                                                   
CGRectGetMinY(self.view.bounds),
                                                                                
                                                                   
CGRectGetWidth(self.view.bounds),
                                                                                
                                                                   
CGRectGetHeight(self.view.bounds) - 50)] autorelease];
        self.moviePlayerHolderView.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        [self.view addSubview:self.moviePlayerHolderView];
        
        self.moviePlayerController = [[[MPMoviePlayerController alloc] init]
autorelease];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                                                
         selector:@selector(moviePlayerLoadStateDidChange)
                                                                                
                 name:@"MPMoviePlayerLoadStateDidChangeNotification"
                                                                                
           object:self.moviePlayerController];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                                                
         selector:@selector(moviePlayerPlaybackStateDidChange)
                                                                                
                 name:@"MPMoviePlayerPlaybackStateDidChangeNotification"
                                                                                
           object:self.moviePlayerController];
        self.moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
        self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
        self.moviePlayerController.shouldAutoplay = NO;
        self.moviePlayerController.view.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        self.moviePlayerController.view.frame = 
self.moviePlayerHolderView.bounds;
        
        [self.moviePlayerHolderView addSubview:self.moviePlayerController.view];
        
        UIButton *loadAudioButton = [UIButton 
buttonWithType:UIButtonTypeRoundedRect];
        loadAudioButton.autoresizingMask =
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin;
        loadAudioButton.frame = CGRectMake(CGRectGetMinX(self.view.bounds),
                                                                           
CGRectGetMaxY(self.moviePlayerHolderView.frame),
                                                                           
CGRectGetWidth(self.view.bounds) / 2,
                                                                           50);
        [loadAudioButton addTarget:self
                                                
action:@selector(loadBigAudioFromFile)
                          forControlEvents:UIControlEventTouchUpInside];
        [loadAudioButton setTitle:@"Load Audio"
                                         forState:UIControlStateNormal];
        
        [self.view addSubview:loadAudioButton];
        
        UIButton *loadVideoButton = [UIButton 
buttonWithType:UIButtonTypeRoundedRect];
        loadVideoButton.autoresizingMask =
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin;
        loadVideoButton.frame = CGRectMake(CGRectGetMaxX(loadAudioButton.frame),
                                                                           
CGRectGetMaxY(self.moviePlayerHolderView.frame),
                                                                           
CGRectGetWidth(self.view.bounds) / 2,
                                                                           50);
        [loadVideoButton addTarget:self
                                                
action:@selector(loadBigVideoFromFile)
                          forControlEvents:UIControlEventTouchUpInside];
        [loadVideoButton setTitle:@"Load Video"
                                         forState:UIControlStateNormal];
        
        [self.view addSubview:loadVideoButton];
}

- (void)viewDidUnload {
        self.moviePlayerHolderView = nil;
        self.moviePlayerController = nil;
        
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        
    [super viewDidUnload];
}

- 
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

#pragma mark -
#pragma mark private API

- (void)loadBigAudioFromFile {
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"heath"
                                                                                
         withExtension:@"mov"];
        
        [self loadAssetFromFileUrl:fileUrl];
}

- (void)loadBigVideoFromFile {
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"heath2"
                                                                                
         withExtension:@"mov"];
        
        [self loadAssetFromFileUrl:fileUrl];
}

- (void)loadAssetFromFileUrl: (NSURL *) fileUrl {       
        [self.moviePlayerController stop];
    self.moviePlayerController.contentURL = fileUrl;
        NSLog(@"after setContentURL to %@: %@", fileUrl,
self.moviePlayerController.contentURL);
}

- (void)moviePlayerPlaybackStateDidChange {
        switch (self.moviePlayerController.playbackState) {
                case MPMoviePlaybackStateStopped:
                        NSLog(@"stopped");
                        break;
                case MPMoviePlaybackStatePlaying:
                        NSLog(@"playing");
                        break;
                case MPMoviePlaybackStatePaused:
                        NSLog(@"paused");
                        break;
                case MPMoviePlaybackStateInterrupted:
                        NSLog(@"interrupted");
                        break;
                case MPMoviePlaybackStateSeekingForward:
                        NSLog(@"seeking forward");
                        break;
                case MPMoviePlaybackStateSeekingBackward:
                        NSLog(@"seeking backward");
                        break;
                default:
                        NSLog(@"Unexpected playback state");
                        break;
        }
}

- (void)moviePlayerLoadStateDidChange {
        MPMovieLoadState loadState = self.moviePlayerController.loadState;
        
        if (loadState == MPMovieLoadStateUnknown) {
                NSLog(@"unknown");
        } else {
                MPMovieLoadState loadStates[3] = {
                        MPMovieLoadStatePlayable, MPMovieLoadStatePlaythroughOK,
MPMovieLoadStateStalled
                };
                for (int i=0; i < 3; i++) {
                        switch (loadState & loadStates[i]) {
                                case MPMovieLoadStatePlayable:
                                        NSLog(@"playable");
                                        break;
                                case MPMovieLoadStatePlaythroughOK:
                                        NSLog(@"playthroughOK");
                                        break;
                                case MPMovieLoadStateStalled:
                                        NSLog(@"stalled");
                                        break;
                                default:
                                        break;
                        }
                }
        }
        
        NSLog(@"ContentURL: %@", self.moviePlayerController.contentURL);
}

@end


-Heath Borders
heath.bord...@gmail.com
Twitter: heathborders
http://heath-tech.blogspot.com



On Mon, Mar 21, 2011 at 10:47 PM, Matt Neuburg <m...@tidbits.com> wrote:
> On Mon, 21 Mar 2011 16:04:52 -0500, Heath Borders <heath.bord...@gmail.com> 
> said:
>>I create an embedded MPMoviePlayerController thusly inside my loadView method:
>>
>>self.moviePlayerController = [[[MPMoviePlayerController alloc] init]
>>autorelease];
>>
>>// add to view, setup moviePlayerController's view frame, etc
>>
>>And I can later load a movie the user chooses thusly:
>>
>>NSURL *fileUrl = ...
>>self.moviePlayerController.contentURL = fileUrl;
>>
>>and everything works great.
>>
>>
>>However, if I set the contentURL again:
>>
>>NSURL *fileUrl2 = ...
>>self.moviePlayerController.contentURL = fileUrl2;
>>
>>This does not work, even if fileUrl2 == fileUrl1.
>>
>>When I change the contentURL, I get the following playbackState and loadState:
>>
>>After first setContentURL:
>>loadState == playable | playthroughOK
>>playbackState == playing
>>
>>After my second setContentURL:
>>playbackState == stopped
>>loadState == unknown
>>
>>I can of course create a new MPMoviePlayerController for every movie,
>>but I want to make sure this issue isn't indicative of a larger
>>problem.
>
> You shouldn't have to create a new MPMoviePlayerController. Setting the 
> contentURL to a different URL works fine. Something else must be going on at 
> your end. As always, my advice is: make a new project, reduce this to the 
> absolute simplest possible case (an MPMoviePlayerController, its view, two 
> embedded movies, and two buttons) and convince yourself. m.
>
> --
> matt neuburg, phd = m...@tidbits.com, <http://www.apeth.net/matt/>
> A fool + a tool + an autorelease pool = cool!
> Programming iOS 4!
> http://www.apeth.net/matt/default.html#iosbook
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to