Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play video stream with MPMoviePlayerController in iOS

I am trying to play a video stream from the internet on the iPhone by pressing a button. I used many code samples but nothing worked. With this code it opens a black view without any video stream or controls in it. (The stream itself works.)

NSURL *url = [NSURL URLWithString:@"http://MyStreamURL.com"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayer];

moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
like image 859
derFalke Avatar asked Jan 14 '12 18:01

derFalke


2 Answers

Instead of creating a MPMoviePlayerController and adding that to your view, it is probably simpler to create a MPMoviePlayerViewController and present that view controller modally (since you are trying to show your video full screen anyway). Then the MPMoviePlayerViewController can manage the presentation of your video for you.

MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(moviePlaybackDidFinish:)
                                         name:MPMoviePlayerPlaybackDidFinishNotification
                                       object:nil];    

mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;

[self presentMoviePlayerViewControllerAnimated:mpvc];
[mpvc release];

In your moviePlayBackDidFinish delegate method you can then dismiss the model view controller.

like image 127
jonkroll Avatar answered Oct 18 '22 18:10

jonkroll


Need to mention movie source type as streaming

moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
like image 28
Rajesh Loganathan Avatar answered Oct 18 '22 19:10

Rajesh Loganathan