音声録音時のエラーについて
音声を録音(.wavで)するアプリで、録音ボタンを押すと、下記のエラーが出てしまいます。
教えていただける方がいましたら、よろしくお願いいたします。ちなみに、音声の拡張子なのですが、.cafの場合はエラーになりません。しかし今回は.wavで保存したいです。
追記 エラーが出る場所は以下のとおりです。
class ViewController: UIViewController, AVAudioPlayerDelegate, AVAudioRecorderDelegate {
@IBOutlet weak var PlayBTN: UIButton!
@IBOutlet weak var RecordBTN: UIButton!
var soundRecorder : AVAudioRecorder!
var SoundPlayer : AVAudioPlayer!
let fileName = "audioFile.wav"//ここがwavの場合のみエラーがでる。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupRecorder()
}
func setupRecorder(){
let recordSettings = [AVSampleRateKey : NSNumber(float: Float(8000.0)),
AVFormatIDKey : NSNumber(int: Int32(kAudioFormatAppleLossless)),
AVNumberOfChannelsKey : NSNumber(int: 2),
AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Max.rawValue))];
var error: NSError?
do {
// soundRecorder = try AVAudioRecorder(URL: getFileURL(), settings: recordSettings as [NSObject : AnyObject])
soundRecorder = try AVAudioRecorder(URL: getFileURL(), settings: recordSettings)
} catch let error1 as NSError {
error = error1
soundRecorder = nil
}
if let err = error {
print("AVAudioRecorder error: \(err.localizedDescription)")
} else {
soundRecorder.delegate = self
soundRecorder.prepareToRecord()
}
}
func preparePlayer(){
var error : NSError?
do{
SoundPlayer = try AVAudioPlayer(contentsOfURL: getFileURL())
}
catch let error as NSError{
//error = error1
SoundPlayer = nil
}
if let err = error {
print("AVAudioPlayer error: \(err.localizedDescription)")
}
else {
SoundPlayer.delegate = self
SoundPlayer.prepareToPlay()
SoundPlayer.volume = 1.0
}
}
func getCacheDirectory() -> String {
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as [String]
return paths[0]
}
func getFileURL() -> NSURL{
//let path = getCacheDirectory().stringByAppendingPathComponent(fileName)
let path = (getCacheDirectory() as NSString).stringByAppendingPathComponent(fileName)
let filePath = NSURL(fileURLWithPath: path)
//let filePath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(filename))
return filePath
}
@IBAction func Record(sender: UIButton) {
if sender.titleLabel?.text == "Record"{
soundRecorder.record()
//ここでEXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0x0)というエラーが出てしまいます。
sender.setTitle("Stop", forState: .Normal)
PlayBTN.enabled = false
}
else{
soundRecorder.stop()
sender.setTitle("Record", forState: .Normal)
PlayBTN.enabled = false
}
}
}