Как получить URL измененного аудио файла? - PullRequest
0 голосов
/ 11 июня 2019

Есть идеи, как я могу получить измененный (переданный) URL-адрес, чтобы отправить его на сервер?На данный момент я могу играть только сразу после записи, но я хочу как-то получить его и отправить уже измененный на сервер.

Alamofire.upload(multipartFormData: { (multiPartFormData) in
            do {
                let soundfile = try Data.init(contentsOf: self.soundFileURL)
                self.setupAudio(url: self.soundFileURL)
                multiPartFormData.append(soundfile, withName: "message", fileName: "audio.wav", mimeType: "application/octet-stream")
            } catch {
                print("error")
            }
        }


    func setupAudio(url: URL) {
        // initialize (recording) audio file
        do {
            audioFile = try AVAudioFile(forReading: url)
        } catch {
            showAlert("alert", message: String(describing: error))
        }
    }

У меня есть эта функция playSound, которая изменяет soundFile ...Я могу получить модифицированный файл и отправить его на сервер

private func playSound(rate: Float? = nil, pitch: Float? = nil, echo: Bool = false, reverb: Bool = false) {

        // initialize audio engine components
        audioEngine = AVAudioEngine()

        // node for playing audio
        audioPlayerNode = AVAudioPlayerNode()
        audioEngine.attach(audioPlayerNode)

        // node for adjusting rate/pitch
        let changeRatePitchNode = AVAudioUnitTimePitch()
        if let pitch = pitch {
            changeRatePitchNode.pitch = pitch
        }
        if let rate = rate {
            changeRatePitchNode.rate = rate
        }
        audioEngine.attach(changeRatePitchNode)

        // node for echo
        let echoNode = AVAudioUnitDistortion()
        echoNode.loadFactoryPreset(.multiEcho1)
        audioEngine.attach(echoNode)

        // node for reverb
        let reverbNode = AVAudioUnitReverb()
        reverbNode.loadFactoryPreset(.cathedral)
        reverbNode.wetDryMix = 50
        audioEngine.attach(reverbNode)

        // connect nodes
        if echo == true && reverb == true {
            connectAudioNodes(audioPlayerNode, changeRatePitchNode, echoNode, reverbNode, audioEngine.outputNode)
        } else if echo == true {
            connectAudioNodes(audioPlayerNode, changeRatePitchNode, echoNode, audioEngine.outputNode)
        } else if reverb == true {
            connectAudioNodes(audioPlayerNode, changeRatePitchNode, reverbNode, audioEngine.outputNode)
        } else {
            connectAudioNodes(audioPlayerNode, changeRatePitchNode, audioEngine.outputNode)
        }

        // schedule to play and start the engine!
        audioPlayerNode.stop()
        audioPlayerNode.scheduleFile(audioFile, at: nil) {

            var delayInSeconds: Double = 0

            if let lastRenderTime = self.audioPlayerNode.lastRenderTime, let playerTime = self.audioPlayerNode.playerTime(forNodeTime: lastRenderTime) {

                if let rate = rate {
                    delayInSeconds = Double(self.audioFile.length - playerTime.sampleTime) / Double(self.audioFile.processingFormat.sampleRate) / Double(rate)
                } else {
                    delayInSeconds = Double(self.audioFile.length - playerTime.sampleTime) / Double(self.audioFile.processingFormat.sampleRate)
                }
            }

            // schedule a stop timer for when audio finishes playing
            self.playSoundTimer = Timer(timeInterval: delayInSeconds, target: self, selector: #selector(self.stopAudio), userInfo: nil, repeats: false)
            RunLoop.main.add(self.playSoundTimer!, forMode: .defaultRunLoopMode)
        }

        do {
            try audioEngine.start()
        } catch {
            showAlert("alert", message: String(describing: error))
            return
        }

        // play the recording!
        audioPlayerNode.play()
    }
...