如何在应用中播放电影?

时间:2011-03-03 10:39:55

标签: iphone cocoa-touch ios4 media-player

我想在应用程序的游戏结束时播放一段短片,我将如何完成它?有关MPMovieplayer的任何教程吗?

感谢您回答我的问题

欢呼声

我目前有这些代码,如何在应用中播放它而不会弹出youtube界面?

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;

1 个答案:

答案 0 :(得分:1)

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]; 

还要将MediaPlayer.framework添加到项目中