This is the crash I am gettin gwhile playing a video on 3.1.3. It works fine on 4.0.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIWindow addEventMonitor:]: unrecognized selector sent to instance 0x127500'
any answers would be ver helpful. Thanks,
playing videos is handled differently between os 3.0+ and os 4.0+, also for what reason are you using addEventMonitor?
Here's how I do it:
-(void)playStoryMovie {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"campaign1" ofType:#"mp4"]];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
// Use the new 3.2 style API
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.shouldAutoplay = YES;
// This does blows up in cocos2d, so we'll resize manually
// [moviePlayer setFullscreen:YES animated:YES];
[moviePlayer.view setTransform:CGAffineTransformMakeRotation((float)M_PI_2)];
CGSize winSize = [[CCDirector sharedDirector] winSize];
moviePlayer.view.frame = CGRectMake(0, 0, winSize.height, winSize.width); // width and height are swapped after rotation
[[[CCDirector sharedDirector] openGLView] addSubview:moviePlayer.view];
} else {
// Use the old 2.0 style API, YES APPLE WE KNOW ITS DEPRECATED BUT THIS IS FOR THE OLD DEVICES.
moviePlayer.movieControlMode = MPMovieControlModeHidden;
[moviePlayer play];
}
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// If the moviePlayer.view was added to the openGL view, it needs to be removed
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
[moviePlayer.view removeFromSuperview];
}
[moviePlayer release];
}
Related
I was looking for answer in other questions but found out it's impossible to play video in iPhone in a view and not fullscreen, but maybe it has changed with the new versions? Somebody knows anything?
Use MPMoviePlayerController for playing the video and set the frame where you want to display it in the layout.
// Create custom movie player
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:URL];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(onMSAASDone:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[moviePlayer setFullscreen:FALSE];
//---play partial screen---
moviePlayer.view.frame = CGRectMake(0, 0, 200, 300);
[self.view addSubview:moviePlayer.view];
The following is the code I used to play a movie:
playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]];
float screenWidth = self.view.frame.size.width;
float screenHeight = self.view.frame.size.height;
[playerViewController.view setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[self.view addSubview:playerViewController.view];
[self.view setUserInteractionEnabled:YES];
//---play movie---
player = [playerViewController moviePlayer];
[player setControlStyle:MPMovieControlStyleNone];
[player setFullscreen:TRUE];
[player play];
skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
[skipButton setTitle:#"Skip" forState:UIControlStateNormal];
skipButton.frame = CGRectMake(0, 0, 150, 50);
[skipButton setCenter:CGPointMake(screenWidth - 30 - skipButton.frame.size.width, 100)];
[skipButton addTarget:self action:#selector(skipMovie) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:skipButton];
But, I have some issue with these on iPad1. If I did not use the setFullScreen method, the movie didn't play in FullScreen mode, even if I set the Rect (1024x768). But if I set these, the movie does play in FullScreen, but my #skipButton is not visible.
If i use:
[self.view addSubview:playerViewController.view];
after
[player play];
the first issue happened. The code works properly in iPad2, even without setFullScreen.
Does anyone have any ideas?
[playerViewController.view bringSubviewToFront: skipButton]
doesn't make any change!!!!
I think you can solve this issue by using this method
[playerViewController.view bringSubviewToFront: skipButton];
Please call this function only after your movie player loads the movie.
I am currently working on an app in which I need to play a video. Everything is working fine. But if my application is sent to the background and then again to the foreground it shows a solid black screen.
Please provide me with a solution to this problem.
[player.view setFrame:CGRectMake(0, 0, 480, 278)];
[player.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
outputURL = [NSURL fileURLWithPath:filePath];
[player setContentURL:outputURL];
[player setShouldAutoplay:NO];
player.repeatMode = MPMovieRepeatModeNone;
player.controlStyle = MPMovieControlStyleNone ;
player.scalingMode = MPMovieScalingModeAspectFit;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player prepareToPlay];
[player pause];
[self.view addSubview: player.view];
Just see this solution
For MPMoviePlayer
I am trying to play a video from a server using MPMoviePlayerController and NSURL. The video plays perfectly but it takes lot of time in loading. Following is my code:
- (void) readyPlayer {
mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if ([mp respondsToSelector:#selector(loadState)]) {
// Set movie player layout
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setFullscreen:YES];
// May help to reduce latency
[mp prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
else{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
[lblActivity removeFromSuperview];
[activity stopAnimating];
// Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown)
{
// Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
// Rotate the view for landscape playback
[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
// Set frame of movieplayer
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
// Add movie player as subview
[[self view] addSubview:[mp view]];
// Play the movie
[mp play];
}
}
Can some one help me figuring out why it is taking so much time in loading?
Thanks
Pankaj
I'm assuming its obviously not a problem with your internet connection on the phone. Did you try loading the same video on your phone, from your app and another native app (say YouTube or Safari) to see if there is a difference?
Looking at a similar code we've done, I'm wondering about the need of explicitly calling the [mp prepareToPlay]; method. Its automatically called when play is called, so we haven't explicitly done so. Could you try it out without this method to see if there is a difference?
Hope this helps!
I am using MPMoviePlayerController in my UIView and the goal is to put it there on View as inline view. The problem is that the code doesn't work except the full screen.
-(IBAction)startVideo {
//start video here
NSURL *path = [[NSURL alloc] initWithString:[self localVideoPath:NO]];
// Create custom movie player
MPMoviePlayerController *moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:path] autorelease];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setFullscreen:FALSE];
// May help to reduce latency
[moviePlayer prepareToPlay];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(onMSAASDone:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
//---play partial screen---
//moviePlayer.view.frame = CGRectMake(0, 0, 200, 300);
moviePlayer.view.frame = image.frame;
//[[moviePlayer view] setFrame: [image bounds]];
[image removeFromSuperview];
[self.view addSubview:moviePlayer.view];
// Show the movie player as modal
//[self presentModalViewController:moviePlayer animated:YES];
// Prep and play the movie
[moviePlayer play];
}
The sample code from Apple is flawed or let's say outdated. You need to add the moviePlayer's view as subview to your view. Something like this:
MPMoviePlayerController *moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:path] autorelease];
...
// Adjust positioning where I used the bound of the outer view (of type UIView)
moviePlayer.view.frame = outerView.bounds;
// Now add the movie player to the outer view
[outerView addSubView:moviePlayer.view];
...
This should do the trick.
Sorry, I did not see that you added the subview already.
Okay, for a sample code you can take the XCode sample project called MoviePlayer_iPhone (inside the XCode documentation for MPMoviePlayerController you'll find a link for the MoviePlayer sample project) and just adjust AppDelegate's initAndPlayMovie this way:
-(void)initAndPlayMovie:(NSURL *)movieURL
{
// Initialize a movie player object with the specified URL
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (mp)
{
// save the movie player object
self.moviePlayer = mp;
[mp release];
// Apply the user specified settings to the movie player object
[self setMoviePlayerUserSettings];
self.moviePlayer.view.frame = self.window.bounds;
[self.window addSubview:self.moviePlayer.view];
// Play the movie!
[self.moviePlayer play];
}
}
This one is crude because it does not set the frame or centers the view but it should display the movie when you go to local and click on Play Movie.
The only drawback I saw was that the fullscreen is not going black. That said the sample project is pretty weird and not very well written. That said it displays the non-fullscreen video.