IOS5 - How To play videos within a programatically created View? - ios

I'm creating a game for iPad using OpenGL. I have created a view (with a simple background) grammatically using the following code:
- (void) addNewView {
UIWindow* window = [UIApplication sharedApplication].keyWindow;
UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 60, 0, 900, 900)];
polygonView.backgroundColor = [UIColor blueColor];
//here play a movie:-)
[window addSubview:polygonView];
[polygonView release];
}
and i'm display it using the following code within a case switch in a window:
[self addNewView];
This all works well.
However, I need help to implement a video feature in this view, when it is displayed.
Can anyone provide me with assistance in the form of code or a link to a relevant tutorial that can aid me with this?

Take a look at:
MPMoviePlayerController Class Reference
and
MPMoviePlayerViewController Class Reference
That should get you started.

Related

Re-enable mirroring on iOS

In my iOS app I need to display custom content on external display (using AirPlay) as well as mirroring some screens on TV.
For presenting custom content I use code from Multiple Display Programming Guide for iOS and it works well: while my iPad is in 'mirror' AirPlay mode I'm able to show some stuff on the TV. However, documentation says6
To re-enable mirroring after displaying unique content, simply remove the window you created from the appropriate screen object.
And this part isn't working at all. I just cannot destroy my window that I use to display content on external screen. Here's the code:
- (void) destroySecondWindow{
if (secondWindow){
for( UIView* view in secondWindow.subviews ){
[view removeFromSuperview];
}
secondWindow.backgroundColor = [UIColor clearColor];
secondWindow.hidden = YES;
// Hide and then delete the window.
[secondWindow removeFromSuperview];
secondWindow = nil;
}
}
As far as unique content should be displayed only when one particular view controller is visible, I'm trying to destroy external window like this:
- (void) viewWillDisappear:(BOOL)animated{
[self destroySecondWindow];
}
Here's how I create second window:
- (void) createSecondWindowForScreen:(UIScreen*)screen{
if( screen == nil || secondWindow != nil ){
return;
}
CGRect screenBounds = screen.bounds;
secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
secondWindow.backgroundColor = [UIColor blueColor];
secondWindow.screen = screen;
[secondWindow setHidden:NO];
}
So the question is: does anybody know how to re-enable screen mirroring after displaying unique content on TV?
Thanks in advance!

bad orientation in screen with multiple viewcontrollers/ views in iOS 7, landscape only (iOS 8 is fine)

I'm using TheSidebarController to implement add a sliding menu into an iOS application. This is the library I'm using, but I've found the same issue in other libraries, like ECSlidingViewController, etc. They essentially work by adding multiple view controllers onto a containing view controller, nothing too crazy.
The issue is, when you make the app a landscape app, all the screens in the container- the menu, the content screen- seem to think they're in portrait mode, and get cut off half way. You can see the issue in this screenshot where the table is cut off:
http://imgur.com/xD5MUei
I've been trying to get this to work in any way I can, and no luck.
The library I'm using + example project can be found here:
https://github.com/jondanao/TheSidebarController
Any help is greatly appreciated :)
EDIT: people are saying I can stretch the table out to make it look normal, but this just masks the underlying problem, which is the app and/or the screens still think they're in portrait orientation. As a quick example, if I take the example project, and in LeftViewController substitute the following code:
- (void)dismissThisViewController
{
UIViewController* vc = [[UIViewController alloc] init];
UINavigationController* pulldown = [[UINavigationController alloc] initWithRootViewController:vc];
pulldown.view.frame = CGRectMake(pulldown.view.frame.origin.x, -[[UIApplication sharedApplication] delegate].window.frame.size.height,
pulldown.view.frame.size.width, pulldown.view.frame.size.height);
[[[UIApplication sharedApplication] delegate].window addSubview:pulldown.view];
[UIView animateWithDuration:.5 animations:^{
pulldown.view.frame = CGRectMake(pulldown.view.frame.origin.x, 0,
pulldown.view.frame.size.width, pulldown.view.frame.size.height);
} completion:^(BOOL finished) {
;
}];
}
The viewcontroller comes in sideways, not from the top.
This was a strange one... I had to set the frame of the content view controller, which made sense, but then I had to reset it every time the content was refreshed:
- (void)setContentViewController:(UIViewController *)contentViewController
{
// Old View Controller
UIViewController *oldViewController = self.contentViewController;
[oldViewController willMoveToParentViewController:nil];
[oldViewController.view removeFromSuperview];
[oldViewController removeFromParentViewController];
// New View Controller
UIViewController *newViewController = contentViewController;
[self.contentContainerViewController addChildViewController:newViewController];
[self.contentContainerViewController.view addSubview:newViewController.view];
[newViewController didMoveToParentViewController:self.contentContainerViewController];
_contentViewController = newViewController;
if ([DeviceDetection isDeviceiPad]) {
_contentViewController.view.frame = CGRectMake(0, 0, 1024, 768);
}
}
Did you check if it's has something to do with the new interface orientation?
https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html
chapter -> Supporting New Screen Sizes and Scales
In CenterViewController.h make the class a subclass of a UITableViewController instead.
Then comment out [self.view addSubview:self.tableView]; in CenterViewController.m.
Done!
In centerViewController.m, when you create the tableview, add this line:
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

