简体   繁体   English

AVAudioRecorder.record()在iOS 8中始终返回“ false”

[英]AVAudioRecorder.record() always returns “false” in iOS 8

I'm trying to create a practice app using AVAudioRecorder that, upon pressing a button, records 1 second of audio through the iPhone's mic to a file called "Test.wav" and plays it back. 我正在尝试使用AVAudioRecorder创建一个练习应用程序,该应用程序在按下按钮时会通过iPhone的麦克风录制1秒的音频到一个名为“ Test.wav”的文件中,然后进行播放。 However, both the record() and recordForDuration() methods both seem to fail every time I try to execute them. 但是,每次尝试执行它们时,record()和recordForDuration()方法似乎都失败了。 I've already tried setting up an AVAudioSession and making it active, but nothing seems to work. 我已经尝试设置AVAudioSession并将其激活,但是似乎没有任何效果。 Any help would be greatly appreciated. 任何帮助将不胜感激。

import UIKit

import AVFoundation 导入AVFoundation

class ViewController: UIViewController, AVAudioRecorderDelegate { 类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)
}

} }

I had this issue too - turns out it's not actually to do with AVAudioRecorder but actually the way user directories are structured in iOS8. 我也遇到了这个问题-事实证明,它实际上与AVAudioRecorder无关,而实际上与iOS8中用户目录的结构方式有关。 See this question and answer for more info. 有关更多信息,请参见此问题和答案。

Anyway, I fixed it by changing the way the file was accessed to something more like this: 无论如何,我通过将文件的访问方式更改为类似这样的方式来修复它:

let DOCUMENTS_FOLDER: NSString! = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).first as NSString

@IBAction func recordSoundAndPlay() {
    recorder.prepareToRecord()
    println(recorder.recordForDuration(1.0))

    var testSound = SystemSoundID()
    AudioServicesCreateSystemSoundID(NSURL(fileURLWithPath: "\(DOCUMENTS_FOLDER)/Test.wav")), &testSound)
    AudioServicesPlaySystemSound(testSound)
}

Let me know how it goes! 让我知道事情的后续!

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

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