I am trying to play a video file (m4v) from resource folder of my app (i.e local video). but I am not able to play the video. I referred apple's sample project for the MPMoviePlayer.
Also when i am giving background color to my movieplayercontroller object its appearing as its behind some black view and the black view ahead is showing that something is loading for few seconds but nothing get loaded and it results in black screen only.
Can someone help me out with it. please tell me what i am doing wrong.
Also, i have taken screenshot of the screen with problem so that you all can understand what i am trying to say but i dont know how to post it here. :(
NSString *urlString = [[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v"];
NSURL *urlObj = [NSURL fileURLWithPath:urlString];
UIGraphicsBeginImageContext(CGSizeMake(1,1));
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:urlObj];
UIGraphicsEndImageContext();
[player.view setBounds:self.view.bounds];
// when playing from server source type shoud be MPMovieSourceTypeStreaming
[player.moviePlayer setMovieSourceType:MPMovieSourceTypeStreaming]; // I was missing this line therefore video was not playing
[player.moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[self.view addSubview:player.view];
[player.moviePlayer play];
Related
After the release of iOS 13, there was some exception in one of our video playback interfaces.
This interface is to play video using MPMoviePlayerController.
In some cases, videos that would only be played once will now automatically loop.
At the same time, the MPMoviePlayerPlaybackDidFinishNotification notification is also not received.
From the feedback of users, this problem will only appear on iOS13 devices.
After testing, I found that only one iPhone11 in all iOS13 devices will have this problem, and other iOS13 devices (including another iPhone11) have no such problem.
When the problem occurred, I printed the value of the repeatMode property and found that it is still 0.
Later I tried to manually set the repeatMode to MPMovieRepeatModeNone and found that this problem still occurs.
Some additional information:
1) For some reason, I am still using Xcode10.3 (Base SDK: iOS12.4) to compile the project, so using MPMoviePlayerController does not crash directly.
2) The problematic device is the partner's test machine, so I can only remotely instruct them to test, there is no way to debug locally.
Here is the code I used to test:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:filePath];
self.playerController = [[MPMoviePlayerController alloc] init];
[self.playerController setScalingMode:MPMovieScalingModeAspectFit];
[self.playerController.backgroundView setBackgroundColor:[UIColor clearColor]];
self.playerController.movieSourceType = MPMovieSourceTypeFile;
[self.playerController setControlStyle:MPMovieControlStyleNone];
[self.playerController.view setFrame:self.view.bounds];
self.playerController.view.userInteractionEnabled = YES;
[self.view addSubview:self.playerController.view];
self.playerController.contentURL = url;
[self.playerController prepareToPlay];
[self.playerController play];
I am little bit confused about the behaviour.
What I want to do is,
I am having two different application.
One is made by me and other is by a guy who left the company & now I have to complete his projects.
But the task to be done in both project is same.
I have to show 5 sec video when I open the app every time.
Like Some branding video. (Done in angry birds app)
I checked all the links on SO, but didn't helped any one.
My senior who left the company, he have used Main.storyboard for navigation each screen.
and I am not that much good in using storyboard.
In my app, I created navigation programatically.
When my splash screen disappears,
I show dashboard.
But before showing dashboard, I have to show the video every time.
So what I did,
I created a view controller only for Video named VideoViewController,
and call that view controller from ViewDidLoad of dashboardView.
and when my video finished,
then by using NSNotificationCenter I removed the VideoViewController.
When I Nslog the url path, its printing correct, but My app crashes after that.
I checked it by applying ExceptionalBreakpoints, but didn't helped.
Here is my code to launch video in ViewDidLoad of VideoViewController
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"video" ofType:#"mp4"]];
NSLog(#"video path :- %#",url);
playercontroller = [[MPMoviePlayerController alloc] init];
[playercontroller setContentURL:url];
[playercontroller.view setFrame:CGRectMake (0, 0, 320, 460)];
[self.view addSubview:playercontroller.view];
[playercontroller play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playercontroller];
and I am pushing VideoViewController from dashboard's
-(void)viewDidAppear:(BOOL)animated
method.
As I am doing any thing wrong.
As My url is getting Nslg well, so no doubt of loading video.
Please guide me for the same.
also guide me to achieve same task using storyboard.
Thanks in advance.
you shouldn't hardcode set the frame like that though..
Assuming 'self' is a ViewController (which has been properly addChildViewController'ed all the way from the RootViewController), try the next thing:
NSString *path = [[NSBundle mainBundle] pathForResource:videoFileName ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
MPMoviePlayerViewController *videoController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:videoController];
[videoController.moviePlayer play];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = [AVPlayer playerWithURL:[[NSBundle mainBundle]
URLForResource:#"Besan"
withExtension:#"mp4"]];
[playerViewController.player play];
I am trying to use the AVPlayerViewController in iOS 8. But the video does not load. I simply get the screen below.
The entire codebase is here https://github.com/sairamsankaran/AVPlayerDemo
I download your project and had a look. The solution is pretty simple, you forgot to include the video file in your target, which means when you're setting up the URL you're pointing to a file that doesn't exist, which means the NSURL is nil.
In the Utilities side bar in Xcode, with your mp4 file chosen in the navigator, choose the File Inspector and in "Target Membership" be sure to check your app target.
Once you have done this your movie will play on launch.
You might be better off starting with a working example and extending that. If you like, you can try a working xcode example project that uses AVPlayerViewController in github described in my blog post, this is an example project that shows a high def SDO video of the Sun, it looks best on a newish retina iPad since the video is very high quality.
#runmad, i also done with the same way,but i'm trying to open local video.here is my code
{
// Initialize the movie player view controller with a video URL string
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
/*NSURL *url = [NSURL URLWithString:#"https://www.youtube.com/watch?v=qcI2_v7Vj2U"];
playerViewController.player = [AVPlayer playerWithURL:url];*/
// playerViewController.player = [AVPlayer playerWithURL: [ [filePath]URLForResource:#"Besan"
// withExtension:#"mp4"]];
NSLog(#"\nFile URL\n%#",videosURL);
AVPlayerItem* item=[[AVPlayerItem alloc]initWithURL:videosURL];
NSLog(#"\n\nAVPlayerItem\n%#",item);
playerViewController.player=[AVPlayer playerWithPlayerItem:item];
[playerViewController.player play];
[self presentViewController:playerViewController animated:YES completion:^{}];
}
i've done without AVPlayerItem but the result is same(Only black screen with done,play,forward,backward buttons).
than i tried with MPMoviePlayerController but it dismissed soon without displaying video.
here is MPMoviePlayerController Code,
{
MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:videosURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
[moviePlayerController.moviePlayer play];
}
I just want to load a video file which is in the main bundle, this seems pretty simple but for some reason I keep getting an error of the MPMoviePlayerController, I have the following code.
- (void)viewDidLoad{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"ipad2" ofType:#"mp4"];
self.myPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:path]];
[self.myPlayer prepareToPlay];
self.myPlayer.movieSourceType = MPMovieSourceTypeFile;
[self.myPlayer.view setFrame:self.view.bounds];
[self.view addSubview:self.myPlayer.view];
[self.myPlayer play];
}
I only get a black screen and the following output:
2013-01-09 13:38:15.686 myVideoApp[1789:907] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2013-01-09 13:38:15.690 myVideoApp[1789:907] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
I also tried adding these notification for playing but is never sent:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(playVideo:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:self.myPlayer ];
And when I print self.myPlayer.loadState I get 0which is undefined loadState.
These is a simple viewController with any other method, and I have these declaration on the .h file:
#property (nonatomic, strong) MPMoviePlayerController *myPlayer;
I`m running on iOS 6, and these happens both in device and simulator
NSString *path = [[NSBundle mainBundle] pathForResource:#"ipad2" ofType:#"mp4"];
Have you checked in the debugger that path is not nil?
self.myPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:path]];
That's the wrong way to create a URL from a file path. Use +[NSURL fileURLWithPath:] instead.
I also using the MPMoviePlayerViewController to live video on my app and getting same above list errors.And I found MPMoviePlayer not support larger data to show video but if you used smaller data of video is working fine its not give any error. In fact is not problem of prepare to play and play property of movie player.
If you need to show larger data then used webview on your app.
I've developed an iPhone app that has been running MPMoviePlayer (pre 3.2 SDK) with no problems. I know this is a newbie question, but how do I get a movie to play in the new MPMoviePlayerViewController. I am only getting audio and wish to learn the new view controller. I've ported my whole app over to iPad and everything else works fine except for video. Could someone please show an example using the movie view controller? Any help would be appreciated.
Thanks,
NSURL* videoURL = [NSURL URLWithString:url];
MPMoviePlayerController moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer prepareToPlay];
[moviePlayer play];
//For viewing partially.....
[moviePlayer.view setFrame:CGRectMake(50, 200, (self.view.frame.size.width)-100 , 400)];
moviePlayer.view.backgroundColor = [UIColor grayColor];
[self.view addSubview:moviePlayer.view];
You need to use MPMoviePlayerViewController, not MPMoviePlayerController. Search the docs for MPMoviePlayerViewController.
Try this in your view controller:
MPMoviePlayerViewController* theMoviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:theMoviePlayer];
This should get you started but check the MPMoviePlayerViewController Class Reference for detailed info.
I've struggled all over the place with this - Apple's own MoviePlayer sample code doesn't play video, only audio. It actually took me ages to figure out that it was playing audio because the sample airplane video they supply has NO AUDIO TRACK!!! (So I click the button and get absolutely nothing.)
Thanks for all the suggestions here. I finally found https://developer.apple.com/library/ios/#qa/qa2010/qa1240.html
and even that is an obscure-ish fix.
This works: in initAndPlayMovie after [mp release]:
[[self.moviePlayer view] setFrame:[window bounds]];// size to fit parent view exactly
[window addSubview:[self.moviePlayer view]];
Bon voyage and happy flying :)
change this line:
MPMoviePlayerController moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
to
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
you need the *
I am pretty sure that its broken in simulator at present ! I get same behavior (no video but hear audio) with latest sdk and their own MPMoviePlayer sample code with Apples own video that Will play in iPad simulator using safari... So bottom line video plays on simulator-ipad inside of Safari but Not from an app using MpMoviePlayer class. Bug or feature, you decide. (I think its just broken). release notes for this version have lots of changes going on in MPMoviePlayer class...