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/
Related
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
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;
}
}
Having trouble with MPMoviePlayerController. When I attempt to load the video I just get the activity indicator ir the navbar and it says loading. You see the controllers at the bottom, so the controller loads just not the video. I have copied the link into a browser and the video plays fine. I am using the same code from the app that is already up in iTunes, I am just revamping it. So when I click on the videos in the app that is in iTunes the videos work fine. Just not sure what is going on now here is my code:
-(void)loadVideo {
NSURL *url = [NSURL URLWithString:self.videoUrl];
NSLog(#"url %#", self.videoUrl);
self.movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.movie];
self.movie.controlStyle = MPMovieControlStyleDefault;
self.movie.shouldAutoplay = YES;
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
Any ideas why this is happening?
Solved the issue. When I used the property self.videoUrl it was a string that had the #"" still attached. I needed to strip that from the string so only the url was left.
I am using this code with the MediaPlayer framework to play a video:
-(void)GrommeVideoExcerpt1
{
NSURL *url1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"GrommeVideoExcerpt1" ofType:#"mp4"]];
grommePlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:url1];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:grommePlayer1];
grommePlayer1.controlStyle = MPMovieControlStyleDefault;
grommePlayer1.shouldAutoplay = YES;
[self.view addSubview:grommePlayer1.view];
[grommePlayer1 setFullscreen:YES animated:YES];
}
But when I play one video, then navigate to another video and try to play it, the app crashes. It gives me this error:
http://pastebin.com/nUGLXEAi
The problem here is you are adding a notification for when the video finishes playing, and that notification triggers a method you do not have (moviePlayBackDidFinish:) causing the crash.
You could implement this method or remove the notification, depends what you want to do after the video is done playing i.e. remove a viewController that plays the video, etc.
//Register notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
//Then the method ...
-(void) moviePlaybackDidFinish:(NSNotification *)aNotification{
[grommePlayer1.view removeFromSuperview];
grommePlayer1 = nil;
}
Hi i have created app for ipad , it has an video files in resource folder and it plays when the user click play button.
It works for first time user clicks play button but when user plays subsequent times of video file.The issues occurs that is the video is not play only audio is playing.These issues occurs not randomly but some more times it will occurs.
One more thing at the time of issue occurs (Video is not playing ) , when i click the wo-arrow icons located at the bottom right corner of the player the movie goes to full screen and it shows video .At the time video is playing.
Can any one help me ?
Here is my sample code
MPMoviePlayerController *moviePlayer;
}
#property (readwrite, retain) MPMoviePlayerController *moviePlayer;
#synthesize moviePlayer;
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
[super viewDidLoad];
}
-(IBAction)PlayBtnPressed:(id)sender
{
NSURL *movieURL;
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"title" ofType:#"mp4"];
movieURL = [NSURL fileURLWithPath:moviePath];
// Initialize a movie player object with the specified URL
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (mp)
{
// save the movie player object
self.moviePlayer = mp;
[mp release];
UIInterfaceOrientation orientation = [[UIDevice currentDevice]orientation];
[self shouldAutorotateToInterfaceOrientation:orientation];
// Play the movie!
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer play];
}
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
NSLog(#"movieFinishedCallback aNotification");
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[player.view removeFromSuperview];
}
Thanks in advance ........
Looks like the movieplayer is not getting released until after you've allocated a second one on the same movie file:
self.moviePlayer = mp;
retains it, but the movie finished part doesn't do a
self.moviePlayer = nil;
Could be that's causing you problems.