簡體   English   中英

無法調用非功能類型“ [AVAssetTrack]”的值

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

當我在目標C項目中導入Swift庫時,我遇到了這個問題,但它與swift項目一起使用。 這是課程。 請查閱

  1. 我通過pod導入庫。

  2. 然后添加快速橋接文件,然后在運行應用程序之后構建,然后給我錯誤。

  3. 這是問題所在 這是問題所在

碼:

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試試

    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
    }

我也面臨着這個問題,並進行如下更改。

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

這可能適合您。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM