mpmovieplayercontroller Issues ipad - ipad

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.

Related

How to play video without the full screen mode?

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"];
//}

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];

ShouldAutoplay on MoviePlayerController

I am a new developer, therefore I'm still learning and know I'm doing some things wrong. I have a segmented control object in which when the user presses one of the segments, it goes to play a video. I have it setup to where they press the segment, then have to press a play button to get the video to play. I want to cut out the play button and have it play automatically. This is where I'm having trouble. I found the shouldAutoplay option but when I use it and cut out the button, it won't take me to the video at all. I'm sure I'm not using the shouldAutoplay option correctly. Was hoping for some help or at least a point in the right direction.
- (IBAction)playMovie:(id)sender;
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"mytestimony" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.shouldAutoplay = YES;
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie autorelease];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
Have you tried [theMovie play];?
UPDATE: I just noticed you set shouldAutoplay in theMovie, but what you presented is another instance of MPMoviePlayerViewController, which is moviePlayer. That's why your movie did not autoplay. You should instead have:
[self presentMoviePlayerViewControllerAnimated:theMovie];
Don't play the video, just prepare it for playing and put the shouldAutoPlay as NO, because you don't want to video player to play it automatically.
-(void) SetVideoFile:(NSString *)fileName {
videoFileName=fileName;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:fileName]];
moviePlayer.initialPlaybackTime = 0;
moviePlayer.view.frame = self.view.frame ;
[orientationHandler VideoStart];
[self.view addSubview:moviePlayer.view] ;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.shouldAutoplay = NO;
[moviePlayer prepareToPlay];
}
-(void)moviePlaybackDidFinish:(NSNotification *)notification {
// your custom code which you want to play after the player did finish playing
}

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