我试图在 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];
}
我在这里错过了什么吗?
提前致谢!
@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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)