如何在ios 6中播放视频

时间:2013-11-14 16:29:58

标签: ios objective-c mpmovieplayercontroller

我需要在我的应用中播放视频我能做什么?感谢。 * 这是我的代码。 *

#import <MediaPlayer/MediaPlayer.h>
 NSURL *fileURL = [NSURL URLWithString:@"http://nordenmovil.com/urrea/InstalaciondelavaboURREAbaja.mp4"];

       MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
        [moviePlayerController.view setFrame:CGRectMake(0, 0, 320, 270)];
        [self.view addSubview:moviePlayerController.view];
        moviePlayerController.fullscreen = YES;
        [moviePlayerController play];

2 个答案:

答案 0 :(得分:1)

由于您尝试从URL播放视频,因此可以使用WebView播放。这是一些示例代码(已测试!):

 NSURL *fileURL = [NSURL URLWithString:@"http://nordenmovil.com/urrea/InstalaciondelavaboURREAbaja.mp4"];

NSURLRequest* requestUrl = [[NSURLRequest alloc] initWithURL:fileURL];

UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 640)];
[self.view addSubview:webView];
[webView loadRequest:requestUrl];

答案 1 :(得分:1)

添加属性:

 @property (nonatomic,strong) MPMoviePlayerController *moviePlayerController;

并将您的代码更改为:

NSURL *fileURL = [NSURL URLWithString:@"http://nordenmovil.com/urrea/InstalaciondelavaboURREAbaja.mp4"];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[_moviePlayerController.view setFrame:CGRectMake(0, 0, 320, 270)];
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
[_moviePlayerController play];

或使用此代码:

NSURL *movieURL = [NSURL URLWithString:@"http://nordenmovil.com/urrea/InstalaciondelavaboURREAbaja.mp4"];
MPMoviePlayerViewController *movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:movieController];
[movieController.moviePlayer play];