【问题标题】:Why do I get this output when using AVPlayer in Swift?为什么我在 Swift 中使用 AVPlayer 时会得到这个输出?
【发布时间】:2017-05-22 03:38:06
【问题描述】:

我正在尝试在 swift 中使用 avplayer 播放音频文件,当我播放结合两个文件生成的文件时,我得到了这个输出

 playing file:"file location".m4a -- file:///

但是,当我播放另一个重制的声音文件时,它可以正常播放,并且播放后我没有在输出中得到 --file:///

这就是我播放音频的方式

func play(url:NSURL) {
do {
    soundPlayer = AVPlayer(url: url as URL)
    soundPlayer.volume = 1.0
    soundPlayer.play()
} catch let error as NSError {
    print(error.localizedDescription)
} catch {
    print("failed")
}

}

这就是我用来连接两个音频文件的方法

  func makeSounds(sounds: [NSURL], preName: String){
   let composition = AVMutableComposition()
    print(sounds)

    for sound in sounds {
    let compositionAudioTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
    let avAsset = AVURLAsset(url: sound as URL)
    let track =  avAsset.tracks(withMediaType: AVMediaTypeAudio)[0]
    let timeRange = CMTimeRange(start: CMTimeMake(0, 600), duration: track.timeRange.duration)
    try! compositionAudioTrack.insertTimeRange(timeRange, of: track, at: composition.duration)

    }


    let documentDirectoryURL = NSURL(fileURLWithPath: Urls.user)
    var fileDestinationUrl = documentDirectoryURL.appendingPathComponent("\(SoundData.Name)\(preName).m4a")


    let assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
    assetExport?.outputFileType = AVFileTypeAppleM4A
    assetExport?.outputURL = fileDestinationUrl


    assetExport?.exportAsynchronously(completionHandler:
        {
            switch assetExport!.status
            {
            case AVAssetExportSessionStatus.failed:
                print("failed \(assetExport?.error)")
            case AVAssetExportSessionStatus.cancelled:
                print("cancelled \(assetExport?.error)")
            case AVAssetExportSessionStatus.unknown:
                print("unknown\(assetExport?.error)")
            case AVAssetExportSessionStatus.waiting:
                print("waiting\(assetExport?.error)")
            case AVAssetExportSessionStatus.exporting:
                print("exporting\(assetExport?.error)")
            default:
                soundsToPlay.soundLocation = String(describing: fileDestinationUrl!)
                print("Audio Concatenation Complete")
            }
    })



}

音频文件的位置对我设置要导出到的 url 来说是正确的,但它不播放声音文件,我只是得到那个错误

【问题讨论】:

    标签: swift audio avplayer


    【解决方案1】:

    AVAssetExportSession需要'File'格式的路径字符串,一般通过URL.relativeString获取,其中包含'file:///'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 2020-08-30
      • 2022-12-06
      • 2013-02-28
      • 2022-07-24
      • 2019-07-25
      相关资源
      最近更新 更多