MPMoviePlayerController doesn't play video frame ios7 - iphone

I am porting app from iOS6 to iOS7. There is a weird problem that makes the screen go black after the call from a button is being done. I have tried out this, this and this.
There is no apt answer and I don't feel theoretically there should be any problem in using the previous methods.
Kindly provide me some thread to why this problem is occuring.
Thanks

This can help
Import MediaPlayer in your .h file
#import <MediaPlayer/MediaPlayer.h>
Make a property
#property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
After that you can play video by this
-(void)playMovie:(id)sender
{
NSURL *url = [NSURL URLWithString:
#"http://www.xyz.com/ios_book/movie/movie.mov"];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}
After that for removing the video view add this
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}

Add your movie player controller view on main windows like:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview: player.view];
[player play];
Hope so it will work for you!

Try this way..
ViewController.m
MPMoviePlayerViewController *mp=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:[[arr_videos objectAtIndex:indexPath.row]valueForKey:#"Video_path"]]];
MPMoviePlayerController *pc=[mp moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentViewController:mp animated:YES completion:nil];
[pc prepareToPlay];
[pc play];

Related

How to get video player to NOT auto play while using MPMoviePlayerController

I'm trying to code a video into my iPhone app and the code below works perfectly but the only thing is that I rather the video not automatically start playing when the user opens the page. I want them to be able to push the play button. The code below is located in my viewcontroller.m file.
(void)viewDidLoad
{
NSString *url = [[NSBundle mainBundle] pathForResource:#"77860_00_01_MM02_welcome"
ofType:#"mov"];
player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
player.view.frame = CGRectMake(10, 235, 150, 125);
[self.view addSubview:player.view];
[player play];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *moviePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[player play];
}
Never mind, I figured it out. I just added [player prepareToPlay] under player.shouldAutoplay = NO
Don't put it in your viewDidLoad. Just create an IBAction them link it up to your trigger button.

How to play a video like you tube player in iphone sdk?

I am newbie for ios development, I am working on a project ,where i need to show some sample videos.
i Would like to show the video in streaming format(youtube player) .
-(void) playVideoOfURL:(NSString*)videoPath
{
NSString *url = [NSString stringWithFormat:#"%#%#",TipsService,videoPath];
moviePlayer = [[ MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
if (moviePlayer)
{
moviePlayer.view.frame = CGRectMake(0, 0, 320, 460);
moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.useApplicationAudioSession = YES;
//Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
//setup device rotation notification observer
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
UIViewController *player = [[UIViewController alloc] init];
player.view = moviePlayer.view;
[self.navigationController pushViewController:player animated:YES];
[moviePlayer play];
[moviePlayer setFullscreen:TRUE];
}
}
I tried this one, but no use. Please help me...
Try this
[moviePlayer play];
[moviePlayer setFullscreen:TRUE];
UIViewController *player = [[UIViewController alloc] init];
player.view = moviePlayer.view;
[self.navigationController pushViewController:player animated:YES];

How do you play a movie in an iphone app?

I want to add a movie at the beginning of my iphone app. I have watched the tutorials and this is the code I have come up with. Why won't it work. I don't even see a movie. I call it from the viewDidLoad function of my ViewController with the following code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"BTS Intro" ofType:#"mov"];
[self playMovieAtURL:[NSURL fileURLWithPath:path
-(void)playMovieAtURL:(NSURL *)theURL {
MPMoviePlayerController *thePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
thePlayer.scalingMode = MPMovieScalingModeAspectFill;
thePlayer.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:thePlayer];
[thePlayer play];
}
-(void)myMovieFinishedCallback:(NSNotification *)aNotification {
MPMoviePlayerController *thePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:thePlayer];
[thePlayer release];
}
You initiated playing of a movie but didn't add [thePlayer view] into views hierarchy that is why you do not see anything. Take a look at description of this operation in apple docs:
When you add a movie player’s view to your app’s view hierarchy, be sure to size the frame correctly, as shown here:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];
MPMoviePlayerController Class Reference

MPMoviePlayerViewController can not work on iOS 4.3 on iPhone or iPad

I now have a strange problem when I try to play movie through MPMoviePlayerViewController on iOS 4.3.
It appears just a white screen and nothing happens. My code works well on iOS 4.2. So I created a new
OpenGLES project and just use my movie code, but it can not work either. So can anybody give some tips?
Thanks in advance.
My code is below:
//-------------------------------------TestViewController.h---------------------------------------
#import <MediaPlayer/MediaPlayer.h>
#interface TestViewController : UIViewController
{
bool _isMovieEnded;
MPMoviePlayerViewController* _theMovie;
}
- (void)playMovie:(NSString*)filename;
- (void)movieFinishedCallback:(NSNotification*)aNotification;
- (bool)isMovieEnd;
//-------------------------------------TestViewController.m---------------------------------------
- (void)playMovie:(NSString*)filename
{
NSURL* theURL = [NSURL fileURLWithPath:filename];
_theMovie = [[MPMoviePlayerViewController alloc] init];// initWithContentURL:theURL];
[_theMovie.moviePlayer setContentURL:theURL];
_theMovie.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[_theMovie.moviePlayer setControlStyle:MPMovieControlStyleNone];
[_theMovie.moviePlayer setFullscreen:YES];
// register notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_theMovie];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[_theMovie.moviePlayer play];
[self.view addSubview:_theMovie.view];
}
- (void)movieFinishedCallback:(NSNotification*)aNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:_theMovie];
// release movie
[_theMovie.view removeFromSuperview];
[_theMovie release];
_isMovieEnded = true;
[self startAnimation];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
- (bool)isMovieEnd
{
return _isMovieEnded;
}
//-------------------------------------TestAppDelegate.m---------------------------------------
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
[self.window addSubview:self.viewController.view];
NSString* movieFile = [[NSBundle mainBundle] pathForResource:#"logo" ofType:#"mp4"];
[viewController playMovie:movieFile];
[viewController stopAnimation];
return YES;
}
// Determines if the movie is presented in the entire screen (obscuring all other application content). Default is NO.
// Setting this property to YES before the movie player's view is visible will have no effect.
#property(nonatomic, getter=isFullscreen) BOOL fullscreen;
- (void)setFullscreen:(BOOL)fullscreen animated:(BOOL)animated;
From MPMoviePlayerController.h
so here you are the code:
- (void)playVideoFromUrl:(NSString*)url {
MPMoviePlayerViewController *moviePlayer = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]] autorelease];
[((UIWindow*)[[UIApplication sharedApplication].windows objectAtIndex:0]).rootViewController presentMoviePlayerViewControllerAnimated:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer.moviePlayer setFullscreen:YES animated:YES];
[moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
}
- (void)moviePlaybackComplete:(NSNotification *)notification {
MPMoviePlayerViewController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[((UIWindow*)[[UIApplication sharedApplication].windows objectAtIndex:0]).rootViewController dismissMoviePlayerViewControllerAnimated];
}

playing intro movie on ipad

i am trying to play an intro movie (like a splash screen). it used to work fine for sdk 3.0 but now i am doing it for iPad. and its not running movie instead it just show black screen before view appears.
here is my code
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"intro" ofType:#"m4v" inDirectory:nil];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.fullscreen=YES;
moviePlayer.view.frame = CGRectMake(0, 0,1024,768);
[moviePlayer setShouldAutoplay:YES];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setRepeatMode:MPMovieRepeatModeNone];
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
return YES;
}
and here is my moviePlaybackDidFinish
- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *theMovie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
theMovie.initialPlaybackTime = -1;
[theMovie pause];
[theMovie stop];
[theMovie release];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
i dont know what i am missing here?
thanks for any help....
UPDATE i just found out that it plays sound but no video....is this obvious?pls help
As you're using MPMoviePlayerController over MPMoviePlayerViewController I think you need to be adding the player's view to your view hierarchy before calling play, as per the docs:
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view]; //add the player' view
this might explain why you're hearing audio but not seeing anything, pretty sure you need to setup the app window first too ([window makeKeyAndVisible]; etc)