gpt4 book ai didi

ios - 音频播放有问题

转载 作者:行者123 更新时间:2023-11-28 06:21:23 24 4
gpt4 key购买 nike

项目架构布局:

录制音频 -> 修剪音频 -> 播放修剪后的音频 -> 上传到服务器。

我在播放因 AVAssetExportSession 修剪而创建的音频文件时遇到问题。我怀疑修剪文件的完整性,我已将其上传到服务器并在那里播放正常,但 iOS 拒绝播放。我用修剪文件的 URL 初始化 AVAudioPlayer,然后 play() 没有任何反应,甚至没有抛出错误。

请看下面的代码,是什么导致了这个问题?

  static func outputFileURL() -> URL {

let outputFileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.path.appending("/audioRecord-trimmed.m4a")
return URL(fileURLWithPath: outputFileURL)

}


@IBAction func trimRecording(_ sender: RoundCornerButton) {

//Delete existing recording
deleteEditedRecording()

//Check duration
let duration = CMTimeGetSeconds(recordingToTrim.duration)

if (duration < 5.0) {

let alertController = UIAlertController(title: "Warning", message: "Sound is too short", preferredStyle: UIAlertControllerStyle.alert)

let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)

alertController.addAction(action)

self.present(alertController, animated: true, completion: nil)

return

} else {

let exporter = AVAssetExportSession(asset: recordingToTrim, presetName: AVAssetExportPresetAppleM4A)

exporter?.outputFileType = AVFileTypeAppleM4A

exporter?.outputURL = EditorVC.outputFileURL()

exporter?.timeRange = durationToTrim!

exporter?.exportAsynchronously(completionHandler: {

if exporter?.status == .completed {

let alertController = UIAlertController(title: "Success", message: nil, preferredStyle: UIAlertControllerStyle.alert)

let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)

alertController.addAction(action)

self.present(alertController, animated: true, completion: nil)

} else {

let alertController = UIAlertController(title: "Error", message: exporter?.error?.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)

let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)

alertController.addAction(action)

self.present(alertController, animated: true, completion: nil)

print(exporter?.error?.localizedDescription)

print("Export failed")

return
}

})
}

}

@IBAction func playTrimmedAudio(_ sender: RoundCornerButton) {

print("\nPlay tap\n")

let player = try! AVAudioPlayer(contentsOf: EditorVC.outputFileURL())
player.play()
}

最佳答案

像这样全局声明音频播放器:

var player:AVAudioPlayer!

在你的 playTrimmedAudio 函数中添加这两行代码

player = try! AVAudioPlayer(contentsOf: EditorVC.outputFileURL())
player.play()

关于ios - 音频播放有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43320873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com