2015-09-30 43 views
7

我想在UIView中嵌入一个AVPlayer并从一个url播放mp4视频文件。问题是,我只收到一个黑色的空白视图(见截图)AVPlayer不会在iOS9上播放来自url的视频

enter image description here

在它的工作对我以前的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]; 


} 

我失去了一些东西?

提前致谢!

+5

可能应用的运输保障。尝试通过“https:”加载视频和/或查看http://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled- in-ios-9 – ChrisH

+1

我已经试过但没有成功 – Granit

+0

任何东西都会在您的调试器中记录下来吗? –

回答

13
@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]; 
} 
1

原因视频无法播放仅仅是由于该tls版本太旧在HTTP情况请参见App Transport Security

+0

我已经尝试添加必要的密钥到我的info.plist,但没有成功 – Granit

1

你缺少这行代码在你

playerViewController.player = player; 
+0

添加了这行代码仍然是空白。 – shri

0

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

0

如果你想从一个网址播放视频,那么你不需要使用AVURLAsset。当你有一些视频或网址时,你应该使用AVURLAsset。 (但两者的工作方式)

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

以下方法工作得很好,如果你有一个网址。

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];