繁体   English   中英

如何使用Swift通过用户输入保存音频录制的文件?

[英]How to save audio recorded file by user input using Swift?

我的场景,我正在尝试创建audio record并将文件保存到iPhone documentdirectory

我已完成录制功能,但我需要根据用户输入实现save file名。 之后audio record ,如果用户点击保存按钮,我问的file名用户通过alertviewcontrollertextfield

在这里,我的音频文件由static文件名(audio.m4a)保存,因为在viewdidload我实现了保存文档目录代码,但我不知道如何根据保存操作中的用户输入实现保存文件名。

override func viewDidLoad() {
  super.viewDidLoad() 
  let session = AVAudioSession.sharedInstance()
        try? session.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try? session.overrideOutputAudioPort(.speaker)
        try? session.setActive(true)
        if let basePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
            let baseComponents = [basePath,"audio.m4a"]
            if let audioURL = NSURL.fileURL(withPathComponents: baseComponents) {
                var settings: [String: Any] = [:]
                self.audioURL = audioURL
                settings[AVFormatIDKey] = Int(kAudioFormatMPEG4AAC)
                settings[AVSampleRateKey] = 44100.0
                settings[AVNumberOfChannelsKey] = 2
                audioRecorder = try? AVAudioRecorder(url: audioURL, settings: settings)
                audioRecorder?.prepareToRecord()
            }
        }
}

@IBAction func record_click(_ sender: Any) {
        if let audioRecorder = self.audioRecorder {
            if (audioRecorder.isRecording) {
                audioRecorder.stop()
           } else {
                audioRecorder.record()
            }
        }
    }

// Within below action I am calling alertview with textfield for asking file name
@IBAction func save_click(_ sender: Any) {
   self.savefileAlertView()
}

如果要设置已保存的文件名,可以重命名。

您可以创建此功能

func renameAudio(newTitle: String) {
    do {
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let documentDirectory = URL(fileURLWithPath: path)
        let originPath = documentDirectory.appendingPathComponent("audio.m4a")
        let destinationPath = documentDirectory.appendingPathComponent("\(newTitle).m4a")
        try FileManager.default.moveItem(at: originPath, to: destinationPath)
    } catch {
        print(error)
    }
}

在警报控制器中使用它并作为警报内文本字段的参数传递文本。

import UIKit
import AVFoundation
import Speech

class ViewController: UIViewController,AVSpeechSynthesizerDelegate {

var utterance = AVSpeechUtterance()
let synthesizer = AVSpeechSynthesizer()

var filename : String       = "audio.m4a"

@IBOutlet weak var speechBtnOutlet: UIButton!
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
    super.viewDidLoad()



    utterance = AVSpeechUtterance(string: self.textView.text)
    utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
    utterance.rate = 0.1
    synthesizer.delegate = self
    synthesizer.speak(utterance)

}

func renameAudio(newTitle: String) {
    do {
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let documentDirectory = URL(fileURLWithPath: path)

        let originPath = documentDirectory.appendingPathComponent(utterance.speechString)

        let destinationPath = documentDirectory.appendingPathComponent("\(newTitle).m4a")
        try FileManager.default.moveItem(at: originPath, to: destinationPath)

    } catch {
        print(error)
    }
}



  @IBAction func speechBtn(_ sender: Any) {       
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM