少年辛苦终身事,莫向光阴惰寸功。这篇文章主要讲述Alamofire +ObjectMapper模型: 上传音频。相关的知识,希望能为你提供帮助。
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, _, _):
【Alamofire +ObjectMapper模型: 上传音频。】
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: {
})
}
*/
推荐阅读
- 微博头像专辑删除办法_新浪微博
- Android自己定义View画布Canvas与画笔Paint
- Android自己定义截屏功能,相似QQ截屏
- Cocos2D-X2.2.3学习笔记9(处理重力感应事件,移植到Android加入两次返回退出游戏效果)
- 谋哥(玩App怎么赚钱)
- 移动端调试 — Pure|微信环境调试方案|App环境调试方案
- 移动端调试 — 安卓机+chrome
- Android之——获取手机安装的应用程序
- Android 广播接收者 BroadcastReceiver