I have to play my video in view given CGRectMake. But always it's playing full screen only. I'm using MPMoviewPlayerViewController.
NSString *path;
path=[[NSBundle mainBundle] pathForResource:#"solar" ofType:#"mp4"];
mpviewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
mpviewController.view.frame = CGRectMake(0, 0, 200, 300);
[self.view addSubview:mpviewController.view];
[self presentMoviePlayerViewControllerAnimated:mpviewController];
you have to set MpMoiveScalling mode to aspect fit so just set this line of code and you done.
MPMoviePlayerController *obj; [obj
setScalingMode:MPMovieScalingModeFill];
It worked for me.
Remove this line,
[self presentMoviePlayerViewControllerAnimated:mpviewController];
Working Sample code,
- (IBAction)playVideoAction:(id)sender
{
float width = 200;
float height = 300;
MPMoviePlayerViewController* theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL:moviePathURL];
theMovie.view.frame = CGRectMake((self.view.frame.size.width - width)/2, (self.view.frame.size.height - height)/2, width, height);
[self.view addSubview:theMovie.view];
}
i apply this code in my application.
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
theMovie.view.frame = CGRectMake(0, 0, 200, 300);
Thanks.
Related
I have a AVPlayerViewController that I create in this way:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSURL *videoURL = [NSURL URLWithString:#"testURL"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[playerViewController.player play];
[playerViewController.view setFrame:CGRectMake(0, 0, 300, 300)];
[self.view addSubview:playerViewController.view];
[self addChildViewController:playerViewController];
int width = 100;
int height = 150;
int xPosition = self.view.frame.size.width - width;
int yPosition = 100;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
playerViewController.view.frame = CGRectMake(xPosition, yPosition, width, height);
[UIView commitAnimations];
}
My problem is that in this way I get this:
the playerViewController.view's animation is not synchronized with the
video and the playerViewController.view slowly reduces while the video
is immediately reduced.
I don't understand why.
if you need it I can link a video of what I get
I have some strange thing - after rotating of AVPlayer, I'm updating textField and slider of progress by timer, but view with controllers change coordinates after AVPlayer
mPlayer = [[AVPlayer alloc] init];
mPlayer = [mPlayer initWithURL:streamURL];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:mPlayer];
[[[self view] layer] insertSublayer:playerLayer atIndex:0];
[playerLayer setFrame:self.view.bounds];
rotating
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.ButtonsView.frame.size.height);
if([[UIDevice currentDevice]orientation]==UIDeviceOrientationLandscapeLeft||[[UIDevice currentDevice]orientation]==UIDeviceOrientationLandscapeRight)
{
self.ButtonsView.frame = CGRectMake((self.view.frame.size.width - self.ButtonsView.frame.size.width) /2, playerLayer.frame.size.height, self.view.frame.size.width, self.ButtonsView.frame.size.height);
}
}
and after change of timer, coordinates of ButtonView became wrong
- (void)updateTime:(NSTimer *)timer {
self.tfCurrent.text = [self textFormatting : self.videoCurrentTime];
}
first second coords are good. Why my View redraw? thanks!
I using MPMediaPlayerController and i need hide elements of UI permanently.
But i can see it when player start play video, 2 seconds later it hide.
I using:
setControlStyle:MPMovieControlModeHidden.
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:#"http://catholictvhd-lh.akamaihd.net/i/ctvhd_1#88148/index_3_av-p.m3u8"]];
[player.view setFrame:self.view.bounds];
player.view.backgroundColor = [UIColor blueColor];
player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
CGSize maxSize;
if([[UIScreen screens]count] > 1) {
[self logIt:#"External screen is available"];
newwindow = [[UIWindow alloc] init];
// There is a external display.
UIScreenMode *maxScreenMode;
for(int i = 0; i < [[[[UIScreen screens] objectAtIndex:1] availableModes]count]; i++)
{
UIScreenMode *current = [[[[UIScreen screens]objectAtIndex:1]availableModes]objectAtIndex:i];
if(current.size.width > maxSize.width)
{
maxSize = current.size;
maxScreenMode = current;
}
}
UIScreen *external = [[UIScreen screens] objectAtIndex:1];
external.currentMode = maxScreenMode;
[self logIt:[NSString stringWithFormat:#"%#",maxScreenMode]];
newwindow.screen = external;
// setting external display size.
CGPoint point = CGPointMake(0.0f, 0.0f);
//CGSize size = CGSizeMake(1024.0f, 768.0f);
CGSize size = maxSize;
CGRect frame = player.view.frame;
frame.origin = point;
frame.size.width = size.width;
frame.size.height = size.height;
[player.view setFrame:frame];
[player setControlStyle:MPMovieControlModeHidden];
[player setFullscreen:YES animated:YES];
player.shouldAutoplay = YES;
[player prepareToPlay];
[player play];
player.moviePlayer.controlStyle = MPMovieControlStyleNone; // to hide controls
Before playing the video you have to add subview as MPMoviePlayerController
Add this line before play
[self.view addSubview:player.view];
After load htmlString, contentSize of scrollView in UIWebView don't changed. In other my viewControllers all good, but now it's something mysterious
I have
UIWebView *contentWebView;
I make it in some method
- (void)makeContent
{
...
contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(5, imageView.originY + imageView.height + 5, mainView.width - 10, 100)];
contentWebView.delegate = self;
contentWebView.scrollView.scrollEnabled = NO;
contentWebView.contentMode = UIViewContentModeScaleAspectFit;
NSLog(#"%#", factDict[#"text"]);
[contentWebView loadHTMLString:factDict[#"text"] baseURL:nil];
contentWebView.height = contentWebView.scrollView.contentSize.height;
[mainView addSubview:contentWebView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(#"%f %f %f %f ", webView.height, webView.scrollView.contentSize.height, contentWebView.height, contentWebView.scrollView.contentSize.height);
[webView sizeToFit];
NSLog(#"%f %f %f %f ", webView.height, webView.scrollView.contentSize.height, contentWebView.height, contentWebView.scrollView.contentSize.height);
...
}
Early in other controllers contentWebView.height = contentWebView.scrollView.contentSize.height; help me, but now it's not work
In log I have next
So I don\t understand why contentsize.height not changed
EDIT
In my another viewcontrollers all good
- (void)makeScrollView
{
CGFloat width = [Utils widthOfMainViewForOrientation:self.interfaceOrientation];
[mainView removeFromSuperview];
mainView = [[UIView alloc] initWithFrame:CGRectMake((self.view.width - width) / 2, 15, width, 100)];
mainView.backgroundColor = [UIColor whiteColor];
if ([self.fullInfoDictionary[#"content"] length]) {
contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(6, 4, mainView.width - 12, 100)];
contentWebView.contentMode = UIViewContentModeScaleAspectFit;
contentWebView.delegate = self;
HelperDf.htmlString = self.fullInfoDictionary[#"content"];
[contentWebView loadHTMLString:self.fullInfoDictionary[#"content"] baseURL:nil];
contentWebView.scrollView.scrollEnabled = NO;
[mainView addSubview:contentWebView];
}
[self.scrollView addSubview:mainView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
contentWebView.height = contentWebView.scrollView.contentSize.height;
NSLog(#"%f %f %f %f", webView.height, webView.scrollView.contentSize.height, contentWebView.height, contentWebView.scrollView.contentSize.height);
[self reloadFrames];
}
i have next
I get htmlString from this controller
HelperDf.htmlString = self.fullInfoDictionary[#"content"];
And set in my first UIWebVIew but results - NO.
You can it's no matter if stay contentWebView.scrollView.scrollEnabled = NO; I change to YES, but results are same - in second case all good in first - NO
You need to change NO to YES your contentWebView.scrollView.scrollEnabled, such like
contentWebView.scrollView.scrollEnabled = YES;
EDITED
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView sizeToFit];
webView.scrollView.contentSize = CGSizeMake(320, contentWebView.scrollView.contentSize.height + 60);
}
The problem is that your view isn't visible yet. The values you are looking for are only made visible once it is added to a view which is in the view stack.
Somewhere along the way it seems that your view isn't added to at the time when you are checking your values.
I am trying to add an intro video into my iOS app. I want the video to play with no controls and in a box I create for it. but am not sure how to do that. It is also not playing at the correct size it is super small. If you look below you will see my attempt but it fails to work. How do I achieve this?
-(void)initVideo{
MPMoviePlayerViewController *moviePlayerController=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"video" ofType:#"mp4"]]];
UIView * contain = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
[contain addSubview:moviePlayerController.view];
[self.view addSubview:contain];
MPMoviePlayerController *player = [moviePlayerController moviePlayer];
player.fullscreen = NO;
[player play];
}
I actually have code in my book that shows you exactly how to do this very thing:
http://www.apeth.com/iOSBook/ch28.html#_mpmovieplayercontroller
Read down to the first block of code. As you can see, we load a movie, decide on the rect within our view where we want to display it, and display it:
NSURL* m = [[NSBundle mainBundle] URLForResource:#"ElMirage"
withExtension:#"mp4"];
MPMoviePlayerController* mp =
[[MPMoviePlayerController alloc] initWithContentURL:m];
self.mpc = mp; // retain policy
self.mpc.shouldAutoplay = NO;
[self.mpc prepareToPlay];
self.mpc.view.frame = CGRectMake(10, 10, 300, 250);
self.mpc.backgroundView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.mpc.view];
Of course you will want to change those values! But that's the technique. Also you're going to want to get rid of the controls, but that's easy (read further down in the chapter).
Since you are using the player in a view, you don't need to use a MPMoviePlayerViewController. Try the following code:
-(void)initVideo{
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"video" ofType:#"mp4"]]];
UIView *contain = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
moviePlayerController.view.frame = contain.bounds;
[contain addSubview:moviePlayerController.view];
[self.view addSubview:contain];
moviePlayerController.fullscreen = NO;
[moviePlayerController play];
}
Also, if you are using a navigation bar or status bar, you should take that away from the height:
CGRect f = [[UIScreen mainScreen] bounds];
f.size.height -= [[UIApplication sharedApplication] statusBarFrame].size.height + self.navigationController.navigationBar.frame.size.height;
UIView *contain = [[UIView alloc] initWithFrame:f];