Changing MPMoviePlayerController content not working with performSelectorInBackground - ios

I am trying to change the content of my MPMoviePlayerController method with function running in background.But my content is not getting changed.
[self performSelectorInBackground:#selector(updateVideo) withObject:nil];
This is my viewDidLoad method where I start loading a video.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
VideoView *videoView = [[VideoView alloc] initWithFrame:CGRectMake(0, 0, 293, 320)];
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"My Movie4" ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
controller = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[controller.view setFrame:videoView.bounds];
[videoView addSubview:controller.view];
[self.videoviewer addSubview: videoView];
[controller prepareToPlay];
[controller play];
}
Where VideoView is a subclass of UIView.My updateVideo function is like this.
-(void)updateVideo
{
[controller setContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"video2" ofType:#"mp4"] ]];
[controller prepareToPlay];
[controller play];
}
But the video content is not getting changed when the function is called which is running in background.

Why are you trying to change the content in the background thread ?
You can't change UI element in the background thread. You have to execute this task in the main thread.
Try to do this :
[self updateVideo]
Or this if you want, but I recommend you to use the first method.
-(void)updateVideo
{
[controller setContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"video2" ofType:#"mp4"] ]];
dispatch_async(dispatch_get_main_queue(), ^{
[controller prepareToPlay];
[controller play];
});
}
I hope this can help you

Related

Use video as background of view controller iOS

Does anyone know how I can use a background video as the login like how AirBNB had their log in screen before? I tried to use a gif, but felt like it didn't have great resolution.
Please let me know! thank you!
Have not tried it myself but it should at least send you to the right direction:
- (void)viewDidLoad
{
[super viewDidLoad]
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"your_video_file_name" ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
// or get your videoURL some another way
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer.view setFrame:self.view.bounds];
[self.view insertSubview:moviePlayer.view atIndex:0]; // to be sure it is a background
// [self.view addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
}

Play YouTube Video with URL on iOS

I am trying to play a YouTube video from a URL in my iOS app using MPMoviePlayerController but I just get a black screen and nothing plays. What am I doing wrong? I have verified that the url is correct and is being passed the correct videoId.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlString = [NSString stringWithFormat:#"https://www.youtube.com/watch?v=%#", videoIDString];
NSURL *videoURL = [NSURL URLWithString:urlString];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
moviePlayerController.shouldAutoplay = YES;
moviePlayerController.fullscreen = YES;
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
}

Crash when Running MPMoviePlayerController

Ive been battling with a crash on my exercise app for the past couple of days when I try and play an instance of an MPMoviePlayerController in a UIView created in IB.
The rest of the app runs fine, but if any MPMoviePlayer is instantiated, the debugger pauses the app without raising an exception. It's not just this code that causes it. Other methods of instantiating a moviePlayer from other posts or books cause the same result.
Its when I run this code that it drops out to the main:
NSURL *url = exercise.exerciseVideoURL;//path for vid
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayerController.view setFrame:self.movieHostingView.bounds];
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone];
[self.movieHostingView addSubview:self.moviePlayerController.view];
[[self.moviePlayerController view]
setAutoresizingMask:(UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight)];
[[self.moviePlayerController view] setClipsToBounds:YES];
[[self.moviePlayerController view] setFrame:[[self movieHostingView] bounds]];
[self.movieHostingView addSubview:self.moviePlayerController.view];
self.moviePlayerController.repeatMode = MPMovieRepeatModeOne;
I've tried using MPMoviePlayer instantiating code from other posts, binning off the model and just instantiate it with a direct path like in some of the other posts, but it still drops out to main().
Got it! Ive removed "All Exceptions" from the breakpoint tab and its running fine.Why would that cause it to drop out for MPMoviePlayer?!
Anyhow, Thanks to everyone who helped out.
are you reading video from disk (main bundle) or via internet?
First, be always sure that your filepath is correct:
NSString *filepath = [url absoluteString];
if ([[NSFileManager defaultManager] fileExistsAtPath:filepath])
{
NSLog(#"file exists so init player");
}
else
{
NSLog(#"try again!");
}
In my case I always add MPMoviePlayerController after its loaded.
So you can do something like this:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"demo" ofType:#"mp4"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filepath])
{
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[_moviePlayerController prepareToPlay];
NSLog(#"frame = %#", NSStringFromCGRect(_moviePlayerController.backgroundView.frame));
[_moviePlayerController.view setBackgroundColor:[UIColor yellowColor]];
[_moviePlayerController.view setFrame:CGRectMake(0, 280, 320, 200)];
//
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieLoadStateDidChangeNotification:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:_moviePlayerController];
}
and then some time later:
- (void)movieLoadStateDidChangeNotification:(NSNotification *)notification
{
[self.view addSubview:_moviePlayerController.view];
}

iOS How to play multiple videos on one view

I was wondering how to play multiple videos in one page, like where you have two buttons, and when you press one, it plays a video, and when you press the other it plays a different one. I have this code so far:
-(void)WelcomeVideo1
{
NSURL *url31 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"DirectorsWelcome" ofType:#"mp4"]];
welcomePlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:url31];
welcomePlayer1.controlStyle = MPMovieControlStyleDefault;
welcomePlayer1.shouldAutoplay = YES;
[self.view addSubview:welcomePlayer1.view];
[welcomePlayer1 setFullscreen:YES animated:YES];
}
-(void) moviePlayBackDidFinish:(NSNotification *)aNotification{
[welcomePlayer1.view removeFromSuperview];
welcomePlayer1 = nil;
}
- (void)moviePlayerWillExitFullscreen:(NSNotification*) aNotification {
[welcomePlayer1 stop];
[welcomePlayer1.view removeFromSuperview];
welcomePlayer1 = nil;
}
-(void)storyVideo1
{
NSURL *url4 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"OurStory" ofType:#"mp4"]];
storyPlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:url4];
storyPlayer1.controlStyle = MPMovieControlStyleDefault;
storyPlayer1.shouldAutoplay = YES;
[self.view addSubview:storyPlayer1.view];
[storyPlayer1 setFullscreen:YES animated:YES];
}
-(void) moviePlayBackDidFinish2:(NSNotification *)aNotification{
[storyPlayer1.view removeFromSuperview];
storyPlayer1 = nil;
}
- (void)moviePlayerWillExitFullscreen2:(NSNotification*) aNotification {
[storyPlayer1 stop];
[storyPlayer1.view removeFromSuperview];
storyPlayer1 = nil;
}
but whenever I try to play both videos, the 2nd one I play crashes the app. Any ideas?
Use AVPlayer and AVPlayerLayer instead of MPMoviePlayerController.
Take a look into following apple example.

