как iOS воспроизводит видео по URL - PullRequest
7 голосов
/ 23 июля 2011

Я хочу воспроизвести видео по URL. Я вижу некоторые примеры, коды, как показано ниже:

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

Он воспроизводит только локальный ресурс. Я пишу код вроде:

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

но ничего нет ... зачем .. и как воспроизводить видео по URL?

Ответы [ 3 ]

17 голосов
/ 19 декабря 2012

В следующем коде я воспроизводю видео через Интернет из файла фильма, расположенного на веб-сервере.Не забудьте добавить каркас MediaPlayer и включить «MediaPlayer / MediaPlayer.h» в файл ViewController.h.

При нажатии кнопки используйте следующий код:

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

Метод уведомления:

    - (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];
               }
      }
14 голосов
/ 06 марта 2013

Используйте следующий код:

- (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];
    }
}
  • Используйте следующую строку в файле .h и добавьте MediaPlayer Framework в свой проект

    импорт

12 голосов
/ 23 июля 2011

Попробуйте

NSURL *url = [NSURL URLWithString: strurl];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...