How to play a movie in a app? - iphone

i will like to play a short movie clips in the end of my 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

Related

MPMoviePlayerController doesnot play the video its full length

I am using the MPMoviePlayerController to open a video file.The video runs fine.But suppose if the video file is 10 sec ,with 1 sec remaining the video stops.Is that its natural way of playing or should we specify something.The follow is the code used
NSURL *fileURL = [NSURL URLWithString:location];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
self.moviePlayerController.controlStyle = MPMovieControlStyleFullscreen;
self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController.view setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:self.moviePlayerController.view];
- (void)moviePlaybackComplete:(NSNotification *)notification {
NSNumber *finishReason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded) {
self.moviePlayerController = [notification object];
self.moviePlayerController.view.hidden = YES;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view removeFromSuperview];
[self.moviePlayerController release];
}
[self dismissViewControllerAnimated:YES completion:nil];
}

How to dismiss MPMoviePlayerController after stopping video

I have successfully stopped a video within 30 seconds. But I am not able to dismiss the MP MovieViewController and I want to stop activity for buffering. I have used this code.....
Video Play Code :
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Pungi" ofType:#"mp4"]];
self.movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.movie .controlStyle = MPMovieControlStyleEmbedded;
[ self.movie play];
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
self.movie.initialPlaybackTime = 0.5;
[NSTimer scheduledTimerWithTimeInterval:15.0
target:self
selector:#selector(stopVideo)
userInfo:nil
repeats:NO];
stopVideo :
[self.movie stop];
[self.movie.view removeFromSuperview];
[self.movie release];
Put your MPMoviePlayerController in a separate class and load it:
MoviePlayerViewController.h
#import <MediaPlayer/MediaPlayer.h>
#interface MoviePlayerViewController : UIViewController
#end
MoviePlayerViewController.m
#import "MoviePlayerViewController.h"
MPMoviePlayerViewController *movieController;
#interface MoviePlayerViewController ()
#end
#implementation MoviePlayerViewController
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(#"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(#"exitedFullscreen");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
[self dismissModalViewControllerAnimated:YES];
break;
case MPMovieFinishReasonPlaybackError:
break;
case MPMovieFinishReasonUserExited:
[self dismissModalViewControllerAnimated:YES];
break;
default:
break;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *videoName = #"Videoname";
NSString *filepath = [[NSBundle mainBundle] pathForResource:videoName #"movietype"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[movieController.view setFrame:CGRectMake(0, -20, 320, 480)];
[self.view addSubview:movieController.view];
[movieController.moviePlayer play];
}
ViewController.m
MoviePlayerViewController *player = [[MoviePlayerViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:player animated:YES];
What it does:
MoviePlayerViewController is a custom class which loads an MPMoviePlayerController with a Video. in the viewDidLoad method (or wherever you want it) you LOAD the MoviePlayerViewController.
([self presentModalViewController:animated])....
This has the advantage, that your main class isn't overloaded with moviecrap/definitions and you can EASILY dismiss the MoviePlayerViewController when it is finished using Notifications to check wether it has stopped or not.
if it has stopped:
[self dismissModalViewControllerAnimated:YES];
Hope this Helps!
As code below shows, you should pause and set initialPlaybackTime to -1 before actual stop. This is one of tricky things that MPMoviePlayerController provides.
[_moviePlayerController pause];
if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
}
[_moviePlayerController stop];
if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
}
[_moviePlayerController.view removeFromSuperview];
You need to add notification observer in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
Then add the method:
- (void)moviePlayerPlaybackDidFinishNotification:(NSNotification*)notification
{
[self dismissMoviePlayerViewControllerAnimated];
}
Import the some headers in your header:
#import <MediaPlayer/MediaPlayer.h>
#import <MediaPlayer/MPMoviePlayerViewController.h>
You may also need to balance your "presentMoviePlayer" call with the dismiss somewhere:
[self dismissMoviePlayerViewControllerAnimated];
if you are finished with the resource early, you may be able to release it sooner by using NotificationManager to watch for MPMoviePlayerPlaybackDidFinishNotification.
and also
- (void)dealloc {
[movie release],
movie = nil;
[super dealloc];
}
try this.......
self.movie.initialPlaybackTime = -1;
[self.movie stop];
[self.movie release];

iPhone MPMoviePlayer no video

