I'm trying to play a YouTube video within an iPhone app using the technique in this URL http://iphoneincubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application
The technique works fine and the video plays fine, except that i want the video to switch back to the main view when its finished. when i click on "done" button of the youtube player it does turn back to the main view but when the video is finished the youtube player doesn't disappear(you have to click "done")
Please help!
NSURL *url = [[NSURL alloc]initWithString:#"url for video"];
self.backgroundColor=[UIColor redColor];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer.view setFrame:CGRectMake(0,0,self.frame.size.width-10,self.frame.size.height-10)];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.shouldAutoplay=YES;
moviePlayer.repeatMode = NO;
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer prepareToPlay];
pause=NO;
[self.view addSubview:moviePlayer.view];
You can use notification to inform when its finished so that you can remove it from the view and let previous view on screen.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.player.moviePlayer];
Don't forget to remove notification in moviePlaybackComplete: by
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
Related
I am using the Notification Center to receive the MPMoviePlayerPlaybackDidFinishNotification, which works as expected.
NSURL * url = [[NSURL alloc] initWithString:#"http://INFINITECREATIVEUNIVERSE.COM/Media/module_1_Dec/c_January27AffactPerfect.mov"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer.view setFrame:CGRectMake(0, 0, 500, 260)];
[self.view addSubview:moviePlayer.view];
//Some addiontal customization
moviePlayer.fullscreen = YES;
moviePlayer.allowsAirPlay = YES;
moviePlayer.shouldAutoplay = YES;
moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
//moviePlayer.endPlaybackTime = NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playMovieFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
The selector does what I would like it to do.
-(void)playMovieFinished:(NSNotification*)theNotification
{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// Change Segue ID Here
[self performSegueWithIdentifier:#"VC_5" sender:self];
}
My problem occurs when I click next button while the video is still playing. When my next ViewController appears there is no video and I can still hear the audio from the previous ViewController.
My question is:
How do I properly dismiss the View Controller and the MPMoviePlayer.
I am lost.
I downloaded the example MoviePlayer project from Apple, but it is now deprecated and it does not work properly.
I am lost on how to make this work.
I am crossing my fingers that I have asked the question correctly. Please have mercy on my novice question. Thank you robert
use this code
-(void)playMovieFinished:(NSNotification*)theNotification
{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer stop];
// Change Segue ID Here
[self performSegueWithIdentifier:#"VC_5" sender:self];
}
I'm trying to add a video that I pull from my server do play in my app.
The video plays now, but I don't have any UI controls for volume. fullscreen etc.
Can any one help? Here is my code
NSURL *url = [NSURL URLWithString:#"http://www.gzerodesign.com/sharksclips/video.mp4"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
// Use the new 3.2 style API
moviePlayer.controlStyle = MPMovieControlModeDefault;
moviePlayer.shouldAutoplay = YES;
[[moviePlayer view] setFrame:[[self view] bounds]];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer play];
} else {
// Use the old 2.0 style API
moviePlayer.movieControlMode = MPMovieControlModeHidden;
[moviePlayer play];
}
I only have experience the New 3.2 style API.
moviePlayer.controlStyle = MPMovieControlModeDefault;
comment out
// moviePlayer.controlStyle = MPMovieControlModeDefault;
you may be have fullscreen button and skip button.
but not show volume.
I did not try but, I search this.
MPMoviePlayerViewController not showing volume slider?
Delete this line in your code:
moviePlayer.controlStyle = MPMovieControlModeDefault;
& also this line:
moviePlayer.movieControlMode = MPMovieControlModeHidden;
& then check.
I'm trying to code a video into my iPhone app and the code below works perfectly but the only thing is that I rather the video not automatically start playing when the user opens the page. I want them to be able to push the play button. The code below is located in my viewcontroller.m file.
(void)viewDidLoad
{
NSString *url = [[NSBundle mainBundle] pathForResource:#"77860_00_01_MM02_welcome"
ofType:#"mov"];
player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
player.view.frame = CGRectMake(10, 235, 150, 125);
[self.view addSubview:player.view];
[player play];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *moviePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[player play];
}
Never mind, I figured it out. I just added [player prepareToPlay] under player.shouldAutoplay = NO
Don't put it in your viewDidLoad. Just create an IBAction them link it up to your trigger button.
My Video works when I click my play button, and I know the code for my view change works. I just can't seem to get the view to change after my video is done playing, not sure what is going wrong. Any ideas to get the view to change once the video is done playing?
My code to play movie
-(IBAction)playMovie:(id)sender {
NSString *movieUrl = [[NSBundle mainBundle] pathForResource:#"Movie_1666" ofType:#"m4v"];
playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:movieUrl]];
//Smoothe Transition
//[self presentMoviePlayerViewControllerAnimated:playerController];
//Instant Transistion
[self presentModalViewController:playerController animated:NO];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
[playerController.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerController]; }
My method to change views
- (void)playbackFinished:(NSNotification*) notification {
MPMoviePlayerController *playerController = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerController];
View2 *view2 = [[View2 alloc] initWithNibName:#"View2" bundle:nil];
View2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:view2 animated:YES]; }
EDIT:
My Notification object was wrong and wasn't triggering my playbackFinished method
this change fixes that.
- (void)playbackFinished:(NSNotification*) notification {
playerController = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerController];
I also put this in my header file to make it global for use in my playbackFinished method
MPMoviePlayerViewController *playerController;
Once the movie play finished, dismiss the playerController view without animation. Also check, whether you are presenting the View2 in main thread.
Edit:
Dismiss the playerController by
[playerController dismissModalViewControllerAnimated:NO];
I have to integrate a video into my iphone app.The difference from all the code that there is on the net is that I have to show the video and give to the user the chance to play it.Not to play it automatically.
And here is what I have done:
- (void) play
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"video1" ofType:#"mp4"];
NSURL *fileUrl1 = [NSURL fileURLWithPath:path];
videoPlayer1 = [[MPMoviePlayerController alloc] initWithContentURL:fileUrl1];
[videoPlayer1 setControlStyle:MPMovieControlStyleNone];
[[videoPlayer1 view] setFrame:[self.view bounds]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
//[videoPlayer1 play];
[self.view addSubview:[videoPlayer1 view]];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[videoPlayer1.view removeFromSuperview];
[videoPlayer1 stop];
[videoPlayer1 release];
videoPlayer1 = nil;
}
But the screen just turn black and nothing else shown.
Unless I do [videoPlayer1 play] nothing happens.But I don't want to play the video automatically...it must be played by the user.
So, how should I act in order to show up the video(!not the black screen) and play it when the user wishes?Thanks
You are making an error and here it is
Change this
[videoPlayer1 setControlStyle:MPMovieControlStyleNone];
to
[videoPlayer1 setControlStyle:MPMovieControlStyleDefault];
You are hiding the controls by using that line of yours.
Cheers!!!