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.
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];
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 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];
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.