ios - 保存录制的音频 (Swift)

标签 ios swift audio save

我正在尝试使用 AVAudioRecorder 录制音频,然后保存它以便以后使用。到目前为止,我已经制作了录音机,它做得很好,录音后播放音频,但一旦我关闭程序,它就消失了。我能做些什么?这是我到目前为止编写的全部代码:

class RecordViewController: UIViewController , AVAudioPlayerDelegate, AVAudioRecorderDelegate {

@IBOutlet weak var RecordButton: UIButton!
@IBOutlet weak var PlayButton: UIButton!


var soundRecorder : AVAudioRecorder!
var soundPlayer : AVAudioPlayer!
var recordedAudio: RecordedAudio!
var fileName = "audioFile.m4a"



override func viewDidLoad() {
    super.viewDidLoad()

    setupRecorder()


    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func setupRecorder() {


    let recordSettings : [String : AnyObject] =
    [
        AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC),
        AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue as NSNumber,
        AVEncoderBitRateKey : 320000 as NSNumber,
        AVNumberOfChannelsKey: 2 as NSNumber,
        AVSampleRateKey : 44100.0 as NSNumber
    ]

    do {

      try   soundRecorder = AVAudioRecorder(URL: getFileURL(), settings: recordSettings)
            soundRecorder.delegate = self
            soundRecorder.prepareToRecord()

    } catch {

        print(error)
    }


}

func getCacheDirectory() -> String {

    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) 

    return paths[0]

}

func getFileURL() -> NSURL{

    let path  = getCacheDirectory().stringByAppendingPathComponent(fileName)

    let filePath = NSURL(fileURLWithPath: path)

    return filePath
}

func preparePlayer() {

    do {
        try soundPlayer = AVAudioPlayer(contentsOfURL: getFileURL())
        soundPlayer.delegate = self
        soundPlayer.prepareToPlay()
        soundPlayer.volume = 1.0
    } catch {
        print(error)
    }

}

@IBAction func Record(sender: UIButton) {


    if sender.titleLabel?.text! == "Record" {

        soundRecorder.record()
        sender.setTitle("Stop", forState: .Normal)
        PlayButton.enabled = false

    } else {

        soundRecorder.stop()
        sender.setTitle("Record", forState: .Normal)
        PlayButton.enabled = false

    }

}
@IBAction func PlaySound(sender: UIButton) {

    if sender.titleLabel?.text! == "Play" {

        RecordButton.enabled = false
        sender.setTitle("Stop", forState: .Normal)

        preparePlayer()
        soundPlayer.play()

    } else {

        soundPlayer.stop()
        sender.setTitle("Play", forState: .Normal)
    }


}



func audioRecorderDidFinishRecording(recorder: AVAudioRecorder, successfully flag: Bool) {
    PlayButton.enabled = true
    recordedAudio = RecordedAudio()
    recordedAudio.filePathUrl = recorder.url
    recordedAudio.title = recorder.url.lastPathComponent
    print(recordedAudio.title)



}
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
    RecordButton.enabled = true
    PlayButton.setTitle("Play", forState: .Normal)
}

最佳答案

我认为应用程序退出时该文件可能仍然存在,问题是您的 viewDidLoad() 方法立即调用 setupRecorder(),这又创建了一个新的 AVAudioRecorder 使用与上次完全相同的文件名 - 覆盖您的工作。

为了帮助您重新安排代码,请转到 audioRecorderDidFinishRecording() 并将 print(recordedAudio.title) 更改为 print(recorder.url)。如果您在 iOS 模拟器中运行,它将为您提供指向 OS X 磁盘驱动器上确切文件名的长路径。

如果您使用 Finder 浏览那里,您将能够看到您的“audioFile.m4a”文件被一次又一次地创建和覆盖,这将让您准确地看到您的问题发生的时间。如果您想查看确切的问题,请在调用 prepareToPlay() 时在代码中设置断点,检查文件大小,然后按 F6 执行该行代码,然后再次检查文件大小– 你应该看到它被清除了:)

解决方案可能是每次都生成一个新文件名。如果你愿意,你可以使用 NSUUID:

let filename = NSUUID().UUIDString + ".m4a"

您可能会找到 my tutorial on AVAudioRecorder有用。

注意:您的 getCacheDirectory() 方法命名不当。它正在获取文档目录,而不是缓存目录,这在尝试调试此类问题时有点转移注意力。

关于ios - 保存录制的音频 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34361991/

相关文章:

scala - 如何在SBT Scala中播放声音文件

ios - 仅在手机打开时收到静默通知

iPhone SDK : How to add an outline to UILabel's text

swift - SwiftyJSON 对象的 JSON 序列化失败

swift - 如何解决 Carthage 和 Swift 版本的错误?

audio - 如何使用 FFMPEG 从没有音频的图像转换 MP4 视频文件?

ios - 错误信息 : [MC] Reading from public effective user settings & [MC] System group container for systemgroup. com.apple.configurationprofiles 路径是

ios - 显示具有将图片放在页面顶部的功能的 pdf

while循环中的Swift非法硬件指令

javascript - 使用javascript从localhost网站播放声音