Control buttons in AVPlayer and frame

I have to play a series of videos in my app. I am using AVQueuePlayer to play the videos using the code:
AVQueuePlayer *queuePlayer = [[AVQueuePlayer alloc] initWithItems:items];
AVPlayerLayer *myPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:queuePlayer];
myPlayerLayer.frame = CGRectMake(0, 0, 320, 350);
[self.view.layer addSublayer:myPlayerLayer];
[self.queuePlayer play];
I am facing two issues.
When the video plays, the frame of player is not according to the given values(i.e. 0, 0, 320, 350).
No control buttons(volume,play,stop, forward etc) are being showed as they are shown when we play video with MPMediaPlayer. Am i missing something for both cases?
(I am using AVFoundation for the first time so these questions may seem stupid).
Thanks in advance.
When the video plays, the frame of player is not according to the
given values(i.e. 0, 0, 320, 350).
Apple's sample code doesn't use -playerLayerWithPlayer:. It creates a UIView subclass whose layer is an AVPlayerLayer. Perhaps there's a reason for that. I do that in my code and it works great. Take a look at the AVPlayerDemo code and try doing it that way.
No control buttons(volume,play,stop, forward etc) are being showed as
they are shown when we play video with MPMediaPlayer. Am i missing
something for both cases? (I am using AVFoundation for the first time
so these questions may seem stupid).
AVPlayer does not provide these for you. That's what MPMoviePlayerController is for. If you want controls and you want to use AVPlayer, you'll need to create and manage the controls yourself.
As an alternative to Dave's answer to your first question...
If using auto layout, you can store, as a local property, a reference to the player's layer returned by playerLayerWithPlayer: and update its frame whenever the view controller's view lays out its subviews -
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.playerLayer.frame = self.view.layer.bounds;
}
Or if you're working in a UIView subclass -
- (void)layoutSubviews
{
[super layoutSubviews];
self.playerLayer.frame = self.layer.bounds;
}
This assumes that you would like the player to fill the view; you can update the code if you have other intentions.

ios6 moviePlayer first frame not appearing upon creation

I have methods inside my open class controller that instantiates a moviePlayer, which is set to 'autoPlay = NO';
I have added the movieplayer.view as a subview of the controllers view, configured it and created a full screen button on top for starting the video. Since iOS4.3, this has been working fine. The button is transparent and the first frame of the video showed through ( which was a picture of a custom Automoble Auto-Start button).
Since iOS6, I only get a black screen.
Clicking the image-button does start the video as it should; calls [moviePlayer play]
Has something changed that I have not taken into consideration?
I have provided the two sections of code I think are necessary.
#define INTRO_MOVIE #"Intro.mov"
-(void)viewDidLoad
{
if(SHOULD_PLAY_INTRO_VIDEO)//Debug switch to ignore the intro video
{
// Prepare the movie and player
[self configureIntroMoviePlayer];
[self.view addSubview:moviePlayer.view];
[self.view bringSubviewToFront:moviePlayer.view];
// Add and Show the Start Button to start the App
[self configureStartButton];
[self.view addSubview:startButton];
}
}
-(void)configureIntroMoviePlayer
{
LOGINFO
// Prepare the Intro Video
NSString *pathToIntroVideo = [ mainFilePath_ stringByAppendingPathComponent: INTRO_MOVIE];
NSURL *URLToIntroVideo = [NSURL fileURLWithPath:pathToIntroVideo];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:URLToIntroVideo];
[moviePlayer setShouldAutoplay:NO];
moviePlayer.view.frame = CGRectMake(0, -20, 1024, 768);
[moviePlayer setControlStyle:MPMovieControlStyleNone];
//fixing video brightness Difference with iPad2
if(isIpad2)
{
moviePlayer.backgroundView.backgroundColor = [UIColor blackColor];
moviePlayer.view.alpha = .99;
}
// Create the sKip button for cancelling the Intro Video
skipIntro = [UIButton buttonWithType:UIButtonTypeCustom];
[skipIntro showsTouchWhenHighlighted];
skipIntro.frame = CGRectMake(900, 20, 111, 57);
[skipIntro addTarget:self action:#selector(skipIntroWasPressed) forControlEvents:UIControlEventTouchUpInside];
}
I am not sure why I got a -1 rating for this question for lack of research or clarity?
Maybe I do not know the proper usage of this forum.
I apologize.
I did find that adding [moviePlayer prepareToPlay] solved the problem. Like I said, it was odd that the first frame always showed up prior to iOS 6.
Have you tried:
[moviePlayer.view addSubView:startButton];

Adding image on top of a view using Xcode4.2, assistance needed

In my UIViewController file, inside viewDidLoad method, i
CGRect gameArea = CGRectMake(30, 30, 100, 100);
UIImage *backgroundImage = [UIImage imageNamed:#"cards.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:backgroundImage];
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
My expectation is that on top of existing view, a rectangle will show up with the image as his background.
Instead app crashes. What am i doing wrong?
It does not seem to matter where this code is pasted. I tried putting it as part of init - same thing happens. App crashes
The problem seems to happen here:
The stack trace looks as follows:
The problem is that my code runs as part of viewDidLoad method. By this time, things are established already.
Instead the same code works fine as part of the
- (void) viewWillAppear:(BOOL)animated
Hope this helps someone

Resources