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];
Related
I am using a UICollectionView with paging feature and adding AVPlayer on each cell to play video with URL, but my collection view is giving a strange behavior. When it loads the first cell and video starts playing then it is fine but when I slide and load the next cell then both videos are playing together, while I want the previous one to stop playing. I tried many thing to resolve this issue but nothing is working, someone please suggest me something to work with this. My code of custom cell class where I am adding AVPlayer is given below:
[self.avPlayer pause];
self.avPlayer = nil;
[self.avLPlayer removeFromSuperlayer];
CGRect videoRect;
videoRect = CGRectMake(0,0,[[UIScreen mainScreen]bounds].size.width,[[UIScreen mainScreen]bounds].size.height);
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[indicator startAnimating];
[indicator bringSubviewToFront:_viewForShowVideo];
UIWindow* currentWindow = [UIApplication sharedApplication].keyWindow;
indicator.frame = CGRectMake(currentWindow.frame.size.width/2, _viewForShowVideo.frame.size.height/2, 20, 20);
[_viewForShowVideo addSubview:indicator];
self.avPlayerItem = [AVPlayerItem playerItemWithURL:Url];
self.avPlayer = [AVPlayer playerWithPlayerItem:self.avPlayerItem];
self.avLPlayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
[_avLPlayer setFrame:videoRect];
[_avLPlayer setBackgroundColor:[UIColor blackColor].CGColor] ;
[_avLPlayer setVideoGravity:AVLayerVideoGravityResizeAspect];
_avLPlayer.opacity = 1;
_avPlayer.allowsExternalPlayback = NO;
[_avPlayer setMuted:NO];
_avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[AVAudioSession sharedInstance]setCategory: AVAudioSessionCategoryPlayback error: nil];
[_viewForShowVideo.layer addSublayer:_avLPlayer];
[_avPlayer play];
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnPlayer:)];
[_viewForShowVideo addGestureRecognizer:tgr];
[_viewForShowVideo setBackgroundColor:[UIColor greenColor]];
I want to stream video on my iOS application, the problem is that it displays a black screen with the url I want to show but it works with other url's here is my code :
[super viewDidLoad];
NSURL *fileURL = [NSURL URLWithString:#"http://livevideo.infomaniak.com/iframe.php?stream=arabelfmendirect&name=arabel_webtv&player=2828.m3u8"];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view addSubview:moviePlayerController.view];
// moviePlayerController.fullscreen = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
how can I fix it ?
The video URL mentioned doesn't use HLS format. Thus MPMoviePlayerController cannot play the video.
From http://livevideo.infomaniak.com/iframe.php?stream=arabelfmendirect&name=arabel_webtv&player=2828.m3u8, it looks like it needs flash to play the video.
I have tried playing 500 KB videos on AVPlayer and MPMoviePlayerController, and they both have a slightly annoying delay. AVPlayer and MPMoviePlayerController both display a black screen for 2-3 seconds, and then it starts the video. Thats really annoying, how come it takes so long for the video to start? How can I fix it?
//Movie Player Code
_moviePlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:video];
_moviePlayerVC.moviePlayer.view.userInteractionEnabled = YES;
_moviePlayerVC.moviePlayer.view.multipleTouchEnabled = YES;
_moviePlayerVC.moviePlayer.repeatMode = MPMovieRepeatModeOne;
_moviePlayerVC.moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayerVC.moviePlayer.scalingMode = MPMovieScalingModeFill;
_moviePlayerVC.moviePlayer.view.frame = self.frame;
[_moviePlayerVC.moviePlayer setShouldAutoplay:YES];
_moviePlayerVC.view.frame = self.frame;
[self addSubview:_moviePlayerVC.view];
//AVPlayer Code
AVAsset *asset = [AVAsset assetWithURL:video];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset];
_player = [[AVPlayer alloc] initWithPlayerItem:item];
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
layer.frame = self.frame;
layer.backgroundColor = [UIColor clearColor].CGColor;
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
You could preload an AVPlayer and observe its status. Once it changes AVPlayerStatusReadyToPlay you should be able to start playback without any delay.
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.
I am using the MPMoviePlayerController to play a movie from the web.
Depending on the table row selected a different movie is loaded. However, i would like the MPMoviePlayerController to disappear (or hide itself), once a new row is selected.
Here is the code that gets called to play my movie and to, eventually, hide it
- (IBAction) playMovie{
NSURL *url = [NSURL URLWithString:vidMovie];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
moviePlayer.view.frame = vidPlayer.frame;// CGRectMake(64, 624, 640, 360);
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
}
- (void) hidePlayer{
[moviePlayer stop];
[moviePlayer release];
}
in my .h i declare moviePlayer as such
MPMoviePlayerController *moviePlayer;
I've tried setting the moviePlayer frame height and width to 0 but that still shows the play button.
I've tried the variables .hidden and .opaque and still i get nothing
Could anyone help me figure out what i might have forgotten. Any help would be greatly appreciated!
Thanks
I found it after trying all sorts of different things...
Seems i needed to retain my moviePlyer to be able to remove it in another part of my code.
If anyone has the same problem, here is my solution!
- (IBAction) playMovie{
NSURL *url = [NSURL URLWithString:vidMovie];
moviePlayer = [[[MPMoviePlayerController alloc]initWithContentURL:url] retain];
moviePlayer.view.frame = vidPlayer.frame;// CGRectMake(64, 624, 640, 360);
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
}
- (void) hidePlayer{
[moviePlayer stop];
[moviePlayer.view removeFromSuperview];
}
Hope this might be able to help othes!