无法调用非功能类型的值' [AVAssetTrack]'

时间:2017-11-08 10:49:23

标签: ios swift

当我在目标c项目中导入Swift库时,我遇到了这个问题,但它正在使用swift项目。 这是班级。请查看

  1. 我通过pod导入库。

  2. 然后添加swift桥接文件然后在运行应用程序之后构建然后它给我错误。

  3. 以下是问题 Here is the problem

  4. 代码:

    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)
        }
    }
    

3 个答案:

答案 0 :(得分:2)

xcode 9.3尝试

    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
    }

答案 1 :(得分:1)

我也面临这个问题,并在下面做出改变。

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

这可能对你有用。

答案 2 :(得分:-1)

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