简体   繁体   中英

Cannot call value of non-function type '[AVAssetTrack]'

When i am importing Swift library in objective c project then i am facing this problem but its working with swift project. Here is the class. Please review

  1. I import library via pod.

  2. Then add swift bridging file and then build after then run application then its give me error.

  3. Here is the problem 这是问题所在

Code:

final class FDAudioContext {

/// The audio asset URL used to load the context
public let audioURL: URL

/// Total number of samples in loaded asset
public let totalSamples: Int

/// Loaded asset
public let asset: AVAsset

// Loaded assetTrack
public let assetTrack: AVAssetTrack

private init(audioURL: URL, totalSamples: Int, asset: AVAsset, assetTrack: AVAssetTrack) {
    self.audioURL = audioURL
    self.totalSamples = totalSamples
    self.asset = asset
    self.assetTrack = assetTrack
}

public static func load(fromAudioURL audioURL: URL, completionHandler: @escaping (_ audioContext: FDAudioContext?) -> ()) {
    let asset = AVURLAsset(url: audioURL, options: [AVURLAssetPreferPreciseDurationAndTimingKey: NSNumber(value: true as Bool)])

    guard let assetTrack = asset.tracks(withMediaType: AVMediaType.audio).first else {
        NSLog("FDWaveformView failed to load AVAssetTrack")
        completionHandler(nil)
        return
    }

    asset.loadValuesAsynchronously(forKeys: ["duration"]) {
        var error: NSError?
        let status = asset.statusOfValue(forKey: "duration", error: &error)
        switch status {
        case .loaded:
            guard
                let formatDescriptions = assetTrack.formatDescriptions as? [CMAudioFormatDescription],
                let audioFormatDesc = formatDescriptions.first,
                let asbd = CMAudioFormatDescriptionGetStreamBasicDescription(audioFormatDesc)
                else { break }

            let totalSamples = Int((asbd.pointee.mSampleRate) * Float64(asset.duration.value) / Float64(asset.duration.timescale))
            let audioContext = FDAudioContext(audioURL: audioURL, totalSamples: totalSamples, asset: asset, assetTrack: assetTrack)
            completionHandler(audioContext)
            return

        case .failed, .cancelled, .loading, .unknown:
            print("FDWaveformView could not load asset: \(error?.localizedDescription ?? "Unknown error")")
        }
        completionHandler(nil)
    }
}

xcode 9.3 try

    public static func load(fromAudioURL audioURL: URL, completionHandler: @escaping (_ audioContext: FDAudioContext?) -> ()) {
    let asset = AVURLAsset(url: audioURL, options: [AVURLAssetPreferPreciseDurationAndTimingKey: NSNumber(value: true as Bool)])

    /// guard let assetTrack = asset.tracks(withMediaType: AVMediaType.audio).first else {
    guard let assetTrack = asset.tracks(withMediaType: AVMediaTypeAudio).first else {
        NSLog("FDWaveformView failed to load AVAssetTrack")
        completionHandler(nil)
        return
    }

am also facing this issue and change like below.

guard let assetTrack = asset.tracks(withMediaType: AVMediaTypeAudio).first else {
    NSLog("FDWaveformView failed to load AVAssetTrack")
    completionHandler(nil)
    return
}

this may works for you.

guard let assetTrack = asset.tracks.first else {
            NSLog("FDWaveformView failed to load AVAssetTrack")
            completionHandler(nil)
            return
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM