how iOS play the video by URL

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

提交回复
热议问题