我已经以编程方式实现了avplayer。
@interface showAskUsVideo ()
{
AVURLAsset *asset;
AVPlayerItem *item;
}
@property (strong, nonatomic) AVPlayer *player;
-(void)viewWillAppear:(BOOL)animated
{
[self playVideo];
}
-(void)playVideo {
[playerViewController.view setFrame:CGRectMake(37, 80 ,495,220)];
asset = [AVURLAsset assetWithURL: url];
item = [AVPlayerItem playerItemWithAsset: asset];
_player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.showsPlaybackControls = NO;
playerViewController.player = _player;
[item addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];
[_player addObserver:self forKeyPath:@"rate" options:0 context:nil];
[self.view addSubview:playerViewController.view];
}
但是当我选择部署目标10.0时,它的工作正常。但是对于iOS 8.0到9.3,只能在屏幕上看到PlaybackControls而不是视频。
我的代码需要更改什么?
答案 0 :(得分:0)
playerViewController
是视图控制器层次结构的一部分吗?如果没有,这可以解释它。尝试将其添加为视图控制器的子视图控制器:
[self addChildViewController:playerViewController];
[self.view addSubview:playerViewController.view];
答案 1 :(得分:0)
尝试:添加到.m
@property(非原子,保留)AVPlayer *播放器;
@property(弱,非原子)IBOutlet AVPlayerClass * playerView;
(播放器视图是您要显示它的视图)
NSURL *url = [[NSBundle mainBundle]URLForResource:@"video_name" withExtension:@"mp4"];
_player = [AVPlayer playerWithURL:url];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
playerLayer.frame = _playerView.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[_playerView.layer insertSublayer:playerLayer atIndex:0];
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_player currentItem]];
[_player play];
-(void)playerItemDidReachEnd:(NSNotification *)notification{
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
答案 2 :(得分:0)
如果您为播放器使用ViewController
。最佳做法是presentingViewController
[self presentViewController:playerViewController animated:YES completion:nil];
如果你使用xib
PlayerViewController *vc = [[PlayerViewController alloc]
initWithNibName:@"PlayerViewController" bundle:nil];
[self.view addSubview:[vc view]];