Stop dismissing in an animated fashion for MPMoviePlayerViewController? - ios

I have a MPMoviePlayerViewController. That ViewController always dismisses in an animated fashion. I want to dismiss it in 'NO animated' fashion. How can I do that?
NSURL *url = [NSURL fileURLWithPath:filePath];
moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentViewController:moviePlayer animated:NO completion:nil];
[moviePlayer.moviePlayer play];
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[moviePlayer.moviePlayer stop];
[self dismissViewControllerAnimated:NO completion:nil];
}
Currently, from what I have tried, all dismissings are in an animated fashion and I don't know why.

Related

How do I dismiss the AVPlayer automatically when playback has completed?

I have an app that plays local videos using the AVPlayerViewController.
But when the video has finished playing I want it to close automatically (like the MPMoviePlayerViewController used to do).
After posting this question, I managed to solve this partially thanks to the answers below and partially from Apples "Guides and Sample code".
This is the code that worked for me:
#import "ViewController.h"
#import AVFoundation;
#import AVKit;
#interface ViewController ()
#property(nonatomic, readonly) AVPlayerItem *currentItem;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)playerItemDidReachEnd:(NSNotification *) notification {
//remove the player
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)playVideo:(id)sender {
// grab a local URL to our video
NSURL *videoURL = [[NSBundle mainBundle]URLForResource:#"bagare" withExtension:#"mp4"];
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];
[self presentViewController:controller animated:YES completion:nil];
// show the view controller
controller.view.frame = self.view.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:_currentItem];
}
#end
NSURL *url=[NSURL URLWithString:#"URLLINK"];
AVAsset *avAsset = [AVAsset assetWithURL:url];
AVPlayerItem *playerItem = [[AVPlayerItem alloc]initWithAsset:avAsset];
Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
Pass the AVPlayerItem to a new player
AVPlayer *player = [AVPlayer playerWithURL:videoURL]
AVPlayerViewController *controller=[[AVPlayerViewController alloc]init];
controller.player=player;
[player play];
[self presentViewController:controller animated:YES completion:nil];
Here remove Player dismiss or Player added view
-(void)itemDidFinishPlaying:(NSNotification *) notification {
[self dismissViewControllerAnimated:YES completion:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
}
This should work:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoDidFinish:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[controller.player currentItem]];
- (void)videoDidFinish:(id)notification
{
AVPlayerItem *p = [notification object];
//do something with player if you want
[[NSNotificationCenter defaultCenter] removeObserver:self];
//fade out / remove subview
}
In .h file
#property(nonatomic, strong)AVPlayerViewController *controller;
In .m file:
- (IBAction)playVideo:(id)sender {
// grab a local URL to our video
NSURL *videoURL = [[NSBundle mainBundle]URLForResource:#"bagare" withExtension:#"mp4"];
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
// create a player view controller
self.controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];
[self presentViewController:controller animated:YES completion:nil];
// show the view controller
controller.view.frame = self.view.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:_currentItem];
}
And for dismiss:
- (void)playerItemDidReachEnd:(NSNotification *)notification {
[self.controller dismissViewControllerAnimated:YES completion:nil];
}

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 do i fix MPMoviePlayerViewController

i click on the button here is the code
//NSURL *url = [NSURL URLWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:self.urlFile];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
_moviePlayer.view.frame = CGRectMake(0, 0, 320, 300);
[_moviePlayer setFullscreen:YES animated:YES];
and it plays but when i press the Done button, here is the method
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
player.initialPlaybackTime = -1;
[player.view removeFromSuperview];
}
it looks like this
sorry dont have enough to post image
it has the black lining on the bottom and the play button disappears as well, how do i load it back to the point it was when the cell was pressed and came to the detailViewController

MP movie controller removes when done button is clicked in iphone

I am playing video in MPmovieController it works fine but player removes when complete video is played.I want that when user pressed done button then video should stop here is the code i am using.
NSURL*myURL=[NSURL fileURLWithPath:url];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:myURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view setFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
Try this one:
NSURL *fileURL=[NSURL URLWithString:[[array objectAtIndex:videoid] valueForKey:#"VideoUrl"]];
self.mpPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[self presentMoviePlayerViewControllerAnimated:self.mpPlayer];
[self.mpPlayer.moviePlayer prepareToPlay];
self.mpPlayer.moviePlayer.shouldAutoplay=NO;
[self.mpPlayer.moviePlayer play];
- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer pause];
[self dismissMoviePlayerViewControllerAnimated];
// [moviePlayer.view removeFromSuperview];
}
may be it will help.
happy coding...
remove this line into your then it will work
[moviePlayerController.view removeFromSuperview];

view doesn't come back after video plays

I'm running the following code. The video plays fine but after it finishes it just goes to a black srcreen, my original view never comes back. When I tap on the black screen i just see the message "loading....." Can someone please explain what I'm doing wrong. Thanks
- (IBAction)video:(UIBarButtonItem *)sender
{
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"IMG_0973" ofType:#"MOV"]];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDonePressed:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
moviePlayer.controlStyle=MPMovieControlStyleDefault;
//moviePlayer.shouldAutoplay=NO;
[moviePlayer play];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
}
- (void) moviePlayBackDonePressed:(NSNotification*)notification
{
[moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
}
moviePlayer=nil;
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
}
}
Add this notification Method
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification
object:player];
This method is called after your movie is loaded and in this method you add your moviePlayer view.
-(void)moviePreloadDidFinish:(NSNotification*)notification
{
moviePlayer.controlStyle=MPMovieControlStyleDefault;
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
[moviePlayer setFullscreen:YES animated:YES];
}
In your video IBAction, you need to add the subview before you tell it to play. Switch the lines [moviePlayer play] and [self.view addSubview:moviePlayer.view]. Let us know it it works! Actually you may need to place moviePlayer play even after the full screen line.
i think this will help you .....
-(void)playVideo
{
NSString *contentURL = [[NSBundle mainBundle] pathForResource:#"xyz" ofType:#"mp4"];
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:contentURL]];
if (moviePlayerViewController)
{
[moviePlayerViewController.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
[moviePlayerViewController.moviePlayer setFullscreen:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MovieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
[moviePlayerViewController.moviePlayer play];
[navi presentModalViewController:moviePlayerViewController animated:NO];
[moviePlayerViewController release];
moviePlayerViewController = nil;
}
}
-(void)MovieFinished:(NSNotification *)notification
{
MPMoviePlayerController *player = (MPMoviePlayerController *)notification.object;
[player stop];
[[NSNotificationCenter defaultCenter] removeObserver:self];
//do rest of the stuff
}

Resources