如何在ResearchKit中配置具有多个ORKAudioSteps的ORKOrderedTask?

时间:2016-10-31 19:34:43

标签: ios swift researchkit

过去几天一直在反对某个问题。这就是我想要完成的事情:

我想呈现一个由多个AudioSteps组成的ORKOrderedTask,每一步都显示一个用户会背诵的句子。当然,ORKOrderedTask.audioTask很棒,但是这个预先配置的任务只提供一个音频提示。我希望用户能够录制一个句子,点击下一个,记录下一个,点击下一个等等。

我遇到的问题: 当我尝试用多个ORKAudioSteps实现我自己的OrderedTask时,无论我做什么,该步骤总是报告“TOO LOUD”,波形显示全红条。

相关代码:

var steps = [ORKStep]()

let instructionStep = ORKInstructionStep(identifier: "IntroStep")
instructionStep.title = "Speech Task"
instructionStep.text = "Placeholder"
steps += [instructionStep]

let countdownStep = ORKCountdownStep(identifier: "CountdownStep")
countdownStep.stepDuration = 5
steps += [countdownStep]

let recordingSettings = [
    AVFormatIDKey : kAudioFormatAppleLossless,
    AVNumberOfChannelsKey : 2,
    AVSampleRateKey: 44100.0
] as [String : Any]


for (index, sentence) in sentences.enumerated() {
    let audioStep = ORKAudioStep(identifier: "AudioStep\(index)")
    audioStep.title = sentence
    audioStep.stepDuration = 5
    audioStep.shouldContinueOnFinish = true;
    let config = ORKAudioRecorderConfiguration(identifier: "Recorder\(index)", recorderSettings: recordingSettings)
    audioStep.recorderConfigurations?.append(config)
    steps += [audioStep]
}

return ORKOrderedTask(identifier: "SpeechTask", steps: steps)

// And the viewController creation function elsewhere in the application
func presentTask(task: ORKOrderedTask) {
    let taskViewController = ORKTaskViewController(task: task, taskRun: nil)
    taskViewController.outputDirectory = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory,  .userDomainMask, true)[0] )
    taskViewController.delegate = self
    self.present(taskViewController, animated: true, completion: nil)
}

(句子只是一个句子提示字符串数组)

我的想法: 我怀疑这个错误与我正在处理录制配置或输出目录的方式有关。输出目录在ViewController中分配给此OrderedTask。我在ORKOrderedTask.m中使用了ORKOrderedTask.audioTask作为构建ORKAudioStep的参考,但很明显我正在做一些让记录器不满意的事情。

感谢您的时间。

1 个答案:

答案 0 :(得分:2)

我使用下面的代码解决了这个问题。请注意AVFormatIDKey和recorderConfigurations赋值的UInt转换。

let recordingSettings = [
    AVFormatIDKey : UInt(kAudioFormatAppleLossless),
    AVNumberOfChannelsKey : 2,
    AVSampleRateKey: 44100.0
] as [String : Any]


for (index, sentence) in sentences.enumerated() {
    let countdownStep = ORKCountdownStep(identifier: "CountdownStep\(index)")
    countdownStep.stepDuration = 5
    steps += [countdownStep]

    let audioStep = ORKAudioStep(identifier: "AudioStep\(index)")
    audioStep.title = sentence
    audioStep.stepDuration = 5
    audioStep.shouldContinueOnFinish = false;
    let config = ORKAudioRecorderConfiguration(identifier: "audio\(index)", recorderSettings: recordingSettings)
    audioStep.recorderConfigurations = [config]

    steps += [audioStep]
}