2013-12-08 46 views
1

我想在我的网站上播放mp4视频。MPMoviePlayerViewController不是从url播放视频

MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://mywebsite.com/video.mp4"]]; 
    moviePlayer.view.frame = CGRectMake(0, 0, 500, 500); 
    moviePlayer.moviePlayer.shouldAutoplay=YES; 


    moviePlayer.moviePlayer.movieSourceType= MPMovieSourceTypeFile; 
    [moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleDefault]; 

    [self.view addSubview:moviePlayer.view]; 

    [moviePlayer.moviePlayer play]; 

它不播放视频,并给出了这样的错误:

2013-12-08 22:31:00.497 UIWebViewVideoArge[684:60b] _itemFailedToPlayToEnd: { 
    kind = 1; 
    new = 2; 
    old = 0; 
} 
+0

当您通过网络浏览器直接加载视频时是否可以正常工作? – royherma

+0

是的,它的工作原理... – onivi

+0

请参阅http://stackoverflow.com/questions/22068681/itemfailedtoplaytoend-error-when-playing-video-in-mpmovieplayercontroller –

回答

2

地址在你的代码:http://mywebsite.com/video.mp4 - 无效。这个URL播放器会产生一个类似于你的问题的错误。但是,如果您尝试使用另一个网址(例如:http://nordenmovil.com/urrea/InstalaciondelavaboURREAbaja.mp4 - 将其用于测试),您的播放器将无误地运行,但不正确。为了正确工作,你必须改变你的代码:

添加到您的.h文件@interface部分财产(不要忘记加):

@property (nonatomic, strong) MPMoviePlayerViewController *moviePlayer; 

,改变像下面的代码:

_moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://nordenmovil.com/urrea/InstalaciondelavaboURREAbaja.mp4"]]; 
    _moviePlayer.view.frame = CGRectMake(0, 0, 500, 500); 
    _moviePlayer.moviePlayer.shouldAutoplay=YES; 


    _moviePlayer.moviePlayer.movieSourceType= MPMovieSourceTypeFile; 
    [_moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleDefault]; 

    [self.view addSubview:_moviePlayer.view]; 

    [_moviePlayer.moviePlayer play]; 

作为替代方案,您可以使用UIWebView播放视频:

NSURL *fileURL = [NSURL URLWithString:@"http://nordenmovil.com/urrea/InstalaciondelavaboURREAbaja.mp4"]; 
NSURLRequest* requestUrl = [[NSURLRequest alloc] initWithURL:fileURL]; 
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)]; 
[self.view addSubview:webView]; 
[webView loadRequest:requestUrl]; 

如果您将使用UIWebView播放视频 - 请尝试使用您的网址,您将看到您的链接错误。

+0

我可以播放youtbue网址吗? –

+0

@MANCHIKANTIKRISHNAKISHORE不,'MPMoviePlayer'不能播放来自直接链接的视频(可能不支持视频格式)。但是,如果您使用'UIWebView',则可以使用像'https://www.youtube.com/watch?v=jyP58SorNUI'这样的链接来显示带有嵌入式播放器的网页(直接链接不起作用)。 –

相关问题