gpt4 book ai didi

ios - 录音看起来没问题,但播放却不行

转载 作者:行者123 更新时间:2023-11-30 14:00:49 28 4
gpt4 key购买 nike

以下代码应该录制音频然后播放。录制时可能一切正常,但无法播放。有没有人遇到同样的问题,或者有人知道我可以做什么来修复此代码?

var filename="/audioFile.caf"
var soundRecorder:AVAudioRecorder!
var soundPlayer:AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
self.setupRecorder() }



func setupRecorder() {
let recordSettings = [AVSampleRateKey : NSNumber(float: Float(44100.0)),
AVFormatIDKey : NSNumber(int: Int32(kAudioFormatMPEG4AAC)),
AVNumberOfChannelsKey : NSNumber(int: 1),
AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Medium.rawValue)),
AVEncoderBitRateKey : NSNumber(int: Int32(320000))]
var error:NSError?
do {
soundRecorder=try AVAudioRecorder(URL: getFileURL(), settings: recordSettings)
soundRecorder.delegate=self
soundRecorder.prepareToRecord()
}
catch {
}
}
func getCacheDictionary() -> String {
let paths=NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
return paths[0]
}
func getFileURL() -> NSURL {
let path=getCacheDictionary().stringByAppendingString(filename)
let filePath=NSURL(fileURLWithPath: path)
return filePath
}
@IBAction func recordingButton(sender: UIButton) {
soundRecorder.record()
}

@IBAction func Play(sender: AnyObject) {
do {
soundPlayer=try AVAudioPlayer(contentsOfURL: getFileURL())
soundPlayer.delegate=self
soundPlayer.prepareToPlay()
soundPlayer.play()
}
catch {


}
}
@IBAction func PauseRecording(sender: AnyObject) {
soundRecorder.pause()
}

最佳答案

播放前您必须停止录音机(关闭声音文件)。

@IBAction func Play(sender: AnyObject)中,将其添加到顶部:

soundRecoreder.stop()

这是来自 documentationstop 方法的抽象。 :

Stops recording and closes the audio file.

关于ios - 录音看起来没问题,但播放却不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33017591/

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