//
// RecordSoundsViewController.swift
// PitchPerfect
//
// Created by Zzcz on 2017/9/27.
// Copyright © 2017年 fly. All rights reserved.
//
import UIKit
import AVFoundation
class RecordSoundsViewController: UIViewController {
@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var recordingLable: UILabel!
@IBOutlet weak var stopRecordingButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
stopRecordingButton.isEnabled = false
}
var audioRecorder : AVAudioRecorder!
@IBAction func recordAudio(_ sender: Any) {
recordingLable.text = "Recording in Progress"
stopRecordingButton.isEnabled = true
recordButton.isEnabled = false
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let recordingName = "recordedVoive.wav"
let pathArray = [dirPath,recordingName]
let filePaht = URL(string: pathArray.joined(separator: "/"))
let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.defaultToSpeaker)
try! audioRecorder = AVAudioRecorder(url: filePaht!, settings: [:])
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()
}
@IBAction func stopRecording(_ sender: Any) {
recordingLable.text = "Tap to Record"
recordButton.isEnabled = true
stopRecordingButton.isEnabled = false
audioRecorder.stop()
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setActive(false)
}
}
extension RecordSoundsViewController : AVAudioRecorderDelegate {
func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
print("finished recording")
}
}
使用Swift,錄製並保存音頻文件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.