2

Been bashing my head against an issue for the past few days. Here's what I'm trying to accomplish:

I'd like to present an ORKOrderedTask consisting of multiple AudioSteps, with each step displaying a sentence which the user will recite. Of course, ORKOrderedTask.audioTask is great, but this preconfigured task only gives a single audio prompt. I'd like the user to be able to record a sentence, hit next, record the next one, hit next, etc.

The problem I'm having: When I try to implement my own OrderedTask with multiple ORKAudioSteps, the step always reports "TOO LOUD" no matter what I do, with the waveform displaying full red bars.

The relevant code:

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)
}

(Sentences is simply an array of sentence prompt strings)

My thoughts: I suspect that this error has to do with the way I'm handling the recording configurations or the output directory. The output directory is assigned in the ViewController to which this OrderedTask is given. I've used ORKOrderedTask.audioTask in ORKOrderedTask.m as a reference for building an ORKAudioStep, but clearly I'm doing something that's making the Recorder unhappy.

Thanks for your time.

AdventureBeard
  • 151
  • 2
  • 7

1 Answers1

2

I solved the problem using the code below. Note the UInt conversion for the AVFormatIDKey and the recorderConfigurations assignment.

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]
}
AdventureBeard
  • 151
  • 2
  • 7