在iphone中播放视频网址

时间:2012-02-07 11:24:31

标签: iphone ios ios4 xcode4 xcode4.2

如何在Iphone中播放服务器URL视频? 如何使用MPMoviePlayer从网址播放视频?

 (void)loadVideo {

     videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://movies.apple.com/media/us/mac/getamac/2009/apple-mvp-biohazard_suit-us-20090419_480x272.mov"]];
       if(videoUrl) {

        if([videoUrl scheme]) {

            [self playVideoStream:videoUrl];
        }
    }
}

-(void)playVideoStream:(NSURL *)movieFileURL {

MPMovieSourceType movieSourceType = MPMovieSourceTypeUnknown;
/* If we have a streaming url then specify the movie source type. */
if ([[movieFileURL pathExtension] compare:@"mov" options:NSCaseInsensitiveSearch] == NSOrderedSame) 
{
    movieSourceType = MPMovieSourceTypeStreaming;
}
[self createAndPlayMovieForURL:movieFileURL sourceType:movieSourceType]; 
}

-(void)createAndPlayMovieForURL:(NSURL *)movieURL sourceType:(MPMovieSourceType)sourceType {

/* Play the video! */    
[moviePlayer setMovieSourceType:sourceType];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];;
moviePlayer.view.frame = CGRectMake(10, 20, 280, 210);  
[customVideoView addSubview:moviePlayer.view];
[moviePlayer play];
}

3 个答案:

答案 0 :(得分:2)

您需要先设置contentURL属性

moviePlayer.contentURL = urlVideo;

[moviePlayer prepareToPlay];
[moviePlayer play];

答案 1 :(得分:1)

试试这个:

  {

      NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"//name of your video here" ofType:@"//format e.g. mp4, m4v"]];
MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];

playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

[playerController.moviePlayer play];


// When movie finished loading you can have a notification and do extra code
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MovieDidLoad:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];



[playerController release];

playerController = nil;
  }


  -(void)MovieDidLoad:(NSNotification *)notification {

  NSLog(@"logged and notification is %@", notification);

 // extra code if needed


   }

答案 2 :(得分:0)

其中一种简单方法是将该视频网址加载到UIWebview