【发布时间】:2014-12-05 13:41:39
【问题描述】:
我正在尝试使用 AVAudioRecorder 创建一个练习应用程序,按下按钮后,它会通过 iPhone 的麦克风将 1 秒的音频录制到一个名为“Test.wav”的文件中并播放。但是,每次我尝试执行它们时,record() 和 recordForDuration() 方法似乎都失败了。我已经尝试设置 AVAudioSession 并使其处于活动状态,但似乎没有任何效果。任何帮助将不胜感激。
import UIKit
导入 AVFoundation
类 ViewController: UIViewController, AVAudioRecorderDelegate {
@IBOutlet var recordAndPlayButton: UIButton!
let settings = [
AVFormatIDKey: kAudioFormatLinearPCM,
AVSampleRateKey: 192000.0,
AVNumberOfChannelsKey: 1,
AVLinearPCMBitDepthKey: 16,
AVLinearPCMIsBigEndianKey: false,
AVLinearPCMIsFloatKey: false,
AVLinearPCMIsNonInterleaved: false
]
var audioSession = AVAudioSession.sharedInstance()
var recorder : AVAudioRecorder!
override func viewDidLoad() {
audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
audioSession.setActive(true, error: nil)
recorder = AVAudioRecorder(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Test", ofType: "wav")!), settings: settings, error: nil)
recorder.delegate = self
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func recordSoundAndPlay() {
recorder.prepareToRecord()
println(recorder.recordForDuration(1.0))
var testSound = SystemSoundID()
AudioServicesCreateSystemSoundID(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Test", ofType: "wav")!), &testSound)
AudioServicesPlaySystemSound(testSound)
}
}
【问题讨论】: