为什么在Swift中使用AVPlayer时会得到此输出?

时间:2017-05-22 03:38:06

标签: swift audio avplayer

我正试图在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")
            }
    })



}

音频文件位置对于我要设置导出到的网址是正确的,但它不播放声音文件,我只是得到了该错误

1 个答案:

答案 0 :(得分:0)

AVAssetExportSession需要'文件中的路径字符串。格式,通常通过URL.relativeString获得,其中包含' file:///'