AVPlayer不会从iOS9的URL播放video

我想embedded一个UIView内的AVPlayer,并从一个URL播放MP4video文件。 问题是我只收到一个黑色的空白视图(见截图)

在这里输入图像说明

在以前的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, 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, 0, self.view.bounds.size.width, self.view.bounds.size.width)]; playerViewController.showsPlaybackControls = YES; [self.view addSubview:playerViewController.view]; [player play]; } 

video不播放的原因只是由于HTTP版本太旧,请参阅App Transport Security

你错过了这行代码在你的

  playerViewController.player = player; 

使AVPlayerViewController作为视图控制器的属性呈现它,然后在viewDidLoad中分配它。

如果你想从一个url播放video,那么你不需要使用AVURLAsset。 当你有一些video或url时,你应该使用AVURLAsset。 (但两种方式工作)

参考: https : //developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAsset_Class/index.html#//apple_ref/occ/cl/AVAsset

以下方法工作正常,如果你有一个url。

 NSURL *url = [NSURL URLWithString:@“<your_url_here>”]; AVPlayer *player = [[AVPlayer alloc] initWithURL: url]; AVPlayerViewController *controller = [[AVPlayerViewController alloc]init]; controller.player = player; controller.delegate = self;// if you want to use delegates... controller.showsPlaybackControls=YES; controller.view.frame = self.view.frame; [self.view addSubview:controller.view]; [player play];