我正在尝试使用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)
}
答案 0 :(得分:6)
我认为该应用程序退出时该文件可能仍然存在,问题是您的BE->A
方法会立即调用viewDidLoad()
,而后者会使用完全相同的方式创建新的setupRecorder()
文件名为上次 - 覆盖您的工作。
为帮助您重新安排代码,请转到AVAudioRecorder
并将audioRecorderDidFinishRecording()
更改为print(recordedAudio.title)
。如果您在iOS模拟器中运行,那么将为您提供一条到OS X磁盘驱动器上的确切文件名的长路径。
如果您使用Finder在那里浏览,您将能够看到您的“audioFile.m4a”文件被反复创建和覆盖,这将让您准确了解问题发生的时间。如果您想查看确切的问题,请在调用print(recorder.url)
时在代码中设置断点,检查文件的大小,然后按F6执行该行代码,然后再次检查文件的大小 - 您应该看到它被清除:))
解决方案可能是每次都生成一个新的文件名。如果您愿意,可以使用prepareToPlay()
:
NSUUID
您可能会发现my tutorial on AVAudioRecorder有用。
注意:您的let filename = NSUUID().UUIDString + ".m4a"
方法命名不佳。它正在获取文件目录,而不是缓存目录,这在尝试调试此类问题时有点像红色鲱鱼。