Embedded vimeo video in iPhone app using iOS 6 not playing

大城市里の小女人 提交于 2019-12-03 03:52:40

For this I had to ditch the UIWebView completely.

I don't know if you get this on free vimeo accounts, but on our PRO account, in the video settings there's a tab for 'video file' and at the bottom there's a field labelled 'HTTP Live Streaming' and there's practically a direct link to your video.

And then instead of the UIWebView, I used MPMoviePlayerViewController to play the video.

Add the MediaPlayer.framework into your 'Link Binary With Libraries' setting. In the .h file of my view controller that has a UIImageView, a couple of labels, and a play button, I imported #import <MediaPlayer/MediaPlayer.h> and setup a property @property(nonatomic, readonly) MPMoviePlayerViewController *player;

Then, in the .m file:

-(IBAction)playVideo:(id)sender{
    NSString *videoString = @"http://player.vimeo.com/external/THE_VIDEO_ID.m3u8?p=standard,mobile&s=UNIQUE_VALUE_FOR_EACH_VIDEO";
    player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:videoString]];
    [player.moviePlayer prepareToPlay];
    [player.view setFrame: self.view.bounds];
    [self.view addSubview: player.view];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerDidExitFullscreen:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [player.moviePlayer play];
}

- (void)MPMoviePlayerDidExitFullscreen:(NSNotification *)notification{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [player.moviePlayer stop];
    [player.moviePlayer.view removeFromSuperview];
}

I did try loading the HTTP Live Stream URL into the UIWebView, but it doesn't seem to work. A black screen briefly appears with the vimeo play button in the middle. Then within a second of that the fullscreen player opens for about a second, closes and then the UIWebView goes white.

I'm still testing around with this and so far have managed to play our longest video, 1hr 45min without issue over WiFi on iOS 6 and 5.1. But hopefully this will help others out as a start.

You can still use UIWebview by registering to notifications from UIMoviePlayerController and handling the corresponding events...

-(void)viewDidLoad
{

...

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoStarted:)   name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
}

-(void)videoStarted:(NSNotification *)notification{
// your code here
}

-(void)videoFinished:(NSNotification *)notification{
// your code here
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!