Playing Streaming Video With AVPlayer

不问归期 提交于 2019-12-22 04:29:52

问题


How do you play a streaming video with 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)
        }
    }
}

If I run it, the app won't crash, but I get a broken link as shown below.

I have read a few dozen topics here. One of them says that you have to run it with a background thread. Another one says that a URL must end with a file type like mp4. One guy says that you can't with one down vote. By the way, I write code for both macOS and iOS. So I leave the topic for both platforms. Thanks.


回答1:


SWIFT 3.0

You need three properties ,

 var player :AVPlayer?
 var playerItem: AVPlayerItem?
 var avPlayerLayer: AVPlayerLayer?

In view did load or when you wants to start the player call following method

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)

    }

Then implement this method

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;
            }
        }
    }

Objective C

You need three properties ,

@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic,strong) AVPlayerItem * playerItem;
@property (nonatomic, strong )AVPlayerLayer *avPlayerLayer;

In view did load or when you wants to start the player call following method

    -(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];
}

Then implement this method

   - (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;
             }
        }
    }

Try above code and see the magic :) And do all this on main thread.



来源:https://stackoverflow.com/questions/47055042/playing-streaming-video-with-avplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!