【问题标题】:Uploading recording to Firebase with Swift使用 Swift 将录音上传到 Firebase
【发布时间】:2018-07-29 07:33:56
【问题描述】:

我一直在尝试在用户停止录制到 Firebase 后立即上传录音。但除了创建一个名为“audio”的新文件夹之外,它什么也没做。

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

@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)
}

我用于上传到 Firebase 的代码

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
        print("finished recording")


    let storageRef = Storage.storage().reference().child("audio/recordedVoice.wav")

    if let uploadData = AVFileType(self.recordedVoice.wav!) {

        storageRef.put(uploadData, metadata: nil) {(metadata, error) in

            if error != nil {
                print(error)
                return
            }
        }
    }
  }

请帮帮我!

【问题讨论】:

    标签: ios swift firebase avfoundation firebase-storage


    【解决方案1】:

    试试:

     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!")
                }
            }
        }
    

    【讨论】:

    • 我收到错误“'ViewController' 类型的值没有成员 'recordedVoice'”。
    • @Joel 尝试像这样录制您的音频:Another method
    • 好的,我会...谢谢 :) 如果可行,将在此处更新。
    猜你喜欢
    • 2017-10-16
    • 2017-10-14
    • 2018-10-17
    • 2020-12-19
    • 2020-04-12
    • 2017-08-02
    • 1970-01-01
    • 2018-04-09
    • 2016-11-23
    相关资源
    最近更新 更多