MPMoviePlayerController shows blank screen or crashes app - ios

I have a UIViewController subclass called videoPlayerViewController.
In storyboard I dragged out a UIViewController. I set the class correctly in Storyboard.
I put a button on the screen. When I click the button, it executes the method playVideo:.
- (IBAction)playVideo:(id)sender
{
NSURL *movieURL = [NSURL URLWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: movieURL];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: player.view];
[player play];
}
I get a black screen that does nothing.
This is the simplest version of the code I've had in this method over the last 6 hours.
I've tried every StackOverflow answer out there.
I just want to play a video that I stream from the internet.
If I set:
player.movieSourceType = MPMovieSourceTypeStreaming;
the screen reads "loading" for a second, and then crashes.
What exactly am I doing wrong?
I understand there are so many MPMoviePlayerController questions out there, and to just ask a general "How does it work" question seems ludicrous. I've been at this almost 6 hours now and no Google/Stack Overflow/Apple Documentation search has made this work.

I used your provided code but created a MoviePlayerController property named mcp in the header file that I assigned to player to after creating it. That worked for me. The movie loaded and played just fine.
In the .h file:
#property (strong, nonatomic) MPMoviePlayerController *mcp;
In the .m file:
- (IBAction)playVideo:(id)sender
{
NSURL *movieURL = [NSURL URLWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
_mcp = player;
[self.view addSubview: _mcp.view];
[_mcp play];
}

Related

AVPlayer is not visible in iOS 11

In my app we are using AVPlayer to play a audio file. In iOS 11 Beta versions the audio is playing fine but the player is not visible. its just displaying a black background.
can any one help me to solve this issue?
_avpvc = [[AVPlayerViewController alloc] init];
AVPlayerItem *aPlayerItem = [[AVPlayerItem alloc] initWithURL:url];
_avpvc.player = [[AVPlayer alloc] initWithPlayerItem:aPlayerItem];
_avpvc.player = [AVPlayer playerWithURL:url];
[self.view addSubview:self.avpvc.view];
self.avpvc.view.frame = self.audioPlayerView.bounds;
self.avpvc.view.autoresizingMask =
UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.audioPlayerView addSubview:self.avpvc.view];
[self.avpvc didMoveToParentViewController:self];
[_avpvc.player play];
You need to create your own custom player to serve the purpose.Using UISliders and UIbuttons.By Default you won't get Player UI.

I don't have interactivity with the video player when I put it in a pop-up

When I click on a picture, I show a pop-up with a video in it. In that pop up, a video is launching automatically. The problem is that I don't have interactivity with the video (play, pause, etc...). So for now, I found the way to loop it with the AVPlayerItemDidPlayToEndTimeNotification but I would like the user has the capability to play/pause.
Here is my code :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:player];
NSURL *url = [NSURL fileURLWithPath:mediaPath];
player = [[AVPlayer alloc] initWithURL: url];
controller = [[AVPlayerViewController alloc] init];
controller.player = player;
controller.delegate = self;
controller.showsPlaybackControls = YES;
UIImageView *popView = [[[[[[NSBundle mainBundle] loadNibNamed:mediaType owner:self options:nil] objectAtIndex:0] viewLoadedFromXib] subviews] objectAtIndex:0];
[controller.view setFrame:CGRectMake(0, 0, 850, 600)];
[popView addSubview:controller.view];
[player play];
I precise that when I click on the video, the bar with the pause video appears below, but when I click on the button, nothing happens.
1ST EDIT: It seems that there is a AVTouchIgnoringView that has been added automatically by the AVPlayerViewController. It seems that there is a link with UserInteraction... But I try to put it to YES without success. May be I don't do it for the good view. Any ideas ?
2ND EDIT : this is the setFrame that adds the AVTouchIgnoringView. So do you know how can I change the size of the frame without it adding automatically that AVTouchIgnoringView ?
First, I think that the UIImageView is not correct as you put a video in it, so I advise you to change in UIView.
UIView *popView = [[[[[[NSBundle mainBundle] loadNibNamed:mediaType owner:self options:nil] objectAtIndex:0] viewLoadedFromXib] subviews]
Second, I think that you may have user interactivity with the video. Try a bigger size to display the bottom bar with controls.
//video resize
controller.view.frame = CGRectMake(0, 0, 900, 600);//for example
In my case touch on the AVPlayerViewController view was ignored because of the gesture recognizer on one of its superviews. Mentioned recognizer was returning NO from the method: canBePreventedByGestureRecognizer:.
Adding an exception for the recognizer owned by AVPlayerViewController fixed the issue and the UI is usable again.

MPMoviePlayerController backward buffer video again

I am using a MPMoviePlayerController to play video from server.it's work fine,no issue.Now once i play buffered video and then go backward it start to buffered again. Is that a encoding issue or any other.Any help or suggestion would be appreciate
Try below code
#import <MediaPlayer/MediaPlayer.h>
#property (strong, nonatomic) MPMoviePlayerController *streamPlayer;
NSURL *streamURL = [NSURL URLWithString:#"http://clips.vorwaerts-gmbh.de/VfE_html5.mp4"];
_streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];
// depending on your implementation your view may not have it's bounds set here
// in that case consider calling the following 4 msgs later
//http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8
[self.streamPlayer.view setFrame: self.view.bounds];
self.streamPlayer.controlStyle = MPMovieControlStyleDefault;
[self.view addSubview: self.streamPlayer.view];
[self.streamPlayer play];

Play video in fullscreen while click playbutton

I have video play play. I'm displaying the video in center of the view adjusting of CGRectMake. But when i click the play button i need to play video in full screen mode automatically without pressing maximize icon. Is it possible?
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"solar" ofType:#"mp4"]]];
[[moviePlayerController view] setFrame:CGRectMake(172, 210, 680, 400)];
[moviePlayerController setShouldAutoplay:NO];
[moviePlayerController setControlStyle:MPMovieControlStyleEmbedded];
[[moviePlayerController backgroundView] setBackgroundColor:[UIColor clearColor]];
[moviePlayerController prepareToPlay];
[self.view addSubview:moviePlayerController.view];
Set MoviePlayer to FullScreen by below method
mpviewController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self presentModalViewController:mpviewController animated:NO];
This will embed your video to your iOS app:
Add MediaPlayer.framework
Add your video to the root of your project
In your .h file add the following
#property(nonatomic, strong) MPMoviePlayerViewController *moviePlayer; - Put this under #interface ViewController :UIViewController;
in your .m file, add the following
#synthesize moviePlayer; -Put this under #implementation ViewController
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"your video name" ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
First try to use add observer pattern to know when movie player begins
to play at that time switch it to full screen mode. I am giving you
the link just check it. It may help you . In this link the observer
pattern is used for checking whether the player is enter on fullscreen
and in selector method what he wants to do he did. So u can also use
same methodology and instead of checking for fullscreen u should check
for play begin and on selector method set it to full screen..
MPMoviePlayerController stops working in full screen mode // portrait orientation // iOS 7

iOS: MPMoviePlayer won't play

Using the code at the bottom of this post I can successfully display a movie. However I need to now use moviePlayer as a property rather than a local variable. So by adding the following in the header file:
#property (retain, nonatomic) MPMoviePlayerController *mPlayer;
...and synthesizing it as:
#synthesize mPlayer = _mPlayer
Here's the original code. This works fine. All I change is the initialisation line where instead of using a local variable I use the property. A
MPMoviePlayerController * mPlayer = [[[MPMoviePlayerController alloc] initWithContentURL:videoURL] autorelease];
mPlayer.shouldAutoplay=YES;
[self.welcomeImage setUserInteractionEnabled:YES];
[mPlayer.view setFrame: self.welcomeImage.bounds]; // player's frame must match parent's
[self.welcomeImage addSubview:mPlayer.view];
mPlayer.scalingMode = MPMovieScalingModeAspectFit;
mPlayer.controlStyle = MPMovieControlStyleEmbedded;
[mPlayer prepareToPlay];
[mPlayer play];
I just get a black box, no controls or video. What am I doing wrong?
Your not using your synthesized property. Change mPlayer to _mPlayer.
_mPlayer = [[[MPMoviePlayerController alloc] initWithContentURL:videoURL] autorelease];
_mPlayer.shouldAutoplay=YES;
[self.welcomeImage setUserInteractionEnabled:YES];
[_mPlayer.view setFrame: self.welcomeImage.bounds]; // player's frame must match parent's
[self.welcomeImage addSubview:_mPlayer.view];
_mPlayer.scalingMode = MPMovieScalingModeAspectFit;
_mPlayer.controlStyle = MPMovieControlStyleEmbedded;
[_mPlayer prepareToPlay];
[mPlayer play];
This should work as long your videoURL is valid.

Resources