Replacing AVPlayer with MPMoviePlayerController - ios

I am trying to replace AVPlayer with MPMoviePlayerController as I want to be able to add an animation with transparent background to a view. The problem is the animation is not displaying with MPMoviePlayerController.
Please find my lines below. The first portion is with AVPlayer and it's work. The second is with MPMoviePlayerController and doesn't.
What the code does is it plays an animation and when this is done it is launching an action.
Code with AVPlayer (that works):
NSString *filepath = [[NSBundle mainBundle] pathForResource:theAnimationFileName ofType:theString;
//NSURL *fileURL = [NSURL fileURLWithPath:filepath];
// First create an AVPlayerItem
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:fileURL];
// 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
controlledPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
AVPlayerLayer *animatedLayer = [AVPlayerLayer playerLayerWithPlayer:controlledPlayer];
[animatedLayer setFrame:CGRectMake(0, 0, 1024, 1024)];
[thisReplacementView.layer addSublayer: animatedLayer];
// Begin playback
[controlledPlayer play];
Code with MPMoviePlayerController (does not display anything):
NSString *moviePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:theString];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
moviePlayer.controlStyle = MPMovieControlModeDefault;
moviePlayer.view.frame = CGRectMake(0, 0, 1024, 1024);
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[thisReplacementView addSubview:moviePlayer.view];
[moviePlayer play];

NSURL *url = [NSURL URLWithString:#"http://iOS.mp4"];
self.moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];
[self presentMoviePlayerViewControllerAnimated:self.moviePlayer];
-(void)playbackFinishedCallback:(NSNotification *)notification{
self.moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
self.moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
[self.moviePlayer.view removeFromSuperview];
}

Related

AVQueuePlayer play but no sound

- (void)playVideoInteractionNarration:(NSString *)vNarr{
[self pauseActionExecution];
NSURL *u = [NSURL fileURLWithPath:vNarr];
AVQueuePlayer *player = [[AVQueuePlayer alloc] initWithURL:u];
self.controller = [[AVPlayerViewController alloc]init];
controller.player = player;
controller.showsPlaybackControls=NO;
[controller.player setActionAtItemEnd:AVPlayerActionAtItemEndAdvance];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(videoNarrationFinishedPlaying:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:player.currentItem];
//NSLog(#"MPVIDEOOOOOOO %#", player);
[controller.player setVolume:1.0];
[controller.player setMuted:NO];
[controller setViewInCurrentController];
[controller.player play];
}
- (void)videoNarrationFinishedPlaying:(NSNotification *) aNotification{
AVPlayerItem * item = [aNotification object];
[self.controller removeViewInCurrentController];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:item];
[self.controller.player pause];
[self.controller release];
[self playActionExecution];
}
the video start and finish correctly but NO sound
why ?
the right answer is this:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
#import AVKit;
#import AVFoundation;
#property (nonatomic) AVPlayer *avPlayer;
NSURL *fileURL = [NSURL URLWithString:#"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.view.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]];
[self.avPlayer play];
Try this code and comment your one and check that in your device did sound work or not ?

AVPlayer play library video in view

i want to play a video in a frame of a view, but the following code does not work.
can you help me?
MPMediaItem *song = [allVideos objectAtIndex:indexPath.row];
NSLog(#"%#",[song valueForProperty:MPMediaItemPropertyAssetURL]);
NSURL *url = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVPlayer *player = [[AVPlayer alloc] init];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:avAsset];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
player = [[AVPlayer alloc]initWithPlayerItem:playerItem];
//[player seekToTime:kCMTimeZero];
[player play];
You need to use AVPlayerLayer, add this
AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];
[self.view.layer addSublayer:playerLayer];
to your code.
Have you try to use MPMoviePlayerController as like below
MPMoviePlayerController * _player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
CGRect frame = self.frame;
frame.origin = CGPointZero;
_player.view.frame = frame;
[self.view addSubview:_player.view];
[_player prepareToPlay];
[_player play];
Try this one ..
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
player.view.frame = CGRectMake(0,45, 1024, 723); // take your require frame size of player
[player.view setBackgroundColor:[UIColor clearColor]];
player.repeatMode=MPMovieRepeatModeNone;
[self.view addSubview:player.view];
[player play];

MPMoviePlayerController ipad stuck loading

This is the code i am trying to run to show a movie on the ipad.
What happens is i just see loading... on the bottom of the screen.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *movpath = [[NSBundle mainBundle] pathForResource:#"movie" ofType:#"mov"];
MPMoviePlayerViewController* mpviewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:movpath]];
mpviewController.view.userInteractionEnabled = YES;
[self.view addSubview:mpviewController.view];
MPMoviePlayerController *mp = [mpviewController moviePlayer];
mp.controlStyle = MPMovieControlStyleDefault;
[mp prepareToPlay];
[mp play];

Movie play error

I am trying play a video on device. Video is this
video
and my code:
NSString *urlStr = #"http://easyhtml5video.com/images/happyfit2.mp4";
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 320, 400);
[moviePlayer play];
When i start app an movie screen is seen but movie doesnt play. Can you help me where is the mistake ?
Try use
NSURL *url = [NSURL URLWithPath:urlStr];
instead of
NSURL *url = [NSURL fileURLWithPath:urlStr];
It is a web url not a file url.
Check this video for reference. This works for me -
- (IBAction)playMovie:(id)sender
{
NSString *filepath = #"http://easyhtml5video.com/images/happyfit2.mp4";
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}

Video playback disappears after a few seconds

i want to play a video within a UIView using MPMoviePlayerController.
After pressing a UIButton, the video appears and starts playing.
Unfortunately after 3-4 seconds, the video gets black, the audio is still playing.
Any ideas?
Thanks for all your time.
(Using Xcode 4.2.1)
-(void) playMovieButtonPressed:(UIButton*) button{
NSString* video = #"testmovie.m4v";
NSString *filepath = [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController* player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
player.movieSourceType = MPMovieSourceTypeFile;
[player.view setFrame:CGRectMake(20, 400, 300, 200)];
[self.view addSubview:player.view];
[player prepareToPlay];
[player play];
}
- (void) moviePlaybackComplete:(NSNotification*) notification {
NSLog(#"videoviewcontroller complete: %#", notification);
MPMoviePlayerController *mymoviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:mymoviePlayerController];
}
I´ve talked to an Apple Engineer. The solution is that the player would have to be an instance variable or property of the view controller. So its still accessible when view was being torn down.
-(void) playMovieButtonPressed:(UIButton*) button{
NSString* video = #"testmovie.m4v";
NSString *filepath = [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
// .....
}

Resources