I was very delighted to see that when "upgrading" to Xcode 4.5, now none of the videos in my app play at all.
Generally I do something like this:
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getBundleClip:#"theVideo"]];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.shouldAutoplay = YES;
self.moviePlayer.repeatMode = MPMovieRepeatModeOne;
self.moviePlayer.view.frame = self.container.frame;
self.moviePlayer.view.userInteractionEnabled = NO;
[self.container addSubview:self.moviePlayer.view];
- (NSURL*)getBundleClip:(NSString*)clip
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:clip ofType:#"mp4"];
return [NSURL fileURLWithPath:moviePath];
}
Again, everything played perfectly before I updated Xcode to 4.5. Anyone else have this issue?
I also get this output:
[MPAVController] Autoplay: Disabling autoplay for pause
[MPAVController] Autoplay: Disabling autoplay
I suppose that you define it within function?
if you use ARC, you have to retain MPMoviePlayerController
Add it to interface file!
#property(nonatomic, strong) MPMoviePlayerController *moviePlayer;
I'm not sure why it is not playing specifically, but it looks like behavior is different depending on which version of iOS SDK you build against. Look at the MPMoviePlayerController documentation.
In particular, you may want to try calling the prepareToPlay method on the movie player.
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];
Actually i am using MPMoviePlayerViewController in our app . And my code perfectly working in iOS 8.4 but when i upload my app into iPad (iOS 9 ) its not working ...
I am create ViewController to play two video one by one but when one video is completed or stop the controller going to my first page of app and behave unespected in iOS 9 .I am using custom MPMoviePlayerViewController to play video and put one button on first video to change other video but its not working...
NSURL *URL = [[NSBundle mainBundle] URLForResource:#"video" withExtension:#"mp4"];
self.indiaplayer = [[MPMoviePlayerController alloc] initWithContentURL:URL];
self.indiaplayer.view.frame = CGRectMake(0, 0,1024,718);
[self.indiavideoThumbnail addSubview: self.indiaplayer.view];
self.indiaplayer.controlStyle = MPMovieControlStyleNone;
self.indiaplayer.fullscreen = YES;
self.indiaplayer.repeatMode = YES;
[self.indiaplayer prepareToPlay];
self.indiaplayer.shouldAutoplay = NO;
self.indiaplaybackView = [[UIView alloc] initWithFrame:CGRectZero];
self.indiaplaybackView.backgroundColor = [UIColor colorWithRed:242.0/255 green:163.0/255 blue:10.0/255 alpha:1];
//self.playbackView.layer.cornerRadius = 2;
[indiaView addSubview:self.indiaplaybackView];
self.indiaslider.minimumValue = START;
self.indiaslider.maximumValue = END;
Thanks in advance
wait wait in iOS 9.x.x
IMPORTANT
The MPMoviePlayerViewController class is formally deprecated in iOS 9. (The MPMoviePlayerController class is also formally deprecated.) To play video content in iOS 9 and later, instead use the AVPictureInPictureController or AVPlayerViewController class from the AVKit framework, or the WKWebView class from WebKit.
For more detail
https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMoviePlayerViewController_class/
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];
}
Ive used SystemSound in my app in order to play simple sound effects. In addition to this I play a musicvideo through the MPMoviePlayerController - now when I turn the volume up/down the music from the video responds as intended (lowering the volume up/down).
But the system sounds that are played does not respond to the volume. Im playing the system sounds when the user taps certain areas in the app. Heres a snippet of my code for that:
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
SystemSoundID completeSound = nil;
//yellow folder in xcode doesnt need subdirectory param
//blue folder (true folder) will need to use subdirectory:#"dirname"
NSURL *sound_path = [[NSBundle mainBundle] URLForResource: target_sound_filename withExtension: #"wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)sound_path, &completeSound);
AudioServicesPlaySystemSound(completeSound);
}
PS. I doublechecked that my "Settings->Sounds->Ringer and Alerts->Change With Buttons" is set to ON (as I read on some other SO answers that leaving this option OFF will cause systemsound to not respond to the volume buttons)
Further the reason for using systemsound is that it gave the most accurate and responsive results when playing multiple sounds (like in a game).
Id prefer to not use OpenAL if possible (even through 3rd party sound libraries like Finch or CocosDenshion)
Any ideas?
Use the AVAudioPlayer class for playing sounds that are controlled by the user's volume settings (non-system sounds).
You can retain instances of AVAudioPlayer for each sound file that you use regularly and simply call the play method. Use prepareToPlay to preload the buffers.
Cheers to Marcus for suggesting that i could retain instances of AVAudioPlayer for each sound file and use prepareToPlay to preload the sounds. It might be to help for others looking for the same solution so here is how I did it (feel free to comment if anyone have suggestions for improvements)
//top of viewcontroller.m
#property (nonatomic, strong) NSMutableDictionary *audioPlayers;
#synthesize audioPlayers = _audioPlayers;
//on viewDidLoad
self.audioPlayers = [NSMutableDictionary new];
//creating the instances and adding them to the nsmutabledictonary in order to retain them
//soundFile is just a NSString containing the name of the wav file
NSString *soundFile = [[NSBundle mainBundle] pathForResource:s ofType:#"wav"];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile] error:nil];
//audioPlayer.numberOfLoops = -1;
[audioPlayer prepareToPlay];
//add to dictonary with filename (omit extension) as key
[self.audioPlayers setObject:audioPlayer forKey:s];
//then i use the following to play the sound later on (i have it on a tap event)
//get pointer reference to the correct AVAudioPlayer instance for this sound, and play it
AVAudioPlayer *foo = [self.audioPlayers objectForKey:target_sound_filename];
[foo play];
//also im not sure how ARC will treat the strong property, im setting it to nil in dealloc atm.
-(void)dealloc {
self.audioPlayers = nil;
}
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.