2011-07-23 53 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