Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

ios - How to stream a video with AVURLAsset and save to disk the cached data

Some days ago I was asked to check how difficult is to play a video while downloading it from Internet. I know it's an easy task because someone told me a while ago. So, I checked and it was super easy.

The problem was that I wanted to save to disk the video to do not force the user to download it again and again.

The problem was to access the buffer and store it to disk.

Many answers in Stackoverflow says it is nor possible. Specially with videos.

My original code to play the video:

import AVFoundation

....

//MARK: - Accessors

lazy var player: AVPlayer = {

    var player: AVPlayer = AVPlayer(playerItem: self.playerItem)

    player.actionAtItemEnd = AVPlayerActionAtItemEnd.None

    return player
}()

lazy var playerItem: AVPlayerItem = {

    var playerItem: AVPlayerItem = AVPlayerItem(asset: self.asset)

    return playerItem
}()

lazy var asset: AVURLAsset = {

    var asset: AVURLAsset = AVURLAsset(URL: self.url)

    return asset
}()

lazy var playerLayer: AVPlayerLayer = {

    var playerLayer: AVPlayerLayer = AVPlayerLayer(player: self.player)

    playerLayer.frame = UIScreen.mainScreen().bounds
    playerLayer.backgroundColor = UIColor.clearColor().CGColor

    return playerLayer
}()

var url: NSURL = {

    var url = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")

    return url!
}()

//MARK: - ViewLifeCycle

override func viewDidLoad() {

    super.viewDidLoad()

    view.layer.addSublayer(playerLayer)

    player.play()
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The solution for this problem is to use AVAssetExportSession and AVAssetResourceLoaderDelegate:

First step is to add a notification to know when the video finish. Then we can start saving it to disk.

override func viewDidLoad() {

    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(playerItemDidReachEnd(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)

    ...
}

deinit {

    NSNotificationCenter.defaultCenter().removeObserver(self)
}

The implementation of our function:

func playerItemDidReachEnd(notification: NSNotification) {

    if notification.object as? AVPlayerItem  == player.currentItem {

        let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)

        let filename = "filename.mp4"

        let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last!

        let outputURL = documentsDirectory.URLByAppendingPathComponent(filename)

        exporter?.outputURL = outputURL
        exporter?.outputFileType = AVFileTypeMPEG4

        exporter?.exportAsynchronouslyWithCompletionHandler({

            print(exporter?.status.rawValue)
            print(exporter?.error)
        })
    }
}

Finally we need to make our AVURLAsset delegate of AVAssetResourceLoaderDelegate:

lazy var asset: AVURLAsset = {

    var asset: AVURLAsset = AVURLAsset(URL: self.url)

    asset.resourceLoader.setDelegate(self, queue: dispatch_get_main_queue())

    return asset
}()

And:

extension ViewController : AVAssetResourceLoaderDelegate {

}

I created a small demo with this code in GitHub.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...