2011-07-23 148 views
7

我想通过URL播放视频。我看到一些样品,如下面代码:iOS如何通过URL播放视频

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

它只能播放本地resource.I喜欢写一些代码:

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

但并没有什么... why..and如何通过网址播放视频?

回答

12

尝试

NSURL *url = [NSURL URLWithString: strurl]; 
+0

感谢~~这是OK.thanks为您热情帮助:) –

+1

做到了解决这个问题? – pasine

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

谢谢:) :) – swiftBoy

+0

需要在全局添加MPMoviePlayerController * moviePlayer,否则它将无法工作。 – Alok

+0

MPMoviePlayerController现已弃用.....要替换的是什么? – Simmy

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框架乌尔项目

    进口

+2

这对我有用,但是你需要把它添加到你的.h文件中@property(atomic,strong)MPMoviePlayerController * moviePlayer;'然后在你的.m文件中合成属性'@synthesize moviePlayer;' – justinhartman