视频无法在Objective c中播放

时间:2015-12-15 06:40:24

标签: ios objective-c iphone

我想点击按钮播放视频。我的AVPlayerViewController已打开,但没有播放视频。请帮助。

- (IBAction)btnPlay_Click:(id)sender {
    NSURL *videoURL = [NSURL URLWithString:@"https://player.vimeo.com/video/143506410"];
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    AVPlayerViewController *playerViewController = [AVPlayerViewController new];
    playerViewController.player = player;
    [self presentViewController:playerViewController animated:YES completion:nil];
}

2 个答案:

答案 0 :(得分:1)

如果您使用的是vimeo视频,则可以使用第三方工具来帮助您加载vimeo视频。

解决方案1:try这个,它适用于我,只需要几行代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    vimeoHelper = [[VimeoHelper alloc] init];
    [vimeoHelper getVimeoRedirectUrlWithUrl:@"http://vimeo.com/52760742" delegate:(id)self];
}

- (void)finishedGetVimeoURL:(NSString *)url
{
    _moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
    [self presentViewController:_moviePlayerController animated:NO completion:nil];
}

解决方案2: YTVimeoExtractor

可能会帮助你。

答案 1 :(得分:0)

使用MPMoviePlayerController并添加以下代码:

 NSURL *videoURL = [NSURL URLWithString:@"https://player.vimeo.com/video/143506410"];
 MPMoviePlayerViewController *player =[[MPMoviePlayerViewController alloc] initWithContentURL: videoURL];
 [self.view addSubview: [player view]];
 [self presentMoviePlayerViewControllerAnimated:player];
 [player.moviePlayer prepareToPlay];

再添加一行:[self.view addSubview:[player view]];

  • movieController是.h文件中声明的MPMoviePlayerViewController的一个实例。
  • [movieController.movi​​ePlayer play]不是必需的,无论你是否将自动播放设置为NO,播放器都将启动,但我观察到如果你将播放放入其中它会更快开始。这可能只是巧合。

---------如果这不起作用你就可以这样做----------

player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[player.view setFrame:CGRectMake(0, 0, 320, 320)];
[player play];
[self.view addSubview:player.view];