您好,登錄后才能下訂單哦!
import Foundation
import Alamofire
//上傳音頻 ,multipartFormData 上傳。key = audio
extension HttpManager {
/**
音頻文件
- parameter audioData: 音頻 Data
- parameter success: 成功回調 audio model
- parameter failure: 失敗
*/
class func uploadAudio(
_ audioData: Data,
recordTime: String,
success:@escaping (_ audioModel: UploadAudioModel) ->Void,
failure:@escaping (Void) ->Void)
{
let parameters = [
"access_token": UserInstance.accessToken,
"record_time": recordTime
]
/*
這里需要填寫上傳音頻的 API
*/
let uploadAudioURLString = ""
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(audioData, withName: "audio", fileName: "file", mimeType: "audio/AMR")
for (key, value) in parameters {
multipartFormData.append(value!.data(using: String.Encoding.utf8)!, withName: key)
}
},
to: uploadAudioURLString,
encodingCompletion: { result in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
//XCGLogger,TSLogger.swift
log.info("response:\(response)")
switch response.result {
case .success(let data):
/*
根據 JSON 返回格式,做好 UploadAudioModel 的 key->value 映射, 這里只是個例子
*/
let model: UploadAudioModel = TSMapper<UploadAudioModel>().map(JSONObject: data)!
success(model)
case .failure( _):
failure()
}
}
case .failure(let encodingError):
debugPrint(encodingError)
}
})
}
}
// 上傳音頻接口返回的圖片 Model
//class UploadAudioModel : TSModelProtocol {
// var audioId : String?
// var duration : Int?
// var audioURL : String?
// var fileSize : Int?
// var keyHash : String?
// var recordTime : String?
//
// required init?(map: Map) {
//
// }
//
// func mapping(map: Map) {
// audioId <- map["audio_id"]
// audioURL <- map["audio_url"]
// duration <- map["duration"]
// keyHash <- map["key_hash"]
// fileSize <- map["file_size"]
// recordTime <- map["recordTime"]
// }
//}
//使用的地方
/**
* 異步上傳音頻文件, 然后上傳成功后,把 model 值改掉
* 因為還沒有上傳的 API,所以這個函數會返回錯誤 T.T
HttpManager.uploadAudio(uploadAmrData, recordTime: String(recordTime), success: {model in
audioModel.keyHash = model.keyHash
audioModel.audioURL = model.audioURL
audioModel.duration = recordTime
}, failure: {
})
}
*/
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。