2016-10-10 4 views
0

私はオーディオレコーディングフレームワークを作成していますが、正しくコンパイルされています。このフレームワークをプロジェクトで使用すると、記録ファイルはフレームワークで指定されたドキュメントフォルダに作成されますが、サイズは4KBのままで、ファイル内にはオーディオは増えず、オーディオもありません。私は30秒の録音時間を与えました。私はオーディオ録音にAVFoundationを使用しています。私は自分のプロジェクトで直接使用していますが、カスタム作成のフレームワークでコードを呼び出すと同じコードが機能しません。カスタムオーディオレコーダーフレームワークスウィフト3.0

public func startRecording() { 
    do { 
     if (recordingSession != nil) { 
      return 
     } 
     recordingSession = AVAudioSession.sharedInstance() 
     try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord) 
     try recordingSession.setActive(true) 

     recordingSession.requestRecordPermission() { allowed in 
      DispatchQueue.main.async { 
       if allowed { 
        print("allowed") 
        let audioFilename = self.getDocumentsDirectory().appendingPathComponent("recording.caf") 
        print(audioFilename) 
        let settings = [ 
         AVFormatIDKey: Int(kAudioFormatAppleIMA4), 
         AVSampleRateKey: 16000, 
         AVNumberOfChannelsKey: 1, 
         AVLinearPCMBitDepthKey: 16, 
         AVLinearPCMIsBigEndianKey: 0, 
         AVLinearPCMIsFloatKey: 0 
        ] 

        do { 
         self.audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings) 
         self.audioRecorder.delegate = self 
         self.audioRecorder.isMeteringEnabled = true 
         let audioHWAvailable = self.recordingSession.isInputAvailable 
         if !audioHWAvailable 
         { 
          print("no audio input available") 
          return 
         } 
         UIApplication.shared.beginReceivingRemoteControlEvents() 

         self.audioRecorder.record(forDuration: 30) 
         if self.audioRecorder.prepareToRecord() 
         { 
          print("Recording started") 
          self.audioRecorder.record() 
         } 

        } catch { 
         self.finishRecording() 
        } 
       } else { 
        print("failed to record!") 
       } 
      } 
     } 
    } catch { 
     print("failed to record!") 
    } 

} 

このstartRecordingメソッドは、私のプロジェクトから呼び出すフレームワーククラスにあります。

編集:self.audioRecorder.record()行の後にタイマーを追加すると、録音は機能しますが、その理由はわかりません。

答えて

0

最後に、Appleサポートチームの助けを借りて解決策を見つけました。

startRecording()をAudioRecordingクラスのグローバル変数で呼び出すことで、タイマーを追加することなく動作しました。