How to upload audio with Alamofire multipart upload?

旧时模样 提交于 2019-11-28 02:18:04

Hi im using this code to upload song image and m4a file to my server . Hope this work for you.

func call_Api_Add_PostWithImage(_ uploadImage:UIImage, _ songName:String, _ songData_:NSData,_ text:String)
    {
        self.slider_progress.value = 0
        self.slider_progress.isHidden = false
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyyMMddhhmmss"
        let dateString = dateFormatter.string(from: NSDate() as Date)
        let imgName = "\(dateString)_SM_POST.png"

        let profileId = AppConfig.USER_ID == parentVC.profile_id ? AppConfig.USER_ID : parentVC.profile_id
        var param = API_KEYS.post_dict

        param["userid"] = AppConfig.USER_ID
        param["profile_id"] = profileId
        param["posttype"] = "4"
        param["parentpost"] = "0"
        param["description"] = txt_message
        param["image"] = ""
        param["source"] = "1"
        param["title"] = ""
        param["info"] = songJsonString


        Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(UIImageJPEGRepresentation(uploadImage, 0.5)!, withName: "audio_banner", fileName: imgName, mimeType: "image/jpeg")
            multipartFormData.append(songData_ as Data, withName: "audio", fileName: songName, mimeType: "audio/m4a")
            for (key, value) in param {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
        }, to: API_POST_ADD_POST)
        { (result) in
            switch result {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (Progress) in
                    print("Upload Progress: \(Progress.fractionCompleted)")
                    DispatchQueue.main.async {
                        self.slider_progress.setValue(Float(Progress.fractionCompleted), animated: true)
                    }

                })

                upload.responseJSON { response in
                    if let JSON = response.result.value {
                        print("Response : ",JSON)
                        if let dictJson = JSON as? NSDictionary
                        {
                            let checkResult = dictJson[successKey] as? Int ?? 0
                            if checkResult == 1
                            {
                                if let valueData = dictJson[resKey] as? NSDictionary
                                {
                                    if let objeResponse = UserPostModel(dictionary: valueData)
                                    {                                        
                                        self.parentVC.arr_userPosts.insert(objeResponse, at: 0)
                                        DispatchQueue.main.async {
                                            self.showSuccessPopup()
                                            self.parentVC.tbl_profile_info.reloadData()
                                        }
                                    }
                                }
                            }
                        }
                    }

                    DispatchQueue.main.async {
                        self.slider_progress.value = 0
                        self.slider_progress.isHidden = true
                    }

                }

            case .failure(let encodingError):
                print(encodingError)
                DispatchQueue.main.async {
                    self.slider_progress.value = 0
                    self.slider_progress.isHidden = true
                }
            }

        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!