AVPlayer不会在iOS9中播放来自网址的视频 [英] AVPlayer wont play video from url in iOS9
本文介绍了AVPlayer不会在iOS9中播放来自网址的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在UIView中嵌入AVPlayer并从网址播放mp4视频文件。问题是我只收到一个黑色的空白视图(见截图)
I am trying to embed an AVPlayer inside a UIView and play a mp4 video file from an url. The problem is I only receive a black blank view (See screenshot)
在之前的iOS版本中,它对我有用,但自从升级到iOS9后我遇到了这个问题。
In previous iOS versions it worked for me, but since upgrading to iOS9 i got this problem.
我的.h文件如下所示:
My .h file looks like this:
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *viewPlayerContainer;
在我的实施文件中,我有以下内容:
Whereas in my implementation file I have the following:
@import AVFoundation;
@import AVKit;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
NSURL *url = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"];
AVURLAsset *asset = [AVURLAsset assetWithURL: url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
[playerViewController.view setFrame:CGRectMake(0, 0, _viewPlayerContainer.bounds.size.width, _viewPlayerContainer.bounds.size.width)];
playerViewController.showsPlaybackControls = NO;
[_viewPlayerContainer addSubview:playerViewController.view];
[player play];
}
我在这里遗漏了什么吗?
Am i missing something here?
提前致谢!
推荐答案
@implementation ViewController{
AVPlayerViewController *playerViewController;
}
- (void)viewDidLoad {
[super viewDidLoad];
playerViewController = [[AVPlayerViewController alloc] init];
}
- (IBAction)playVideo:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"];
AVURLAsset *asset = [AVURLAsset assetWithURL: url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.player = player;
[playerViewController.view setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width)];
playerViewController.showsPlaybackControls = YES;
[self.view addSubview:playerViewController.view];
[player play];
}
这篇关于AVPlayer不会在iOS9中播放来自网址的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文