2016-08-30 4 views
1

많은 게시물을 읽었으며 다양한 접근 방식을 시도했으며 최종적으로 다른 스택 오버 플로우 게시물의 휴지통 기능을 실제로 작동시키는 해결책을 제시하여 효과를 추가하고 파일에 저장합니다 추가 된 효과를 재생하고 저장하지만 문제는 저장된 caf입니다. 파일을 읽을 수 없습니다. 나는 그것이 메인 디렉토리에 파일을 생성하지만 그것을 재생할 수 없다는 것을 알았다. 문제를 일으키는 원인이 무엇인지 크게 알게 될 것입니다.추가 된 효과가있는 오디오 녹음

func playAudio(pitch : Float, rate: Float, reverb: Float, echo: Float) { 
    // Initialize variables 
    audioEngine = AVAudioEngine() 
    audioPlayerNode = AVAudioPlayerNode() 
    audioEngine.attachNode(audioPlayerNode) 

    // Setting the pitch 
    let pitchEffect = AVAudioUnitTimePitch() 
    pitchEffect.pitch = pitch 
    audioEngine.attachNode(pitchEffect) 

    // Setting the platback-rate 
    let playbackRateEffect = AVAudioUnitVarispeed() 
    playbackRateEffect.rate = rate 
    audioEngine.attachNode(playbackRateEffect) 

    // Setting the reverb effect 
    let reverbEffect = AVAudioUnitReverb() 
    reverbEffect.loadFactoryPreset(AVAudioUnitReverbPreset.Cathedral) 
    reverbEffect.wetDryMix = reverb 
    audioEngine.attachNode(reverbEffect) 

    // Setting the echo effect on a specific interval 
    let echoEffect = AVAudioUnitDelay() 
    echoEffect.delayTime = NSTimeInterval(echo) 
    audioEngine.attachNode(echoEffect) 

    // Chain all these up, ending with the output 
    audioEngine.connect(audioPlayerNode, to: playbackRateEffect, format: nil) 
    audioEngine.connect(playbackRateEffect, to: pitchEffect, format: nil) 
    audioEngine.connect(pitchEffect, to: reverbEffect, format: nil) 
    audioEngine.connect(reverbEffect, to: echoEffect, format: nil) 
    audioEngine.connect(echoEffect, to: audioEngine.mainMixerNode, format: nil) 


    // Good practice to stop before starting 
    audioPlayerNode.stop() 

    // Play the audio file 
    if(audioEngine != nil){ 
     audioEngine?.stop() 
    } 

    audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: { 
     print("Complete") 

    }) 

    try! audioEngine.start() 


    let dirPaths: AnyObject = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] 
    let tmpFileUrl: NSURL = NSURL.fileURLWithPath(dirPaths.stringByAppendingPathComponent("dddeffefsdctedSoundf23f13.caf")) 
    filteredOutputURL = tmpFileUrl 

    do{ 
     print(dirPaths) 
     print(tmpFileUrl) 

     self.newAudio = try! AVAudioFile(forWriting: tmpFileUrl, settings:[ 
      AVFormatIDKey: NSNumber(unsignedInt:kAudioFormatAppleLossless), 
      AVEncoderAudioQualityKey : AVAudioQuality.Medium.rawValue, 
      AVEncoderBitRateKey : 12800, 
      AVNumberOfChannelsKey: 2, 
      AVSampleRateKey : 44100.0 
      ]) 

     audioEngine.mainMixerNode.installTapOnBus(0, bufferSize: 2048, format: audioEngine.mainMixerNode.inputFormatForBus(0)) { 
      (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in 

      print(self.newAudio.length) 
      print("=====================") 
      print(self.audioFile.length) 
      print("**************************") 
      if (self.newAudio.length) < (self.audioFile.length){//Let us know when to stop saving the file, otherwise saving infinitely 

       do{ 
        //print(buffer) 
        try self.newAudio.writeFromBuffer(buffer) 
       }catch _{ 
        print("Problem Writing Buffer") 
       } 
      }else{ 
       self.audioEngine.mainMixerNode.removeTapOnBus(0)//if we dont remove it, will keep on tapping infinitely 

      } 

     } 
    }catch _{ 
     print("Problem") 
    } 

    audioPlayerNode.play() 

답변

1

caf 파일이 제대로 기록되도록 오디오 파일을 플러시하고 닫아야합니다.

self.audioEngine.mainMixerNode.removeTapOnBus(0) 
       print("finish?") 
self.newAudio = nil // hopefully flush & close, if there are no other strong references 
+0

자기 :

AVAudioFile을 보는 것은 그 일에 대한 명시 적 방법이없는, 유일한 희망은 당신이 마친 후 전무로 newAudio를 설정하고 AVAudioFiledealloc 동안 수행되는 것을 기대하고있는 것으로 보인다 .newAudio = nil // 문제를 해결했습니다. 고마워요. – ivan123

관련 문제