ios AVPlayer 不会在 iOS9 中播放来自 url 的视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32865228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AVPlayer wont play video from url in iOS9
提问by Granit
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)
我正在尝试将 AVPlayer 嵌入 UIView 并从 url 播放 mp4 视频文件。问题是我只收到一个黑色的空白视图(见截图)
In previous iOS versions it worked for me, but since upgrading to iOS9 i got this problem.
在以前的 iOS 版本中它对我有用,但是自从升级到 iOS9 后,我遇到了这个问题。
My .h file looks like this:
我的 .h 文件如下所示:
@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?
我在这里错过了什么吗?
Thanks in advance!
提前致谢!
回答by Gowrie Sammandhamoorthy
@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];
}
回答by Syed Ali Salman
The reason video is not playing is only due the tls
version is too old in case of HTTP
please see App Transport Security
视频无法播放的原因只是tls
版本太旧,以防万一HTTP
请参阅App Transport Security
回答by Vikram Parimi
You are missing this line of code in yours
你的代码中缺少这行代码
playerViewController.player = player;
回答by Sujoy Singh
Make the AVPlayerViewController as the property of the view controller presenting it and then allocate it inside viewDidLoad.
将 AVPlayerViewController 作为呈现它的视图控制器的属性,然后在 viewDidLoad 中分配它。
回答by Shahul Hasan
If you want to play video from one url then you don't need to use AVURLAsset. You should use AVURLAsset when you have number of videos or urls. (But both ways work)
如果你想从一个 url 播放视频,那么你不需要使用 AVURLAsset。当您有多个视频或网址时,您应该使用 AVURLAsset。(但两种方式都有效)
参考:https: //developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAsset_Class/index.html#//apple_ref/occ/cl/AVAsset
Following method works fine, if you have one 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];