Search code examples
swiftmpmovieplayer

How set thumbnails image of video player


I am new swift

I am creating video player, it's have background play facility but when i goes background or lock iPhone then play,pause,next,previous controls are there but thmbnails of video playing vide not set ...

if can i set thumbnail of video like thisenter image description here

can i set it if have any method to do it

thanks


Solution

  • For Swift 2, to create an image out of video use this code:

        var thumbImage: UIImage?
        let fileURL = NSURL(fileURLWithPath: filePath)
        let asset = AVAsset(URL: fileURL)
        let assetImgGenerate = AVAssetImageGenerator(asset: asset)
        assetImgGenerate.appliesPreferredTrackTransform = true
        let time = CMTimeMake(asset.duration.value / 3, asset.duration.timescale)
        if let cgImage = try? assetImgGenerate.copyCGImageAtTime(time, actualTime: nil) {
            thumbnailImage = UIImage(CGImage: cgImage)
        }
    

    now you have image. To make it cover you must use MPNowPlayingInfo. Add this piece of code when you update info:

        // works to do to initialise now playing info
        if let artwork = thumbImage {
            nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: artwork)
        } else {
            nowPlayingInfo[MPMediaItemPropertyArtwork] = nil
        }
        MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = nowPlayingInfo