This is code supposed to play live video/audio stream,
it work's fine, but the single problem is that it doesnt show the video,
only the audio comes not the video...
#import <MediaPlayer/MediaPlayer.h>
#implementation movieplayerViewController
-(void)awakeFromNib{
NSURL *mediaURL = [NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:mediaURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setMovieSourceType:MPMovieSourceTypeStreaming];
[mp setFullscreen:YES];
[self.view addSubview:[mp view]];
[mp prepareToPlay];
[mp play];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSError *error = [[notification userInfo] objectForKey:#"error"];
if (error) {
NSLog(#"Did finish with error: %#", error);
}
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
If you are using iOS < 4.0 then this would happen. Because in iOS 4.0 there is new Class for playing video. Hope below code helps you.
-(void)playMovieFromLocalPath:(NSString *)strPath{
NSURL *movieURL = [[NSURL alloc]initFileURLWithPath:strPath];
NSString *strVersion = [[UIDevice currentDevice] systemVersion];
float version = [strVersion floatValue];
if(version < 4.0){
MPMoviePlayerController *themovie = [[MPMoviePlayerController alloc]initWithContentURL:movieURL];
themovie.scalingMode=MPMovieScalingModeAspectFill;
[themovie play];
}
else{
MPMoviePlayerViewController *themovie = [[MPMoviePlayerViewController alloc]initWithContentURL:movieURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(DidFinishPlaybackWithReason:) name:MPMoviePlayerPlaybackDidFinishNotification object:themovie.moviePlayer];
[self presentMoviePlayerViewControllerAnimated:themovie];
}
}
-(void)DidFinishPlaybackWithReason:(NSNotification *)aNotification{
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self dismissMoviePlayerViewControllerAnimated];
}
ı wrote this way.thanks for your helps
-(void)awakeFromNib{
[self playMovieFromLocalPath:#"http://eu01.kure.tv:1935/liveedge/shaber.smil/playlist.m3u8"];
}

Play video by default in full screen

I am playing a video by using MPMoviePlayerController:
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"myvideo.MOV" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0,0, 1024, 675);
[moviePlayer play];
But requirement is that by default video should be in full screen and when I minimize it should be in above frame size.
Please help me out.
Try this.
#define degreesToRadian(x) (M_PI * (x) / 180.0)
-(void)playMovieAtURL:(NSURL*)movieURL
{
if ([NSClassFromString(#"MPMoviePlayerController") instancesRespondToSelector:#selector(view)])
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
// running iOS 3.2 or better
mViewPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
mViewPlayer.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
CGRect newFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
mViewPlayer.view.frame = newFrame;
CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
[mViewPlayer.view setTransform: landscapeTransform];
[self.view addSubview:mViewPlayer.view];
[mViewPlayer.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieDidExitFullscreen:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[mViewPlayer moviePlayer]];
#endif
}
else
{
MPMoviePlayerController *mMPPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
mMPPlayer.scalingMode=MPMovieScalingModeFill;
mMPPlayer.backgroundColor=[UIColor blackColor];
[mMPPlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mMPPlayer];
}
}
/*---------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
- (void) movieDidExitFullscreen:(NSNotification*)notification
{
//[[UIApplication sharedApplication] setStatusBarHidden:YES];
/*/ Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:YES];*/
[[NSNotificationCenter defaultCenter] removeObserver: self
name:MPMoviePlayerPlaybackDidFinishNotification
object: [notification object]];
MPMoviePlayerController *theMovie1 = [notification object];
[theMovie1.view removeFromSuperview];
[theMovie1 release];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
//[[UIApplication sharedApplication] setStatusBarHidden:YES];
/*/ Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:YES];*/
[[NSNotificationCenter defaultCenter] removeObserver: self
name:MPMoviePlayerPlaybackDidFinishNotification
object: [notification object]];
MPMoviePlayerController *theMovie1 = [notification object];
[theMovie1.view removeFromSuperview];
[theMovie1 release];
}

how to add video to app

I'm trying to make an app that plays a video, and I'm having problems. I hear the sound but see no video. I have sincerely tried looking around for a solution, but all the tips don't seem to work. Here's the code:
VideoTestViewController.m
#import "VideoTestViewController.h"
#implementation VideoTestViewController
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
NSString *moviePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"spitfiregrill_iPhone.m4v"];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
theMovie.controlStyle = MPMovieControlStyleDefault;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[theMovie play];
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
VideoTestViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface VideoTestViewController : UIViewController {
}
#end
- (void)viewDidLoad {
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//---play partial screen---
player.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:player.view];
//---play movie---
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperView];
[player autorelease];
}
Video in full screen mode -
- (void)viewDidLoad {
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
MPMoviePlayerViewController *playerViewController =
[[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
[self.view addSubview:playerViewController.view];
//---play movie---
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperView];
[player autorelease];
}
A bit of guesswork - but this is what I'd do:
VideoTestViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface VideoTestViewController : UIViewController
{
MPMoviePlayerController* m_player;
}
- (void) play;
#end
VideoTestViewController.m
#implementation VideoTestViewController
- (void) viewDidAppear:(BOOL)animated
{
[self play];
}
- (void) play
{
NSURL* url = [[NSBundle mainBundle] URLForResource:#"spitfiregrill_iPhone" withExtension:#"m4v"];
m_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[m_player.backgroundView setBackgroundColor:[UIColor blackColor]];
[m_player.view setBackgroundColor:[UIColor blackColor]];
[m_player setControlStyle:MPMovieControlStyleNone];
[[m_player view] setFrame:[self.view bounds]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[m_player play];
[self.view addSubview:[m_player view]];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[m_player.view removeFromSuperview];
[m_player stop];
[m_player release];
m_player = nil;
}
#end