2016-10-31 62 views
0

過去幾天一直在反駁一個問題。以下是我想要實現的:如何在ResearchKit中使用多個ORKAudioSteps配置ORKOrderedTask?

我想介紹一個由多個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) 
} 

(句子簡直是一句提示字符串數組)

我的想法: 我懷疑這個錯誤與我處理的方式做錄製配置或輸出目錄。輸出目錄在給定OrderedTask的ViewController中分配。我已經使用ORKOrderedTask.m中的ORKOrderedTask.audioTask作爲構建ORKAudioStep的參考,但顯然我正在做一些令錄製者不快的事情。

謝謝你的時間。

回答

0

我使用下面的代碼解決了問題。請注意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] 
} 
相關問題