环球银行金融电信协会3.0
你需要三个属性,
var player :AVPlayer?
var playerItem: AVPlayerItem?
var avPlayerLayer: AVPlayerLayer?
在视图中加载或当您想要启动播放器时调用以下方法
func startplayer(){
playerItem = AVPlayerItem.init(url: URL.init(string: "https:yourUrl.com")!)
player = AVPlayer.init(playerItem: playerItem)
player?.allowsExternalPlayback = true
avPlayerLayer = AVPlayerLayer.init(player: player)
avPlayerLayer?.frame = CGRect.init(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)
avPlayerLayer?.videoGravity = .resizeAspect
view.layer.insertSublayer(avPlayerLayer!, at: 0)
player?.currentItem?.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
}
然后实现这个方法
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let currentPlayer = player , let playerObject = object as? AVPlayerItem, playerObject == currentPlayer.currentItem, keyPath == "status"
{
if ( currentPlayer.currentItem!.status == .readyToPlay)
{
currentPlayer.play()
currentPlayer.rate = 1.0;
}
}
}
目标C
你需要三个属性,
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic,strong) AVPlayerItem * playerItem;
@property (nonatomic, strong )AVPlayerLayer *avPlayerLayer;
在视图中加载或当您想要启动播放器时调用以下方法
-(void) startplayer
{
AVPlayerItem * playerItem= [AVPlayerItem playerItemWithURL:@"https:yourUrl.com"];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player.allowsExternalPlayback= YES;
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.avPlayerLayer.frame = CGRectMake(OriginX, OriginY, self.view.frame.size.width, self.view.frame.size.height);
self.avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[self.view.layer insertSublayer:self.avPlayerLayer atIndex:FirstIndex];
[self.player.currentItem addObserver:self
forKeyPath:@"status"
options:0
context:nil];
}
然后实现这个方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == self.player.currentItem && [keyPath isEqualToString:@"status"])
{
if (self.player.currentItem.status == AVPlayerStatusReadyToPlay)
{
[self.player play];
self.player.rate = 1.0;
}
}
}
尝试上面的代码,看看神奇的:)并在主线程上完成所有这些。