twilio / conversations-ios

SPM releases
https://www.twilio.com/docs/conversations/ios/changelog
10 stars 5 forks source link

Audio Message not working #9

Closed aiyub007 closed 3 years ago

aiyub007 commented 3 years ago

I am trying to upload media using below code

let messageOptions = TCHMessageOptions()
        do {
            let inputStream = try InputStream(data: Data(contentsOf: recordingView.getFileURL()))
            messageOptions.withMediaStream(inputStream,
                                           contentType: "audio/m4a",
                                           defaultFilename: "sound.m4a",
                                           onStarted: {

                                           },
                                           onProgress: { (bytes) in

                                           }) { (mediaSid) in
                self.currentConversationObj.sendMessage(with: messageOptions) { (result, msg) in
                    if result.resultCode == 200
                    {
                        self.currentConversationObj.setAllMessagesReadWithCompletion { (res, count) in }
                    }
                }

I also tried different Mime type [contentType] . Media is there in data control just not going in started or on progress. completion not working no error got in the result

Thanks in advance

Gray-Wind commented 3 years ago

Hello, you should create options and then send message using these options. To solve your issue it should be enough to move 'sendMessage' from media upload completion.

aiyub007 commented 3 years ago

@Gray-Wind Thanks for response we are already doing the process for the video it works fine.

As per your suggestion i tries modify it in completed :

 let messageOptions = TCHMessageOptions()
        do {
            let inputStream = try InputStream(data: Data(contentsOf: recordingView.getFileURL()))
            messageOptions.withMediaStream(inputStream, contentType: "audio/m4a", defaultFilename: "audio.m4a") {

            } onProgress: { (prg) in
                print(prg)
            } onCompleted: { (mediaSid) in
                self.currentConversationObj.sendMessage(with: messageOptions) { (result, msg) in
                    if result.resultCode == 200
                    {
                        self.currentConversationObj.setAllMessagesReadWithCompletion { (res, count) in }
                    }
                }
            }

        }catch {
            print(error as Error)
        }

still the same problem. control is not reaching to the completion not even on progress Still checking the mime types

aiyub007 commented 3 years ago

It worked Needed to call out side

{ (mediaSid) in
 }
self.currentConversationObj.sendMessage(with: messageOptions) { (result, msg) in
                    if result.resultCode == 200
                    {
                        self.currentConversationObj.setAllMessagesReadWithCompletion { (res, count) in }
                    }
Gray-Wind commented 3 years ago

Basically TCHMessageOptions doesn't do anything except for telling sendMessage what to send.

let messageOptions = TCHMessageOptions()
        do {
            let inputStream = try InputStream(data: Data(contentsOf: recordingView.getFileURL()))
            messageOptions.withMediaStream(inputStream,
                                           contentType: "audio/m4a",
                                           defaultFilename: "sound.m4a",
                                           onStarted: {

                                           },
                                           onProgress: { (bytes) in

                                           }) { (mediaSid) in
                                               print("message with sent with sid \(mediaSid)")
                                           }
            self.currentConversationObj.sendMessage(with: messageOptions) { (result, msg) in
                if result.isSuccessful {
                    self.currentConversationObj.setAllMessagesReadWithCompletion { (res, count) in }
                }
            }
        }