使用Swift将记录上传到Firebase

时间:2018-07-29 07:33:56

标签: ios swift firebase avfoundation firebase-storage

我一直在尝试在用户停止录制到Firebase后立即上传录音。但是除了创建一个名为“ audio”的新文件夹外,它没有任何其他作用。

我用于开始和停止录制的代码

float(-32.2630411)

我用于上传到Firebase的代码

@IBAction func recordAudio(_ sender: AnyObject) {
    recordingLabel.text = "Recording in progress"
    stopRecordingButton.isEnabled = true
    recordButton.isEnabled = false

    let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String
    let recordingName = "recordedVoice.wav"
    let pathArray = [dirPath, recordingName]
    let filePath = URL(string: pathArray.joined(separator: "/"))

    let session = AVAudioSession.sharedInstance()
    try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.defaultToSpeaker)

    try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
    audioRecorder.delegate = self
    audioRecorder.isMeteringEnabled = true
    audioRecorder.prepareToRecord()
    audioRecorder.record()
}


@IBAction func stopRecording(_ sender: AnyObject) {
    print("Stop recording button was pressed")
    recordButton.isEnabled = true
    stopRecordingButton.isEnabled = false
    recordingLabel.text = "Tap to Record"
    audioRecorder.stop()
    let audioSession = AVAudioSession.sharedInstance()
    try! audioSession.setActive(false)
}

请帮助我!

1 个答案:

答案 0 :(得分:0)

尝试:

 let audioName = NSUUID().uuidString //You'll get unique audioFile name
 let storageRef = Storage.storage().reference().child("audio").child(audioName)
 let metadata  = StorageMetadata()
 metadata.contentType = "audio/wav"
 if let uploadData = AVFileType(self.recordedVoice.wav!) {

        storageRef.putData(uploadData, metadata: metadata) { (metadata, err) in
            if err != nil {
                //print(err)
                return
            }
            if let _ = metadata?.downloadURL()?.absoluteString {
                print("uploading done!")
            }
        }
    }