2012-02-07 190 views
1

如何播放服務器URL視頻 in iphone? 如何使用MPMoviePlayer從網址播放視頻?在iphone中播放視頻網址

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

好,但問題在哪裏 – 2012-02-07 11:32:10

回答

2

您需要設置在最前一頁

moviePlayer.contentURL = urlVideo; 

[moviePlayer prepareToPlay]; 
[moviePlayer play]; 
1

contentURL財產試試這個:

{ 

     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 


    } 
+0

如果你需要準備玩這個代碼: – user1194201 2012-02-07 11:48:14

+0

[playerController.movi​​ePlayer prepareToPlay]; – user1194201 2012-02-07 11:48:47

0

其中一個簡單的方法是該視頻網址加載到UIWebview