How do I get all videos only from library?

678 Views Asked by At

I'm trying to get all videos that are in the camera roll on a user's phone when they try and upload a video, but I'm not sure how to.

I've done this to get all pictures and noticed that if I change the .image to .video it gets all the videos, but they are still presented as an image and you can't play the video:

func fetchImagesFromDeviceLibary() {
    
    let allPhotos = PHAsset.fetchAssets(with: .image, options: getAssetFetchOptions())
    DispatchQueue.global(qos: .background).async {
        //Enumerate objects
        allPhotos.enumerateObjects({ (asset, count, stop) in
            let imageManager = PHImageManager.default()
            let targetSize = CGSize(width: 600, height: 600)
            let options = PHImageRequestOptions()
            options.isSynchronous = true
            
            imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: {
                (image, info) in
                if let image = image {
                    self.videos.append(image)
                    self.assets.append(asset)
                    if self.selectedVideo == nil {
                        self.selectedVideo = image
                    }
                    if count == allPhotos.count - 1 {
                        DispatchQueue.main.async {
                            self.collectionView.reloadData()
                        }
                    }
                }
            })
        })
    }
}

func getAssetFetchOptions() -> PHFetchOptions {
    let options = PHFetchOptions()
    options.fetchLimit = 50
    let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false)
    options.sortDescriptors = [sortDescriptor]
    return options
}

How would I get all the videos and display them on screen so that you can interact with them?

1

There are 1 best solutions below

5
Satish Thakur On

After changing fetchAssets with .image to .video make the required changes in the getAssetsFetchOption().

func getAssetFetchOptions() -> PHFetchOptions {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", 
                                             ascending: false)]


// For Images Only
// fetchOptions.predicate = NSPredicate(format: mediaType == %d",   PHAssetMediaType.image.rawValue)

// For Videos Only
// fetchOptions.predicate = NSPredicate(format: "mediaType == %d,  PHAssetMediaType.video.rawValue)

// For Images and Videos
// fetchOptions.predicate = NSPredicate(format: "mediaType == %d || mediaType == %d", PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue) 

// For Videos with some duration, here I’m taking it as 10 second
fetchOptions.predicate = NSPredicate(format: "mediaType = %d AND duration < 10", PHAssetMediaType.video.rawValue)

fetchOptions.fetchLimit = 50

let imagesAndVideos = PHAsset.fetchAssets(with: fetchOptions)
print(“LIST: \(imagesAndVideos)”)

  return options
}

Hope this will work for you too.

Create AVPlayer Instance :

let videoURL = "your video url"

    // Create an AVPlayer, passing it the local video url path
    let player = AVPlayer(url: videoURL as URL)
    let controller = AVPlayerViewController()
    controller.player = player
    present(controller, animated: true) {
        player.play()
    }

Do not forgot to import AVKit and AVFoundation. Also try to make AvPlayer instance globally.