MPMoviePlayerController in background doesn't leave page when we change scene - iphone

I am using MPMoviePlayerController to play a video in the background of a CCMenu. So I have a video playing behind some buttons. When I switch views/scenes, I want the moviePlayer to leave the scene the same as the buttons/images on that layer/scene.
The code to play the movie in the background is as follows:
-(void)playMainMenuVideo{
NSString *path=[[NSBundle mainBundle] pathForResource:#"MYMMainMenu" ofType:#"mov"];
MPMoviePlayerViewController * player=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
player.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[player view] setFrame:[[[CCDirector sharedDirector]view] bounds]];
player.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[[[CCDirector sharedDirector]view] addSubview:[player view]];
[[[CCDirector sharedDirector]view] sendSubviewToBack:player.view];
UIView* glView = [CCDirector sharedDirector].view; // attention
[glView.superview insertSubview:player.view atIndex:0]; // attention
glView.opaque = NO; // attention
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // attention
}
This works perfect, In the app delegate I also changed
CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds]
pixelFormat:kEAGLColorFormatRGB565 // To kEAGLColorFormatRGBA8
Lastly I want to do a page turn like so:
[[CCDirector sharedDirector] replaceScene: [CCTransitionPageTurn transitionWithDuration:1.0 scene:[FirstPageViewController scene]]];
But only the sprites will leave the scene, the video stays playing in the background. How can I fix this? Thanks in advance

I figured it out.
1) Setup notification for when video is done playing.
2) When its done playing, place an image of the last frame of the video, over the video.
3) Then set these properties back to original
UIView* glView = [CCDirector sharedDirector].view; // attention
glView.opaque = YES; // attention
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
4)Remove MPMoviePlayerController
Happy Coding everyone :)

Related

Landscape in AvCam iOS 6

I am new to iOS, trying to create custom camera using AvCam. I am having trouble getting landscape orientation preview -- it rotates the view 90 degree clockwise and shows it on a half screen.
I get this message --
WARNING: -[ setOrientation:] is deprecated.
Please use AVCaptureConnection's -setVideoOrientation:
AVCaptureConnection already sets orientation, so I have no clue what am I supposed else.
I know this question was asked lots of times for previous versions of iOS (4,5), but non of those techniques/codes worked for me (iOS 6).
Original code (did not make any changes from Apple)
if ([self captureManager] == nil) {
AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init];
[self setCaptureManager:manager];
[manager release];
[[self captureManager] setDelegate:self];
if ([[self captureManager] setupSession]) {
// Create video preview layer and add it to the UI
AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]];
UIView *view = [self videoPreviewView];
CALayer *viewLayer = [view layer];
[viewLayer setMasksToBounds:YES];
CGRect bounds = [view bounds];
[newCaptureVideoPreviewLayer setFrame:bounds];
if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
[newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
}
[newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
[self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer];
AVCaptureConnection chunk:
-(void)startRecordingWithOrientation:(AVCaptureVideoOrientation)videoOrientation; {
AVCaptureConnection *videoConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self movieFileOutput] connections]];
if ([videoConnection isVideoOrientationSupported])
[videoConnection setVideoOrientation:videoOrientation];
[[self movieFileOutput] startRecordingToOutputFileURL:[self outputFileURL] recordingDelegate:self];
}
Last time i also stumbled upon this issue. I solved this problem by doing two things
Getting the right orientation
Replace
if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
[newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
}
With
if ([newCaptureVideoPreviewLayer.connection isVideoOrientationSupported]) {
[newCaptureVideoPreviewLayer.connection setVideoOrientation:[UIDevice currentDevice].orientation];
}
Force the update of the video orientation during the initialization to catch video output in landscape mode by triggering
- (void)deviceOrientationDidChange manually within AVCaptureManager.m
I added it to:
- (BOOL) setupSession
{
BOOL success = NO;
...
AVCamRecorder *newRecorder = [[AVCamRecorder alloc] initWithSession:[self session] outputFileURL:self.lastOutputfileURL];
[newRecorder setDelegate:self];
[self performSelector:#selector(deviceOrientationDidChange)];
...
return success;
}
So, here's a workaround, but I am sure that there must be better solution than this. I got part of the code from this question :
iPhone App - Show AVFoundation video on landscape mode
But had to adjust frame for every orientation in order to make it work on iOS6 (and it still shows a warning):
-(void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
[CATransaction begin];
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft){
captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeLeft;
captureVideoPreviewLayer.frame = CGRectMake(0, 0, 480, 320);
} else if (toInterfaceOrientation==UIInterfaceOrientationPortrait){
captureVideoPreviewLayer.orientation = UIInterfaceOrientationPortrait;
captureVideoPreviewLayer.frame = CGRectMake(0, 0, 320, 480);
} else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight){
captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeRight;
captureVideoPreviewLayer.frame = CGRectMake(0, 0, 480, 320);
}
[CATransaction commit];
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

