2016-07-29 4 views
1

사용자의 음성을 녹음하고 텍스트 데이터로 변경하려고합니다. AVAudioRecorder를 사용하여 사운드를 녹음하고 SpeechKit을 사용하여 iOS 10에 포함 된 텍스트로 변경합니다. 사용자가 터치하면 버튼을 누르면 소리가 시작되고 중지됩니다. 그러나 do catch 구문을 사용하여 AVAudioRecorder를 초기화하면 오류가 발생하고 실패합니다. 적절한 프레임 워크 (Speech, AVFoundation)를 추가했습니다. Xcode 8.0 beta3 AVAudioRecorder (url, setting) 오류 catch - "오류 도메인 = NSOSStatusErrorDomain 코드 = -50"(null) ""

import UIKit 
import Speech 
import AVFoundation 

class SearchViewController: UIViewController, CLLocationManagerDelegate, AVAudioRecorderDelegate { 

    var audioRecorder = AVAudioRecorder() 

    let recordSettings = [AVSampleRateKey : String(NSNumber(value: Float(44100.0))), 
         AVFormatIDKey : String(kAudioFileCAFType), 
         AVNumberOfChannelsKey : String(NSNumber(value: 2))] 

@IBAction func recordButtonDown(_ sender: AnyObject) { 

    print("recordButtonDown") 

    self.audioPlayer.play() 
    sleep(1) 

    let fileManager = FileManager.default 
    let paths = fileManager.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask) 
    var audioURL = paths[0] as NSURL 
    audioURL = audioURL.appendingPathComponent("soundForMapSearch.caf", isDirectory: false)! 

    do { 
     self.audioRecorder = try AVAudioRecorder(url: soundFileURL as URL, settings: self.recordSettings) 
     self.audioRecorder.delegate = self 
     self.audioRecorder.prepareToRecord() 
     self.audioRecorder.record() 
    } catch (let error) { 
     print("Error: \(error)") 
    } 
} 

@IBAction func recordButtonUp(_ sender: AnyObject) { 
    self.audioRecorder.stop() 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    do { 
    try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord) 
    try audioSession.setActive(true) 
    audioSession.requestRecordPermission({ (recordPermission) in 
     }) 
    } catch { 
     print("record initiallizing failed") 
    } 

그리고 라인

self.audioRecorder = try AVAudioRecorder(url: soundFileURL as URL, settings: self.recordSettings) 

오류에

가 인쇄됩니다 캐치

Error: Error Domain=NSOSStatusErrorDomain Code=-50 "(null)" 

에서 발생한다. 이 오류를 찾고 코드 = -50은 NSURL 개체가 유효하지 않음을 의미합니다. 이 오류를 어떻게 해결할 수 있습니까?

+0

이이'우리가 알지 못하는 soundFileURL' 대신에 당신의'audioURL'를 사용하지 않나요? – Moritz

답변

1

내 작업 코드

 let recordingName = "recording1" + ".m4a" 
     let pathArray = [dirPath, recordingName] 
     print(pathArray) 
     let filePath = NSURL.fileURL(withPathComponents: pathArray) 
     print(filePath) 


     do{ 


     let session = AVAudioSession.sharedInstance() 
     try! session.setCategory(AVAudioSessionCategoryPlayAndRecord) 


    } catch { 

    assertionFailure("AVAudioSession setup error: \(error)") 
    } 

     let recordSettings: [String: AnyObject] = [ 
      AVFormatIDKey: NSNumber(value: kAudioFormatMPEG4AAC), 
      AVSampleRateKey: 44100.0, 
      AVNumberOfChannelsKey: 1, 
      ] 

     try! audioRecorder = AVAudioRecorder(url: filePath!, settings: recordSettings) 


     audioRecorder.delegate = self 
     audioRecorder.isMeteringEnabled = true 
     audioRecorder.prepareToRecord() 
     audioRecorder.record() 
+0

감사합니다. 내 recordSetting이 잘못되어 수정했습니다. –

+0

나는 recordSettings에 [String : Any]를 사용했습니다. 그것은 내 쟁점이었다. [String : AnyObject]가 적합합니다. –

관련 문제