Salman Ali
Salman Ali

Reputation: 255

Upload audio to Firebase Swift

Hi i am trying to pick an audio file from the iphone and upload it to the Firebase storage.

This is how i am picking the audio file

public func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection){

    let url = mediaItemCollection.items[0].assetURL

    if let u = url?.path{

        if let audioUrl = URL(string: u){
            con?.addNewProduct(name: "Music Test", desc: audioUrl, category: "Horror")
            print("Selected Audio : \(audioUrl)")
        }

    }


    audioSelected = true
    self.dismiss(animated: true, completion: nil)
}

This is the code to upload the audio file to the storage

private func uploadAudio(pRef: FIRDatabaseReference, videoUrl: URL, name: String, category: String){
    if let url : String = pRef.url{
        let storageRef = storage?.reference(withPath: "Products/Audios/\(url).mp3")
        let metaData = FIRStorageMetadata()
        metaData.contentType = "audio/mp3"
        //let movUrl = URL(string: desc)
        let task = storageRef?.putFile(videoUrl, metadata: nil, completion: {
            meta, error in
            if error != nil{
                print("Error uploading File")

            }
        })


        task?.observe(.success, handler: {
            snap in
            switch snap.status{
            case .success:

                pRef.child("name").setValue(name)
                print("Child Added at \(self.dbRef?.child(category).child(name).url)")
                pRef.child("movUrl").setValue(url)
                //pRef?.child("desc").setValue(desc)

                if self.uploadReporter != nil{
                    self.uploadReporter?.imageUploadStatus(status: true)
                }
            case .failure:
                if(self.uploadReporter != nil)
                {
                    self.uploadReporter?.imageUploadStatus(status: false)
                }
                print("Failed")
            // MessageBox.Show(message: "Image Upload failed", title: "Upload Failed", view: nil)
            default:
                print("default")
            }
        })
        task?.observe(.progress, handler: {
            snap in
            if let p = snap.progress?.fractionCompleted{
                if self.uploadReporter != nil{
                    self.uploadReporter?.reportProgress(progress: Float(p))
                }
            }
        })
    }
}

But whenever i try to upload the audio to the Firebase storage, it give me an error.

Here is the error.

2017-05-17 03:52:00.615633+0500 VideoOrganizer[344:23801] CFURLResourceIsReachable failed because it was passed an URL which has no scheme
2017-05-17 03:52:00.621135+0500 VideoOrganizer[344:23801] Body file is unreachable: /item.mp3
Error Domain=NSCocoaErrorDomain Code=4 "The file doesn’t exist."

Please help me through this error. I have been really trying to get rid of this issue but no success. Thanks.

Upvotes: 3

Views: 1826

Answers (1)

Jen Person
Jen Person

Reputation: 7546

Try adding file:// to the front of your path.

Upvotes: -1

Related Questions