how iOS play the video by URL

前端 未结 3 788
春和景丽
春和景丽 2020-12-28 10:49

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

NSString *movieFile= [[NSBundle mainBundle] pathForResource:@\"android\" ofType:@\         


        
3条回答
  •  难免孤独
    2020-12-28 11:11

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

提交回复
热议问题