MPMoviePlayerController into a view - iphone

I need to play a video into a cocos2d project.. and my question is: how can I put MPMoviePlayerController into my view like this:?
UIView *theView = [[CCDirector sharedDirector] openGLView];
thanks very much!!

You can't put controllers into views. However, you can put the views that they control into a view. Try this:
// instantiate and configure your MPMoviePlayerController here, c
// call it movieController
[theView addSubview:movieController.view];
disclaimer: I'm at the office so I have no way of knowing if this will work.

Related

MPMoviePlayerViewController, remove quicktime symbol/add background image?

I have an MPMoviePlayerViewController that plays audio. I would like to remove the Quicktime logo and/or add a custom background image to the player but keep the playback controls. I swear I have done this before prior to iOS 5 but I can't re-figure it out! Things I have tried:
-Adding a subview to MPMoviePlayerViewController. Puts the subview OVER the playback controls, not good.
-setBackgroundColor of the moviePlayer with a patternimage. Doesn't do anything.
-setView on the entire MPMoviePlayerViewController. As expected, the playback controls and navigation bar are gone. I suppose this could work but I'd need to recreate all the playback controls manually, but I'd really rather not.
Can anyone shed some light?
Check out the MPMoviePlayerController property backgroundView.
From the MPMoviePlayerController Class Reference;
backgroundView
A customizable view that is displayed behind the movie
content. (read-only)
#property (nonatomic, readonly) UIView *backgroundView
Discussion This
view provides the backing content, on top of which the movie content
is displayed. You can add subviews to the background view if you want
to display custom background content.
This view is part of the view hierarchy returned by the view property.
Availability Available in iOS 3.2 and later. Declared In
MPMoviePlayerController.h
Try something like this (assuming that your instance of the MPMoviePlayerController is called moviePlayerController):
patternView = [[UIView alloc] initWithFrame:self.view.bounds];
patternView.backgroundColor = [UIColor redColor];
[moviePlayerController.backgroundView addSubview:patternView];
[patternView release];
Till's answer is correct for iPhoneOS 3. This has changed in iOS 4 and 5.
Apple has now added a readonly property to MPMoviePlayerViewController exposing a MPMovieController. The MPMovieController has a backgroundView that should be used.
Now you will want to do something like this:
patternView = [[UIView alloc] initWithFrame:self.view.bounds];
patternView.backgroundColor = [UIColor redColor];
[moviePlayerController.moviePlayer.backgroundView addSubview:patternView];
[patternView release];

How can i create another view? Cocos2d

i want to create a new view in Cocos2d programmatically
can you help me on how can i do this? thanks
You can add uiviews to cocos2d with this line
[[[[CCDirector sharedDirector] glView] window] addSubview: yourUIView];

How to add UITextView in CCLayer or CCScene in cocos2d-iphone

I want to add an instruction page where instruction written in UITextView. How to do it in Cocos2d. Because this is a game page and I want to take menu transaction effects of cocos2d like CCTransitionRadialCCW.
I wrote this code
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10,60, 300,360)];
textView.backgroundColor = [UIColor clearColor];
textView.text = #"I am First enemy";
[textView setEditable:NO];
[[[CCDirector sharedDirector]openGLView]addSubview:textView];
But there is problem that it is added to the main CCDirector page, while I am writing this code
// [[[CCDirector sharedDirector]openGLView]addSubview:textView];
// replaced by :-
[self addChild:textView];
It gives me error. Please tell me how to add UITextView in CCScene or CCLayer.
I know that [self addChild:(CCNode *)node];
addChild: method needs CCNode so please tell me that how can typecast or convert UITextView object to CCNode.
If there is some alternate option for that please tell me .Like CCLabelTTF is alternate for UILabel, is there alternate for UITableView in Cocos2d-iphone.
Thanks in advance
Have a look at CCTableView: http://www.cocos2d-iphone.org/archives/943.
Combining UIKit and Cocos2D in such a way that you have the interleaving you have mentioned is likely to be very difficult, because everything done in Cocos2D is drawn in one OpenGL view. When you add a UIView, you can only add it in front of or behind Cocos2D's view.
No matter which way you're adding the UIView to cocos2d, it will not be animated by the cocos2d transition effects because simply speaking the UIView objects exist outside the OpenGL view that cocos2d uses (and animates in the case of transitions).
Adding UIViews to a cocos2d view as in your first example code is correct. But in order to position the view like a node, you should consider using the CCUIViewWrapper.

