AVPlayer 无法在 iOS9 中播放来自 url 的视频
AVPlayer wont play video from url in iOS9
我正在尝试在 UIView 中嵌入一个 AVPlayer 并从 url 播放一个 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, 0, _viewPlayerContainer.bounds.size.width, _viewPlayerContainer.bounds.size.width)];
playerViewController.showsPlaybackControls = NO;
[_viewPlayerContainer addSubview:playerViewController.view];
[player play];
}
我是不是遗漏了什么?
提前致谢!
视频无法播放的原因仅是 tls
版本太旧 HTTP
请参阅
您的代码中缺少这行代码
playerViewController.player = player;
将 AVPlayerViewController 作为呈现它的视图控制器的 属性,然后在 viewDidLoad 中分配它。
如果你想播放来自一个 url 的视频,那么你不需要使用 AVURLAsset。当您有多个视频或 url 时,您应该使用 AVURLAsset。 (但两种方式都有效)
如果您有 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];
@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];
}
我正在尝试在 UIView 中嵌入一个 AVPlayer 并从 url 播放一个 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, 0, _viewPlayerContainer.bounds.size.width, _viewPlayerContainer.bounds.size.width)];
playerViewController.showsPlaybackControls = NO;
[_viewPlayerContainer addSubview:playerViewController.view];
[player play];
}
我是不是遗漏了什么?
提前致谢!
视频无法播放的原因仅是 tls
版本太旧 HTTP
请参阅
您的代码中缺少这行代码
playerViewController.player = player;
将 AVPlayerViewController 作为呈现它的视图控制器的 属性,然后在 viewDidLoad 中分配它。
如果你想播放来自一个 url 的视频,那么你不需要使用 AVURLAsset。当您有多个视频或 url 时,您应该使用 AVURLAsset。 (但两种方式都有效)
如果您有 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];
@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];
}