Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing Streaming Video With AVPlayer

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.

enter image description here

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.

like image 610
El Tomato Avatar asked Nov 01 '17 12:11

El Tomato


People also ask

How do I play videos on IOS?

Play a videoAs you browse photos and videos in the Photos app, tap a video to play it on your iPhone. While it plays, you can do any of the following: Tap the player controls below the video to pause, unmute, favorite, share, delete, or see video information; tap the screen to hide the player controls.

What is Avplayerlayer?

An object that presents the visual contents of a player object.


1 Answers

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.

like image 177
Abuzar Amin Avatar answered Oct 01 '22 18:10

Abuzar Amin