I'm adding a MPMoviePlayerController as background view and while it plays I get spams of the log-message 'Received memory warning'. I don't now why, and maybe there is a workaround or a better solution.
Here is my code:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[self navigationController]setNavigationBarHidden:YES animated:YES];
[moviePlayer play];
}
- (void) viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[moviePlayer pause];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//self.view.backgroundColor = [UIColor appStyleLightOrangeColor];
//Add Video playback
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"happy-female-friends-smartphon" ofType:#"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.shouldAutoplay = YES;
moviePlayer.repeatMode = MPMovieRepeatModeOne;
moviePlayer.fullscreen = YES;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
//set the frame of movie player
moviePlayer.view.frame = self.view.bounds;
[self.view insertSubview:moviePlayer.view atIndex:0];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appBecameActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
[self performSelector:#selector(animationCode) withObject:nil afterDelay:0.1f];
}
-(void)appBecameActive{
[moviePlayer play];
}
The log says it all. You are receiving a printed warning that you are using too much memory and your app will shutdown if you do not free up space. You need to take immediate steps to lower your memory line, so don't just shrug off those warnings. Yeah, your app might not crash immediately, but it is often a sign of a much bigger problem in your code setup.
Run the Allocations Instrument in XCode to see where the bulk of your memory is being used. I'd first check the size of that m4v video also. You should be streaming the video if it is a significant size. Additionally, ensure that you are not leaking memory using the Leaks instrument. But once again, when you receive the didReceiveMemoryWarning callback, take immediate action. Either pick up the notification in the AppDelegate or subscribe to the UIApplicationDidReceiveMemoryWarningNotification and release items/viewControllers that can be recreated later.
Here's the Memory Management Guide if you want to consult.
Thannks first to #tdevoy for your advices.
I finally got rid of the warning. It was the file type! I had to convert it to .3gpand now it works much smoother and without warnings.
On strange thing is I even use now 4mb more memory than before. But everything works great..
Related
I want to show a small video (3 sec video) every time when user opens the application on iOS device.
(If the app is opened from memory then don't show video)
I am having my "dashboard" view as root view controller.
I searched all the links on SO and other sites, but none helps.
Sometimes my video is working but later application get crashed.
My dashboardview is from "Main.storyboard" but there are no controls on storyboard.
Only linking of navigation bar is done via storyboard.
Here is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self showVideo];
}
- (void)showVideo
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
NSLog(#"video path :- %#",url);
self.navigationController.navigationBar.hidden = YES;
self.videoController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
self.videoController.moviePlayer.controlStyle = MPMovieControlStyleNone;
//[self presentMoviePlayerViewControllerAnimated:videoController];
[self.navigationController pushViewController:self.videoController animated:NO];
[self.videoController.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.videoController];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
self.navigationController.navigationBar.hidden = NO;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self.videoController.view removeFromSuperview];
[self.videoController.moviePlayer stop];
self.videoController = nil;
[self.navigationController popViewControllerAnimated:NO];
//[self.view removeFromSuperview];
}
the nslog is printing video path correctly.
like
video path :- file:///Users/itshastra/Library/Developer/CoreSimulator/Devices/87C93694-66E8-4884-B087-10E1E4CBA4D1/data/Containers/Bundle/Application/DB6C89D4-EE6D-4830-B208-B4AA89FD8E59/Complaint.app/video.mp4
But App get crashed,
Neither it shows the video and then dashboard, not dashboard directly.
Can any one please guide me, what I am doing wrong.
Thanks in advance.
If by mistake I missed any link on SO, please provide that as well.
Thanks.
Try calling method like this,
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(showVideo) withObject:nil afterDelay:0.1f];
}
Please enable the option "Enable Zombie Objects" under following path:
Edit Scheme -> Diagnostics -> Enable Zombie Objects
and let me know the logs.
I'm really going crazy with my welcome view controller.
I have a video in background in continuos loop but every solution that I used causes a small pause/flash when the video is finished and loop.
I use two solution: MPMoviePlayerController and AVPlayer from AVFoundation but I got the same result, a small white flash when video is looped for replay.
My MPMoviePlayerController solution (I prefer a fix for this)
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"welcome_video" withExtension:#"mp4"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
self.moviePlayer.view.frame = self.view.frame;
[self.view insertSubview:self.moviePlayer.view atIndex:0];
[self.moviePlayer prepareToPlay];
// Loop video
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loopVideo) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
}
- (void)loopVideo
{
[self.moviePlayer play];
}
My AVPlayer solution
(void)viewDidLoad
{
[super viewDidLoad];
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
//Set up player
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"welcome_video" ofType:#"mp4"]];
AVAsset *avAsset = [AVAsset assetWithURL:movieURL];
AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
self.avplayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
AVPlayerLayer *avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:self.avplayer];
[avPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[avPlayerLayer setFrame:[[UIScreen mainScreen] bounds]];
[self.movieView.layer addSublayer:avPlayerLayer];
//Config player
[self.avplayer seekToTime:kCMTimeZero];
[self.avplayer setVolume:0.0f];
[self.avplayer setActionAtItemEnd:AVPlayerActionAtItemEndNone];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avplayer currentItem]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerStartPlaying)
name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.avplayer pause];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.avplayer play];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
- (void)playerItemDidReachEnd:(NSNotification *)notification
{
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
- (void)playerStartPlaying
{
[self.avplayer play];
}
What's wrong with these implementation? I really try different fixes found on this site but nothing seems to work.
Any suggestions?
Hm.. I might have an idea for the AVPlayer-approach.
The AVPlayer has this method:
- (void)setRate:(float)rate
time:(CMTime)itemTime
atHostTime:(CMTime)hostClockTime
This means that you can specify WHEN you want the AVPlayer to start at kCMTimeZero. I have not actually used it, but I have an idea for how it can work.
You need to know exactly the moment you start the video the first time. I see you have your own -(void)playerStartPlaying which is called by the notificationCenter on didBecomeActive. I suggest using this method for app variations of [player play];, so put
[self playerStartPlaying];
inside viewDidAppear instead of [self.avplayer play];. It might be good enough.
If you here manage to find the device's hostClockTime, and add the length of the video, you should end up with the exact time when you want it to start from scratch. I am not testing any of this, and I'm typing from head, so you need to understand what I'm doing, and fix it yourself.
- (void)playerStartPlaying
{
//The device's hostClockTime. Basically a number indicating how long the device has been powered on.
CMTime hostClockTime = CMClockGetHostTimeClock;
//A CMTime indicating when you want the video to play the next time.
CMTime nextPlay = CMTimeAdd(hostClockTime, self.avplayer.currentItem.duration);
/* I don't know if that was correct or not, but you'll find out */
//Start playing if we're not already playing. There might be an avplayer.isPlaying or something, I don't know, this is probably working as well..
if(self.avplayer.rate != 1.0)
[self.avplayer play];
//Tell the player to restart the video at the correct time.
[self.avplayer setRate:1.0 time:kCMTimeZero atHostTime:nextPlay];
}
You'll have to remove the entire AVPlayerItemDidPlayToEndTimeNotification-thing. When the video has reached the end, it's already too late. What we're doing now is telling it when to play the second turn when we start the first. We want nothing to happen when didPlayToEndTime is fired, we're handling it manually.
So, if you understand what I have done above, you'll also notice that the video only will play twice. We tell the video to play, at the same time as we tell it to replay at time = now+videoLength. When that replay is done, nothing happens. It simply reaches end. To fix this, you'll need to somehow call -(void)playerStartPlaying at the same time as the setRate:time:atHostTime is executed on the AVPlayer. I guess you could start an NSTimer or dispatch_time and let it execute he method in exactly nextPlay-amount of time, but that would kinda defeat the purpose of this thing. Maybe not. You could try different stuff out. You probably CAN do this with some success, but I suggest finding a way to register for when the player started from the start. Maybe you can observe the rate or something, I don't know.. If you want to try it with a delayed method, you can try this:
double delayInSeconds = CMTimeGetSeconds(self.avplayer.currentItem.duration); //or something
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self playerStartPlaying];
});
Just keep in mind that this is some recursive shit, so even if you pause the video, this will still keep calling after that duration. In that case I suggest making a BOOL paused; and cancel the execution of the entire playerStartPlaying if it's set to YES. ..and of course set paused = YES; whenever you want to pause, next to wherever you say [player pause];
If this actually works, but you still get flashes, I know there are several ways to improve this. For instance, the CMTimeAdd() should probably be using some kind of synchronization-tool to make sure the times add up using the correct timeScale etc.
I have now spent way too much time writing this, and it might not even work. I have no idea. Good luck and good night.
In my application i play video using mpmovieplayercontroller
first set scaling mode to MPmovieScalingmodefill and display video correct to scalingmode.
then after i view video in full screen and exit full screen then not set scaling mode to
MPmovieScalingmodeFill and display video in defualt mode.
below my code for video playing
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(ExitFullScreen:)
name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[appDelegate.moviePlayerController setContentURL:fileURL];
if ([appDelegate checkDevice])
{
[appDelegate.moviePlayerController.view setFrame:CGRectMake(0,0, 320,463)];
}
else
{
[appDelegate.moviePlayerController.view setFrame:CGRectMake(0,0, 320,375)];
}
[appDelegate.moviePlayerController prepareToPlay];
appDelegate.moviePlayerController.scalingMode=MPMovieScalingModeFill;
appDelegate.moviePlayerController.controlStyle=MPMovieControlStyleDefault;
appDelegate.moviePlayerController.shouldAutoplay=NO;
[appDelegate.moviePlayerController setFullscreen:YES animated:YES];
[appDelegate.moviePlayerController play];
[self.view addSubview:appDelegate.moviePlayerController.view];
- (void)ExitFullScreen:(NSNotification *)notification{
NSLog(#"Exit full Screen");
[appDelegate.moviePlayerController setControlStyle:MPMovieControlStyleEmbedded];
[appDelegate.moviePlayerController setScalingMode:MPMovieScalingModeFill];}
so my probleem is how can set scaling mode after exit full screen or do not change scaling mode after exit screen ?
please help me out.
thanks.
This isn't the "ideal" solution, but it works!
Basically, once you exit full screen the MPMoviePlayerController instance gets all screwed up and resetting the scaling property to MPMovieScalingModeFill won't help no matter where or when you do it (I've tried all sorts of stuff and after an hour gave up). Easiest solution is to remove the MPMoviePlayerController and simply allocate a new instance of MPMoviePlayerController each time full screen is exited (not ideal, but totally works):
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:NO];
if (self.moviePlayer != nil)
[self.moviePlayer.view removeFromSuperview];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL];
self.moviePlayer.view.frame = CGRectMake(#, #, #, #);
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayer.shouldAutoplay = NO;
[self.moviePlayer setContentURL:self.videoURL];
[self.moviePlayer prepareToPlay];
[self.moviePlayer setScalingMode:MPMovieScalingModeFill];
[self.view addSubview:self.moviePlayer.view];
}
PS: Don't forget to call super's viewDidAppear or suffer all sorts of unforeseeable mayhem (a very common mistake in iOS development)
I believe this will generate the MPMoviePlayerScalingModeDidChangeNotification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieScalingModeDidChange:)
name:MPMoviePlayerScalingModeDidChangeNotification
object:nil];
MPMoviePlayerScalingModeDidChangeNotification
Posted when the scaling mode of a movie player has changed. There is no userInfo dictionary. Scaling mode can change programmatically or by user interaction. To set or retrieve the scaling mode of a movie player, access its scalingMode property. The movie player whose state has changed is available as the object associated with the notification.
I am trying to use MPMoviePlayerController to play m4v file before the game starts. The game is cocos2d based.
I made a MPMoviePlayerController with the required file and added it as child to CCDirector (which is a subclass of UIViewController). The reason why I am adding MPMoviePlayerController as child is that I want the game to keep loading in background.
My problem is that I am unable to find a way to remove the MPMoviePlayerController after the video finishes.
Please give suggestions.
Thanks in advance.
For cocos2D game CCVideoPlayer class is best and easy to play video. CLICK HERE FOR EXTENSION
You should be able to listen for the MPMoviePlayerPlaybackDidFinishNotification notification, then remove the subview. This code worked in my tests:
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.player = moviePlayer;
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.shouldAutoplay = YES;
[moviePlayer prepareToPlay];
moviePlayer.view.frame = self.view.bounds;
moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
// wait for the video to finish...
MyViewController __weak *weakSelf = self;
[[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:self.player queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
[[NSNotificationCenter defaultCenter] removeObserver:weakSelf name:MPMoviePlayerPlaybackDidFinishNotification object:weakSelf.player];
// remove the view
[weakSelf.player.view removeFromSuperview];
}];
If that doesn't work, you may need to post your player creation and setup code for us to help you figure out what's going on.
I have a weird problem for you all.
MPMoviePlayerController is playing the video fine, and the audio ONLY plays through headphones.
The real drag is that this only happens on some iPads and iPhones, even the SAME EXACT MODELS
running the SAME EXACT SYSTEM!
I've created a simple failing example here:
http://www.porcaro.org/MPMoviePlayerController/TestMovie.zip
I've seen it run fine and fail on iPhone 4S, iPhone 4 and iPad 2.
Here is the most relevant code. Thanks for any insight, I'm going to submit a bug to Apple as well:
(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
moviePath = [NSString stringWithFormat:#"%#/intro.m4v", [[NSBundle mainBundle] bundlePath]];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
theMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
controlStyle = MPMovieControlStyleEmbedded;
movieView = [self view];
movieRect = [[self view] frame];
controlStyle = MPMovieControlStyleFullscreen;
theMoviePlayer.controlStyle = controlStyle;
theMoviePlayer.view.userInteractionEnabled = YES;
if (1) {
NSLog(#"Created theMoviePlayer: %#. Playing: %#", theMoviePlayer, moviePath);
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(checkForEndOfMovie:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:theMoviePlayer];
// this line doesn't fix the problem
//[theMoviePlayer prepareToPlay];
[[theMoviePlayer view] setFrame:movieRect];
[movieView addSubview: [theMoviePlayer view]];
[theMoviePlayer play];
}
This is an old question but maybe it'll help someone. I came across the same issue and found out it was happening when the phone is in silent mode only.
Solution is to set the player's useApplicationAudioSession property to false.
[theMoviePlayer setUseApplicationAudioSession:NO];
This problem was happen to me once playing video on iPad 2 using mpMoviePlayer. The video was playing perfectly but audio only comes when earphones connected.
Solution to solve the problem:
[theMoviePlayer setUseApplicationAudioSession:NO];