开发者

How to play a movie in a app?

i will like to play a short movie clips in the end of m开发者_如何学Goy game in the apps, how will i need to done it ? is there any tutorial on MPMovieplayer ?

thanks for answering my questions

cheers

i currently have these code, how do i play it in the app without poping out the youtube interface ?

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"EndVideo" ofType:@"m4v"]];

MPMoviePlayerViewController * playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

[self presentMoviePlayerViewControllerAnimated:playercontroller];

playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[playercontroller release]; playercontroller = nil;


refer

http://stackoverflow.com/questions/5015165/playing-a-video-file-from-server-in-an-iphone-app

I am playing a movie stored on server.Even if you have local file you can convert the path to NSURL & use it


    Create a NSObject class CustomMoviePlayerViewController

In the .h file 

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


@interface CustomMoviePlayerViewController : UIViewController 
{ 
    MPMoviePlayerController *mp;
    NSURL *movieURL;

}


- (id)initWithPath:(NSString *)moviePath;
- (void)readyPlayer;

@end

In the .m file

//
//  CustomMoviePlayerViewController.m
//

#import "CustomMoviePlayerViewController.h"

#pragma mark -
#pragma mark Compiler Directives & Static Variables

@implementation CustomMoviePlayerViewController

-(void)viewWillDisappear:(BOOL) animated
{  



    if(mp) {
        [mp stop];
    }


}



- (id)initWithPath:(NSString *)moviePath
{ 
    mp.controlStyle =  MPMovieControlStyleNone;
    // Initialize and create movie URL
    if (self = [super init])
    {
        movieURL = [NSURL fileURLWithPath:moviePath];    
        [movieURL retain];

    }
    return self;
}


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

        [mp setControlStyle:MPMovieControlStyleFullscreen];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
    NSString *deviceModel = [NSString stringWithFormat: @"Device Type: %@\n", [[UIDevice currentDevice] model]];    
    NSRange range = [deviceModel rangeOfString:@"iPad"];


    if(range.location != NSNotFound){
        if ([mp loadState] != MPMovieLoadStateUnknown)
        {
            [[NSNotificationCenter  defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

            [[self view] setBounds:CGRectMake(0, 0, 1024, 748)];


            UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
            if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft ){

                [[mp view] setFrame:CGRectMake(0, 0, 480, 320)];

            }
            else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight ){

                [[mp view] setFrame:CGRectMake(0, 0, 480, 320)];


            }

            [[self view] addSubview:[mp view]];   
            [mp play];

        }
    }
    else

    {  

        if ([mp loadState] != MPMovieLoadStateUnknown)
        {
            [[NSNotificationCenter  defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
            UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
            if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
                [[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:NO];
            }
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
            [[self view] setBounds:CGRectMake(0, 0, 1024, 768)];
            [[self view] setCenter:CGPointMake(160, 240)];
            [[mp view] setFrame:CGRectMake(0, 0, 1024, 748)];
            [[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
            [[self view] addSubview:[mp view]];   
            [mp play];

        }
    }
#endif

}


/*---------------------------------------------------------------------------
 * For 3.1.x devices
 * For 3.2 and 4.x see moviePlayerLoadStateChanged: 
 *--------------------------------------------------------------------------*/

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

    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
    [mp play];

}

/*---------------------------------------------------------------------------
 * 
 *--------------------------------------------------------------------------*/
- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{    
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [self dismissModalViewControllerAnimated:NO];   

}

/*---------------------------------------------------------------------------
 *
 *--------------------------------------------------------------------------*/
- (void) readyPlayer
{   
    //NSLog (@"readyPlayer");
    [[UIApplication sharedApplication] setStatusBarHidden:YES];

    mp =  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

    mp.scalingMode= MPMovieScalingModeAspectFit;


    if ([mp respondsToSelector:@selector(loadState)]) 
    {
        // Set movie player layout
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200 

        //[mp setControlStyle:MPMovieControlStyleFullscreen];
        [mp setControlStyle:MPMovieControlStyleNone];
        [mp setFullscreen:YES];
#endif
        file://localhost/Users/ngamacbook/Documents/iPhoneProjectDocs/Team%20Projects/FietIpad_21Feb/Fiet-iPad(05-01-11)
        // May help to reduce latency
        [mp prepareToPlay];

        // Register that the load state changed (movie is ready)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200 

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
#endif
    }  
    else
    {
        // Register to receive a notification when the movie is in memory and ready to play.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
        //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

    }

    // Register to receive a notification when the movie has finished playing. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}

/*---------------------------------------------------------------------------
 * 
 *--------------------------------------------------------------------------*/

- (void) loadView
{ 

    [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
    [[self view] setBackgroundColor:[UIColor blackColor]];
    self.view.clipsToBounds = YES;
    self.view.autoresizesSubviews = YES;

    if(mp) {
        [mp stop];
    }

    UIInterfaceOrientation interfaceOrientation = [self interfaceOrientation];
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
        [[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:NO];
    }       
}


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




@end


whenever you want to play the file


    NSString *movieFile=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];

    // Create custom movie player   
    moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithPath:movieFile] autorelease];

    [self presentModalViewController:moviePlayer animated:NO];
    [moviePlayer readyPlayer]; 

Also add MediaPlayer.framework to your project

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