2015-09-30 64 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];