使用Quickblox SDK 2.17以附件形式发送视频

时间:2018-07-24 11:24:51

标签: swift video attachment quickblox

我尝试了来自各个链接的代码,但是使用最新的SDK似乎无济于事。 我正在尝试以下代码:

let message = QBChatMessage()
message.senderID = (self.senderID)
message.dialogID = self.chatDialogModel.id
message.dateSent = Date()
message.readIDs = [NSNumber.init(value: (self.senderID))]
message.deliveredIDs = [NSNumber.init(value: (self.senderID))]

let aURL = Bundle.main.url(forResource: "testVideo", withExtension:"mp4")
let aData = try! Data(contentsOf: aURL!)

QBRequest.tUploadFile(aData, fileName: "Attachment Video", 
                     contentType:"video/mp4", 
                     isPublic: true, successBlock: { (qbResponse, qbBlob) in

   let attachment : QBChatAttachment = QBChatAttachment()
    attachment.type = "video"
    attachment.id = qbBlob.uid
    attachment.url = qbBlob.publicUrl()
    message.attachments = [attachment]
    message.text = "Attachment video"

    self.chatDataSource.add(message)


ServicesManager.instance().chatService.sendAttachmentMessage(message, to: (self.chatDialogModel)!, with: attachment, completion: { [weak self] (error) -> Void in


  self?.attachmentCellsMap.removeObject(forKey: message.id as AnyObject?)
  guard error != nil else { return }
  self?.chatDataSource.delete(message)

 })

}, statusBlock: nil) { (error) in

}

1 个答案:

答案 0 :(得分:0)

我想出了一个解决方案:

首先编写用于从照片库获取本地视频文件URL的代码。输入网址后,请调用此函数。

func sendVideoAsAttachment(_ url : URL){

    let message = QBChatMessage()
    message.senderID = (self.senderID)
    message.dialogID = self.chatDialogModel.id
    message.dateSent = Date()

    let attachment = QBChatAttachment.videoAttachment(withFileURL: url)
    self.chatDataSource.add(message)

    ServicesManager.instance().chatService.sendAttachmentMessage(message, to: self.chatDialogModel, with: attachment, completion: { (error) in

        self.attachmentCellsMap.removeObject(forKey: message.id as AnyObject?)
        guard error != nil else { return }
        self.chatDataSource.delete(message)
    })
}

这将发送视频作为附件。