Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how iOS play the video by URL

Tags:

url

video

I want to play a video by the URL. I see some sample,the codes like below:

NSString *movieFile= [[NSBundle mainBundle] pathForResource:@"android" ofType:@"mp4"];
videoURL=[[NSURL alloc] initFileURLWithPath:movieFile];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:videoURL];

It play only the local resource.I write some code like:

NSString* strurl =@"https://s3.amazonaws.com/adplayer/colgate.mp4";
videoURL=[NSURL fileURLWithPath:strurl];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:videoURL];

but there is nothing ... why..and how to play video by the url ?

like image 762
Xubing in China. Developer Avatar asked Jul 23 '11 17:07

Xubing in China. Developer


1 Answers

In following code, I am playing a video over the internet from a movie file located on a web server. Dont forget to add MediaPlayer framework and include "MediaPlayer/MediaPlayer.h" in ViewController.h file.

On a button click use following code:

    -(IBAction) playVideo:(id)sender
     {

            NSURL *url=[[NSURL alloc] initWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];

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

Notification method:

    - (void) moviePlayBackDidFinish:(NSNotification*)notification 
      {

               MPMoviePlayerController *player = [notification object];

               [[NSNotificationCenter defaultCenter] removeObserver:self 
name:MPMoviePlayerPlaybackDidFinishNotification object:player];

               if ([player respondsToSelector:@selector(setFullscreen:animated:)])
               {
                        [player.view removeFromSuperview];
               }
      }
like image 130
Himanshu Mahajan Avatar answered Sep 30 '22 18:09

Himanshu Mahajan