gpt4 book ai didi

ios - 使用 Alamofire 上传音频文件

转载 作者:行者123 更新时间:2023-11-29 05:33:55 27 4
gpt4 key购买 nike

我正在尝试使用 Alamofire 从 TableView UI 将音频文件上传到服务器。

我的应用程序崩溃了,我想我在 multipartformdata 行中错误地连接了我的 URL,请帮助我如何正确设置。

@IBAction func Upload(_ sender: Any) {
let voiceData = try? Data(contentsOf: getDirectory())
let params : [String:String] = []

Alamofire.upload(
multipartFormData: { (multipartForm) in
multipartForm.append(voiceData!, withName: "voice", fileName: "file", mimeType: "audio/m4a")
for (key, value) in params {
multipartForm.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
usingThreshold : SessionManager.multipartFormDataEncodingMemoryThreshold,
to : "http://18.136.194.124/voupload.php",
method: .post){ (result) in

switch result {

'''''''
}

func getDirectory() -> URL
{
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = paths[0]
return documentDirectory
}


@IBOutlet weak var mtable: UITableView!
@IBOutlet weak var Butlabel: UIButton!
var recordingSession:AVAudioSession!
var audiorecorder:AVAudioRecorder!
var audioPlayer:AVAudioPlayer!
var numberOfrecords = 0


@IBAction func RecordButton(_ sender: Any) {

if audiorecorder == nil
{
numberOfrecords += 1
let filename = getDirectory().appendingPathComponent("\(numberOfrecords).m4a")


let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 1200, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue]

// VOXIT STARTS
do
{
audiorecorder = try AVAudioRecorder(url: filename, settings: settings)
audiorecorder.delegate = self
audiorecorder.record()

Butlabel.setTitle("Stop Recording", for: .normal)
}
catch{
displayAlert(title: "Ups!", message: "Recording Failed")
}

}
else
{
// STOP rec
}
}

如果有人可以解释一下 MultiDataForm 代码。感谢任何帮助。

最佳答案

Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(data, // the audio as Data
withName: "Filedata",
fileName: "file.m4a", // name of the file
mimeType: "audio/mpeg")
}, to: uploadUrl, encodingCompletion: { (result) in

switch result {
case .failure(let error):
completion(.failure(Error you want to throw))
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})

upload.responseJSON { response in
if let error = response.error {
completion(.failure(Error you want to throw))
} else {
completion(.success(()))
}
}
}

})

关于ios - 使用 Alamofire 上传音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57248190/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com