How to play video without the full screen mode? - ios

Is there a way to add video to a view like you would with an image and an image view? I don't want the full screen mode like in the YouTube app.

You can use AVPlayer from AVFoundation Framework
AVPlayer Demo

Try this code.
1.Import media player framework- #import
2.Declare Instance variable MPMoviePlayerController *player;
-(void)viewDidLoad
{
//you can try other api to get movie file path in ios 8
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Trailer"
ofType:#"m4v"];
player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//—set the size of the movie view and then add it to the View window—
player.view.frame = CGRectMake(10, 10, 300, 300);
[self.view addSubview:player.view];
//—play movie—
[player play];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//—called when the movie is done playing—
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *moviePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
}
//-(NSString *)path
//{
// NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, //NSUserDomainMask, YES);
// NSString *documentDir=[paths objectAtIndex:0];
// return [documentDir stringByAppendingPathComponent:#"Trailer.m4v"];
//}

Related

MPMoviePlayerViewController Dismiss Without playing

The problem video can be seen in :
http://app.bowerchat.com/images/118_1435046739658.mp4
MPMoviePlayerViewController Dismissing immediately without play.
What can be reason?Some videos are loaded nicely and playing.
How can i check the video can be played or not?
I tried
NSLog(#"%lu",(unsigned long)self.mpController.moviePlayer.loadState);
NSLog(#"%d",self.mpController.moviePlayer == nil);
Results always 0 - 0
MY Code to play video
self.mpController = [[MPMoviePlayerViewController alloc] initWithContentURL:moveUrl];
[self.mpController.moviePlayer prepareToPlay];
NSLog(#"%lu",(unsigned long)self.mpController.moviePlayer.loadState);
NSLog(#"%d",self.mpController.moviePlayer == nil);
[parent presentMoviePlayerViewControllerAnimated:self.mpController];
The reason could be that movie that has extention *.mp4 is actually of different type.
Also try to add the following:
self.mpController.moviePlayer.shouldAutoplay = YES;
[self.mpController.moviePlayer play];
Try this snippet :
-(void)setupMovie
{
NSURL *movieURL = [NSURL fileURLWithPath:movieURL];
self.mpController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self. mpController.scalingMode = MPMovieScalingModeAspectFill;
self. mpController.controlStyle = MPMovieControlStyleNone;
self.mpController.view.frame = self.view.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateDidChange:) name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification object:nil];
[self.view addSubview:self.mpController.view];
[self.mpController prepareToPlay];
}
- (void)moviePlayBackDidFinish:(MPMoviePlayerController *)player
{
[self.mpController.view removeFromSuperview];
}
- (void)moviePlayerPlaybackStateDidChange:(NSNotification*)notification
{
if (self. mpController.isPreparedToPlay) {
[self. mpController play];
}
}
And make sure movieURL is correct and video exist by this provided url

How to : Local MP4 Video as Background in iOS App Objective C iOS 8

I try to use a simple 13 sec mp4 video as background loop for my login screen.
I want the Video to autoplay and loop. It has no Audio and I don´t need controls.
I need buttons and other objects in front of it.
I tried to use a WebView and make the MP4 a GIF file from this Tutorial : https://medium.com/swift-programming/ios-make-an-awesome-video-background-view-objective-c-swift-318e1d71d0a2
But the problem is that my 5 MB MP4 has (converted to a GIF) a size of 95 MB.
I can´t use this method.
Is there any "Easy to Use" way ?
EDIT :
Okay this is what I did now.
I imported AVFoundation.
This is the Code in the View
- (void)viewDidLoad {
[super viewDidLoad];
NSURL* mURL = [[NSBundle mainBundle] URLForResource:#"App-BG-Loop" withExtension:#"mp4"];
AVPlayer* player = [AVPlayer playerWithURL:mURL];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[player currentItem]];
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = _videoView.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
playerLayer.needsDisplayOnBoundsChange = YES;
[_videoView.layer addSublayer:playerLayer];
_videoView.layer.needsDisplayOnBoundsChange = YES;
[player play];
// Do any additional setup after loading the view, typically from a nib.}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];}
This works great in the iOS Simulator but when I try to start it on a device it wont start and or play the video. The View just stays white. No Error nothing.
Any Idea?
EDIT 2 :
Problem with not playing on device was the size of the video it was to big. Apple only supports 1080p on the devices.
I recently had to use an animation too. I tried it with a UIImageView first but the memory management was not too good with that. I ended up making a .mp4 from it and use it in a MPMoviePlayerController:
-(void)setupDashboardAnimation
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(checkMovieStatus:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"some_movie" ofType:#"some_extention"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
_backgroundAnimationMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
_backgroundAnimationMoviePlayer.controlStyle = MPMovieControlStyleNone;
_backgroundAnimationMoviePlayer.repeatMode = MPMovieRepeatModeOne;
_backgroundAnimationMoviePlayer.view.backgroundColor = [UIColor clearColor];
for (UIView *aSubView in _backgroundAnimationMoviePlayer.view.subviews)
{
aSubView.backgroundColor = [UIColor clearColor];
}
[_backgroundAnimationMoviePlayer.view setFrame:imv_background.frame];
[_backgroundAnimationMoviePlayer prepareToPlay];
}
-(void)checkMovieStatus:(NSNotification *)notification
{
if (_backgroundAnimationMoviePlayer.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK) && _backgroundAnimationMoviePlayer.playbackState != MPMoviePlaybackStatePlaying)
{
[self.view insertSubview:_backgroundAnimationMoviePlayer.view aboveSubview:imv_background];
[_backgroundAnimationMoviePlayer play];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
dashboardAnimationDidSetup = YES;
}
}

Call MPMoviePlayerViewController in app delegate

I am using story board in app.
Using storyboard all views are connected properly
Now new thing I want to do is,
When my Splash screen goes down, I want to show 3 sec video every time user opens the app.
I know how to load video from viewcontroller,
Following is code that I used to launch the video.
- (void)showVideo
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *path = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
NSLog(#"video path :- %#",url);
videoController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
videoController.moviePlayer.controlStyle = MPMovieControlStyleNone;
[self presentMoviePlayerViewControllerAnimated:videoController];
[videoController.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoController];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[videoController.view removeFromSuperview];
[videoController.moviePlayer stop];
videoController = nil;
[self.view removeFromSuperview];
}
But when I use this code in my rootViewController app crashes saying
Attempt to present <MPMoviePlayerViewController:> on <DashbaordVC:> whose view is not in the window hierarchy
But when I use same code in other demo app using navigation controller (No Storyboard) it works fine.
But in this app where story board is used, it crashes.
Also I tried
[self.navigationController pushViewController:self.videoController animated:NO];
Then I thought of adding this code in AppDelegate file and calling the method from ApplicationDidFinishLaunching
But didn't help.
can anyone guide me.... for the same
Also how to add MPMoviePlayerViewController in app delegate.
I think you can do this by MPMoviePlayerController. Try following code
NSString *path = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
videoPlayer = [[MPMoviePlayerController alloc] init];
[videoPlayer.view setFrame:CGRectMake(0.0, viewTopbar.frame.size.height,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - (viewTopbar.frame.size.height + 50.0))];
[videoPlayer setMovieSourceType:MPMovieSourceTypeFile];
[videoPlayer setContentURL:url];
[videoPlayer setControlStyle:MPMovieControlStyleEmbedded];
[videoPlayer setScalingMode:MPMovieScalingModeNone];
[videoPlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoPlayer];
[self.view addSubview:videoPlayer.view];
[videoPlayer play];
And if you dont want full screen then used it like this
[videoPlayer setControlStyle:MPMovieControlStyleNone];

MPMoviePlayerController.view isn't removed from superview

here is my code
- (void)viewDidLoad
{
...
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"movie" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
_moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayerController.controlStyle=MPMovieControlStyleDefault;
[_moviePlayerController play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieIsOver:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void)movieIsOver:(NSNotification *)notification
{
NSLog(#"movie is over");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.moviePlayerController.view removeFromSuperview];//moviePlayerController is MPMoviePlayerController
}
When the movie plays to the end, I can't see "movie is over" log and the moviePlayerController.view isn't removed. I don't know why.
EDIT:
MPMoviePlayerPlaybackDidFinishNotification works well.I see the "movie is over" log.The problem is moviePlayerController.view isn't removed.
I found out the solution:add
_moviePlayerController.fullscreen = NO;
before removing view from superview
Instead of adding _moviePlayerController.view as subview
[self.view addSubview:_moviePlayerController.view];
you can present _moviePlayerController just like this :
[self presentMoviePlayerViewControllerAnimated: _moviePlayerController];
and in movieIsOver method you simply dismiss
[self dismissMoviePlayerViewControllerAnimated];
I hope its help

How to Open quicktime player on iPad

I am programming an iPad app. In this app I want to open a movie URL in default Quicktime player. When I tried to open the URL in the browser the movie starts playing in the browser. How can I open the movie in the default player (so i can get play pause controls..)
Any help would be much appreciated.
Thanks
Saurabh
Finally I think it is not possible to open the quicktime player on iPad. So I searched on google and found MPMoviePlayerViewController class, which solves my problem quite well.
- (void)viewDidLoad {
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
MPMoviePlayerViewController *playerViewController =
[[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
[self.view addSubview:playerViewController.view];
//---play movie---
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
[super viewDidLoad]; }
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperView];
[player autorelease]; }
I have posted a complete tutorial my blog http://www.makebetterthings.com/blogs/iphone/play-video-on-iphone-and-ipad/

Resources