iOS如何通过URL播放视频

时间:2011-07-23 17:02:34

标签: url video

我想通过网址播放视频。我看到一些样本,代码如下:

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

但没有什么......为什么......以及如何通过网址播放视频?

3 个答案:

答案 0 :(得分:17)

在下面的代码中,我正在通过互联网从位于Web服务器上的电影文件播放视频。别忘了添加MediaPlayer框架并在ViewController.h文件中包含“MediaPlayer / MediaPlayer.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];
               }
      }

答案 1 :(得分:14)

使用以下代码:

- (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

    import

答案 2 :(得分:12)

尝试

NSURL *url = [NSURL URLWithString: strurl];