9

你如何播放流媒体视频AVPlayer

import Cocoa
import AVKit

class StreamViewController: NSViewController {
    var videoPlayer: AVPlayer!

    @IBOutlet weak var moviePlayerView: AVPlayerView!

    override func viewDidLoad() {
        super.viewDidLoad()        
    }

    override func viewWillAppear() {
        super.viewWillAppear()
    }

    override func viewDidAppear() {
        super.viewDidAppear()
        openStream()
    }

    func openStream() {
        completeOpenStream { (done) -> (Void) in
            if done {
                print("done!")
            }
        }
    }

    func completeOpenStream(completionHandler: @escaping (Bool) -> (Void)) -> Void  {
        DispatchQueue.global().async() {
            let urlString = "http://samples.mplayerhq.hu/V-codecs/h264/interlaced_crop.mp4" // You can open it with QuickTime.
            let url = URL(fileURLWithPath: urlString)
            let avAsset = AVURLAsset(url: url)
            let playerItem = AVPlayerItem(asset: avAsset)
            self.videoPlayer = AVPlayer(playerItem: playerItem)
            self.moviePlayerView.player = self.videoPlayer
            completionHandler(true)
        }
    }
}

如果我运行它,应用程序不会崩溃,但我会得到一个断开的链接,如下所示。

在此处输入图像描述

我在这里阅读了几十个主题。其中一个说您必须使用后台线程运行它。另一种说法是 URL 必须以 mp4 之类的文件类型结尾。一个人说你不能一票否决。顺便说一句,我为 macOS 和 iOS 编写代码。因此,我将主题留给两个平台。谢谢。

4

1 回答 1

10

斯威夫特 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;
             }
        }
    }

试试上面的代码,看看它的魔力 :) 并在主线程上做这一切。

于 2017-11-01T13:07:10.320 回答