Add Mobclix to Cocos2d Scene

Has anyone integrating mobclix into a cocos2d application? I tried to add a banner of Ad by using Mobclix, but it did not appear, my code:
//In appDelegate:
[Mobclix startWithApplicationId:#"4778FFA3-DDFF-4550-BFFE-7B7AB4B5765D"];
[Mobclix start];
//In my Scene
-(void)onEnter {
adView = [MobclixAdViewiPhone_320x50 new];
adView.delegate = self; //Optional
// set position
adView.frame = CGRectMake(10, 20, 320, 50);
dView.refreshTime = 15;
// add to view
[[[CCDirector sharedDirector] openGLView] addSubview:adView];
// set refresh (automatically gets one ad when changing the property)
[adView getAd];
[super onEnter];
Anyone help me to show Adbanner on my Scene please?

iOS 4.3 Inline MPMoviePlayerController

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.

Mirror iPhone on external display with cocos2d

I want to reproduce my iPhone display for demoing purposes. I've tried
this and this, but both give me a black screen. Finally, I headed out to make my own solution. Here's what I have:
// Check for external screen.
if ([[UIScreen screens] count] > 1) {
externalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Internal display is 0, external is 1.
externalScreen = [[[UIScreen screens] objectAtIndex:1] retain];
screenModes = [externalScreen.availableModes retain];
UIScreenMode *desiredMode = [screenModes objectAtIndex:0];
externalScreen.currentMode = desiredMode;
externalWindow.screen = externalScreen;
[screenModes release];
[externalScreen release];
CGRect rect = CGRectZero;
rect.size = desiredMode.size;
externalWindow.frame = rect;
externalWindow.clipsToBounds = YES;
externalWindow.hidden = NO;
[externalWindow makeKeyAndVisible];
[externalWindow setUserInteractionEnabled:YES];
[externalWindow setMultipleTouchEnabled:YES];
//[[CCDirector sharedDirector] attachInView:externalWindow];
Now, I can display on the external display or on my iPhone, but I can't display on both at the same time, because [[CCDirector sharedDirector] attachInView:externalWindow]; will only take one UIWindow. How can I get around this and/or get the displayed image and set it to my external display?
Thanks,
Dave
The only solution i see is to render your scene to a texture and render that texture twice as a full screen quad to the backbuffer, once for each UIWindow

iPhone 4.0:MPMoviePlayerController doesn't play the video properly

I am using MPMoviePlayerController class for playing the video in built-in media player on iPhone. The project works fine on iPhone 3.0 simulator or device, but it doesn't show the video view on iPhone 4.0 simulator or device, rather than i could able to hear the sound, no built-in player video display.
Does anyone come across this issue and how to resolve it?
UPDATED:
I placed my code below: Now the problem is, i call the code playing the video when clicking on a row in the TableView. When i click on a row, it started playing the video but the TableView is not being completely hidden. So i could see Tableview and Video, both screens have overwritten. How do i push back the TableView completely when video is playing and come back to the TableView after video view is done?
NSURL *url = [NSURL URLWithString:strUrl];
mP = [ [MPMoviePlayerController alloc] initWithContentURL:url ];
[mP setControlStyle:MPMovieControlStyleFullscreen];
[mP setFullscreen:YES];
[mP play];
[[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)];
[[mP view] setFrame:CGRectMake(0, 0, 480, 320)];
// Add movie player as subview
[[self view] addSubview:[mP view]];
You have to remember that in iOS4 MPMoviePlayerController is not playing in fullscreen by default. You have to add your moviePlayer.view on the superview.
If you want to play the video on fullscreen you should look at the MPMoviePlayerViewController reference