Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] AVAsset tracks is sometimes empty #60

Closed
KasemJaffer opened this issue Sep 26, 2019 · 0 comments
Closed

[Bug] AVAsset tracks is sometimes empty #60

KasemJaffer opened this issue Sep 26, 2019 · 0 comments
Labels
bug Something isn't working

Comments

@KasemJaffer
Copy link

Description

asset.tracks(withMediaType: AVMediaType.video).first! is sometimes crashing the app. Simply initializing the AVAsset is not enough for the asset to be ready to access its tracks.

Platform

IOS

Expected solution

AVAsset needs to be loaded first before accessing any of its tracks.

Here is an example of how to fix it.

    public func getTrack(_ asset: AVURLAsset)->AVAssetTrack? {
        var track : AVAssetTrack? = nil
        let group = DispatchGroup()
        group.enter()
        asset.loadValuesAsynchronously(forKeys: ["tracks"], completionHandler: {
            var error: NSError? = nil;
            let status = asset.statusOfValue(forKey: "tracks", error: &error)
            if (status == .loaded) {
                track = asset.tracks(withMediaType: AVMediaType.video).first
            }
            group.leave()
        })
        group.wait()
        return track
    }
@KasemJaffer KasemJaffer added the bug Something isn't working label Sep 26, 2019
@KasemJaffer KasemJaffer changed the title AVAsset tracks is sometimes empty [Bug] [Bug] AVAsset tracks is sometimes empty Sep 26, 2019
Martibis added a commit to Martibis/VideoCompress that referenced this issue Aug 3, 2020
@rurico rurico closed this as completed in e8dca46 Sep 9, 2020
rurico added a commit that referenced this issue Sep 9, 2020
Load asset before getting its tracks (fixes #60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant