how iOS play the video by URL

前端 未结 3 774
春和景丽
春和景丽 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:04

    Use the following code:

    - (IBAction)playBtnPressed {
        NSURL *url = [[NSURL alloc] initWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
        moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDonePressed:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
    
        moviePlayer.controlStyle=MPMovieControlStyleDefault;
        //moviePlayer.shouldAutoplay=NO;
        [moviePlayer play];
        [self.view addSubview:moviePlayer.view];
        [moviePlayer setFullscreen:YES animated:YES];
    }
    
    - (void)moviePlayBackDonePressed:(NSNotification *)notification {
        [moviePlayer stop];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];                               
    
        if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
            [moviePlayer.view removeFromSuperview];    
        }
    
        [moviePlayer release];
        moviePlayer = nil;
    }
    
    - (void)moviePlayBackDidFinish:(NSNotification *)notification {
        [moviePlayer stop];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];                                              
    
        if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
            [moviePlayer.view removeFromSuperview];
        }
    }
    
    • Use following line in .h file and add MediaPlayer Framework in your project

      import

    0 讨论(0)
  • 2020-12-28 11:10

    Try

    NSURL *url = [NSURL URLWithString: strurl];
    
    0 讨论(0)
  • 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];
                   }
          }
    
    0 讨论(0)
提交回复
热议问题