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



过去几天一直在绞尽脑汁想一个问题。以下是我想要完成的:

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

我遇到的问题:当我尝试用多个ORKAudioSteps实现我自己的OrderedTask时,无论我做什么,该步骤总是报告"太大声",波形显示完整的红色条。

相关代码:

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。audioTask中的ORKOrderedTask。m作为构建ORKAudioStep的参考,但显然我正在做一些使Recorder不满意的事情。

感谢您的宝贵时间。

使用下面的代码解决了这个问题。注意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]
}

相关内容

  • 没有找到相关文章

最新更新