How to Call Second XIB in IPad at Runtime?

In my application i run the video in my first class XIB using MPMoviePlayerController.my video durtion is about 20 seconds.i want that when my video ends its automatically call the Second Class XIB.here is my code.
-(void)viewWillAppear:(BOOL)animated
{
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"3idiots.mov" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:videoPlayer.view];
videoPlayer.view.frame = CGRectMake(0, 0,768, 1000);
[videoPlayer play];
[self performSelector:#selector(gotonextview)];
}
-(void)gotonextview
{
secondview *sec=[[secondview alloc] initWithNibName:#"secondview" bundle:nil];
[self presentModalViewController:sec animated:YES];
[sec release];
}
This code Give me No Error,but its not call the Second Class after video completion.can any body guide me. Thanx in advance
This is all explained in the docs... also behaviour various between iOS versions.
Do not call gotonextview from viewWillAppear. Instead register your view controller as an observer for MPMoviePlayerPlaybackDidFinishNotification and MPMoviePlayerDidExitFullscreenNotification in viewDidLoad with gotonextview:(NSNotification *)notification as the selector.
Also, I would suggest you launch the movie player from viewDidAppear rather than viewWillAppear.
EDIT: Adapted original posters code (untested)...
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(gotonextview:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(gotonextview:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
}
-(void)viewDidAppear:(BOOL)animated
{
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"3idiots.mov" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:videoPlayer.view];
videoPlayer.view.frame = CGRectMake(0, 0,768, 1000);
[videoPlayer play];
}
-(void)gotonextview:(NSNotification *)notification
{
NSDictionary *notifDict = notification.userInfo; // Please refer Apple's docs for using information provided in this dictionary
secondview *sec=[[secondview alloc] initWithNibName:#"secondview" bundle:nil];
[self presentModalViewController:sec animated:YES];
[sec release];
}
One option is:
Use a tabBarController with two tabs. Place your video in one tab and place your "second view" in the second tab. then use
self.tabBarController.selectedIndex=2;
The better way is to use a Timer or register an event listener to the video player.
Examples can be found under http://mobiledevelopertips.com/cocoa/basics-of-notifications.html

Resources