如何在iOS上通过HTTPS从URL播放mp4视频

时间:2014-07-07 12:29:10

标签: ios objective-c cocoa-touch ios7 mpmovieplayercontroller

我正在尝试使用MPMoviePlayerController通过HTTPS在URL上播放mp4格式的视频,但是视频没有播放,我在日志中收到错误:

_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}

有没有办法在iOS上播放这种视频?

这是我的代码:

#import "FirstViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface FirstViewController ()

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;

@end

@implementation FirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self playBtnPressed];
}

-(void)playBtnPressed
{
    NSURL *url=[[NSURL alloc] initWithString:@"https://....mp4"];
    _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=nil;
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    [_moviePlayer stop];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];

    if ([_moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [_moviePlayer.view removeFromSuperview];
    }
}

@end

2 个答案:

答案 0 :(得分:0)

可能是视频宽高比问题尝试设置宽高比

[player setScalingMode:MPMovieScalingModeAspectFit]; player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

或检查视频文件是否在指定的URL位置

答案 1 :(得分:0)

这似乎是一个iOS 7问题。 显然,moviePlayer.movi​​eSourceType = MPMovieSourceTypeStreaming;不再起作用了。 对我来说,用MPMovieSourceTypeFile替换它解决了这个错误。 使用以下代码,它将适合您。确保在播放电影之前声明了通知观察者,如下所示:

[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlayBackDidFinish:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:moviePlayerController.moviePlayer];

        moviePlayerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

        [moviePlayerController.moviePlayer prepareToPlay];

        [self presentMoviePlayerViewControllerAnimated:moviePlayerController];

        [moviePlayerController.moviePlayer play];