Titouan de Bailleul
Titouan de Bailleul

Reputation: 12949

Videos in PhotoData are not playable

I'm building an photo/video picker where I fetch all the videos from the phone using the following code

func fetchVideos() -> AnyPublisher<[Video], Never> {
    Future { promise in
        let options = PHFetchOptions()
        options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
            
        var videos = [Video]()
            
        PHAsset.fetchAssets(with: .video, options: options).enumerateObjects { asset, _, _ in
            videos.append(Video(asset: asset))
        }
            
        promise(.success(videos))
    }
    .eraseToAnyPublisher()
}

Once the user taps on a video I fetch the AVAsset from the PHAsset using the code below

func fetchAVAsset(_ completion: @escaping (AVAsset?) -> Void) {
    let options = PHVideoRequestOptions()
    options.isNetworkAccessAllowed = true

    PHImageManager.default().requestAVAsset(forVideo: asset, options: options) { avAsset, _, _ in
        DispatchQueue.main.async {
            completion(avAsset)
        }
    }
}

I notices that the videos are stored in two different places

The videos stored in the former path are working just fine. The videos stored at the latter path are not playable. I'm trying to play them in the AVPlayerViewController but it displays a crossed-out play button and no video. Why?

Upvotes: 2

Views: 363

Answers (1)

Ray
Ray

Reputation: 777

I had the same problem. If I request with youIt seems to be fetching only meta information. When I tried changing deliveryMode and version, I was able to edit and play correctly.

let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
options.deliveryMode = .highQualityFormat
options.isNetworkAccessAllowed = true

    
PHImageManager.default().requestAVAsset(forVideo:self.videoAssets[0], options: options, resultHandler: { (asset, audioMix, info) in
        DispatchQueue.main.async {
            if let urlAsset = asset as? AVURLAsset {
                
             // asset, urlAsset
                
            }
        }
})

Upvotes: 1

Related Questions