我试图在UIView中嵌入一个AVPlayer并从网址播放mp4视频文件.问题是我只收到一个黑色的空白视图(见截图)
在以前的iOS版本中,它对我有用,但自从升级到iOS9后我遇到了这个问题.
我的.h文件如下所示:
@interface ViewController : UIViewController @property (strong,nonatomic) IBOutlet UIView *viewPlayerContainer;
而在我的实现文件中,我有以下内容:
@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,_viewPlayerContainer.bounds.size.width,_viewPlayerContainer.bounds.size.width)]; playerViewController.showsPlaybackControls = NO; [_viewPlayerContainer addSubview:playerViewController.view]; [player play]; }
我错过了什么吗?
提前致谢!
解决方法
@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,self.view.bounds.size.width,self.view.bounds.size.width)]; playerViewController.showsPlaybackControls = YES; [self.view addSubview:playerViewController.view]; [player play]; }