Adding a scene above Uiview on CCDirector

I want to add a UiView on the CCDirector and a scene on that UIView.
Actually i have some custom Cocoa touch control and that i want to use in my game and above that i need a scene.
Currently i am doing like this.
[[[CCDirector sharedDirector] openGLView] addSubview:gridViewController.view];
and after that line i do
[[CCDirector sharedDirector] runWithScene:[MYScene scene]];
[MYScene scene] returns a CCScene instance.
But what happens that on my custom cocoa touch control is visible but my scene is not visible at all.
Can any one guide me for that how i can accomplish my task.
You should try the opposite approach and have the UIView handled as a CCNode, then add the node to the scene.
There is even a class that makes this easily possible: CCUIViewWrapper.
If you use it, you can do the following:
CCUIViewWrapper* wrapper = [CCUIViewWrapper wrapperForUIView:view];
wrapper.contentSize = view.frame.size;
[self addChild:wrapper z:1 tag:LayerTagUILayer];
and you will get both the UIView and the scene added to CCDirector.glView. The UIView can be any, you can even define it in IB.

UIView addsubview after orientation change: Tell view to resize

I have a UIView as a XIB in Portrait mode.
This view is added programmatically to the viewcontroller like this:
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"InputView" owner:self options:nil];
InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
[self.view addSubview:inputView];
This view has autoresizing masks set up properly and rotates fine when the orientation changes from Portrait to landscape.
However, if the orientation is already landscape and I create the view after the orientation change, it has its initial portrait orientation.
Is there a way to tell the view to initialize or resize itself to portrait by using its masks?
Thanks in advance for any reply!
EDIT:
Using the suggestions of occulus and Inder Kumar Rathore (thanks guys!), I altered the code to this:
InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
[self.view addSubview:inputView];
[self.view setNeedsLayout];
[self.view layoutSubviews];
[self.view layoutIfNeeded];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[self shouldAutorotateToInterfaceOrientation:orientation];
Unfortunately, there is no change at all.
I think I found someone asking the same question:
When adding a sub view the view does not resize if the app is in landscape mode
The answer identifies the problem correctly, but is not very encouraging...
Sure, I could create two nibs or resize the frame, but this seems so contrary to the idea of auto-resizing.
I find it hard to believe that there is no way to tell a nib after awakening and adding it to a view to use its autoresize features...something that works flawless when the device rotates.
EDIT 2:
The solution of idz works:
InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
[self.view addSubview:inputView];
inputView.frame = self.view.bounds;
[inputView show];
Thanks!
Often a NIB/XIB file contains a UIViewController that takes care of all of this. In this case, since their is no view controller (in the NIB/XIB) you need to take over its post-load duties.
Calling layoutSubviews directly, or indirectly via setNeedsLayout or layoutIfNeeded won't do you much good because the default implementation does nothing.
Assuming you want input view to fill the bounds of self.view you do the following:
InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
[self.view addSubview:inputView];
inputView.frame = self.view.bounds;
[inputView show];
All the resize masks of the sub-views must be correctly set for this to work and, obviously, if you don't want to fill the full bounds you may want to adjust the frame.
[self.view addSubview:viewSpinner];
viewSpinner.frame = self.view.frame;
[viewSpinner setNeedsLayout];
This works for me (Y)
I don't know if you still have this issue.
Let's say you have the following architecture:
window
subviewcontroller
(you implemented shouldautorotate correct to answering the wanted orientations)
Into this subviewcontroller you want to add the views of new UIViewControllers by just calling the addSubview function.
Instead of implementing the bounds manipulation in shouldautorotate, you should implement it in
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
self.newUIViewController.view.frame = self.view.bounds;
}
didRotateFromInterface... is called after shouldRotate. In this function the bounds are already setup correctly.
This way you don't need so much manipulation by code.
See this related question:
When do autoresizing masks take effect in iOS?
So after loading your new view from the nib, and adding as a subview to self.view, try calling setNeedsLayout.