Loading Audio message takes too long












0















Im using JSQMessageViewController for my real-time chatting app UI, and firebase for my chatting app database. For text message and image message, the loading still consider ok, but when it comes to audio message, it take very long time. Here is my code for retrieving audio message:



func observeMessages() {

let userMessagesRef = Database.database().reference().child("messages").child(Id).child(senderId).queryLimited(toLast: 50)
userMessagesRef.observe(.childAdded, with: { (snapshot) in

let messageId = snapshot.key
let messagesRef = Database.database().reference().child("messages").child(messageId)

messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in

if (dictionary["fromId"] as? String ?? "" == self.userId ? dictionary["toId"] as? String ?? "" : dictionary["fromId"] as? String ?? "") == self.contactLists.first?.id{

let date = NSDate(timeIntervalSince1970: dictionary["timestamp"] as? Double ?? 0.0)

let textString = dictionary["text"] as? String ?? ""
let imgString = dictionary["imageUrl"] as? String ?? ""
let voiceString = dictionary["voiceUrl"] as? String ?? ""

if textString == "image"{

let imageView = AsyncPhotoMediaItem(withURL: URL(string: imgString)!)
let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, media: imageView)
self.messages.append(message!)

}else if textString == "voice"{

print("voice msg")
let url = URL(string: voiceString)
let data = try? Data(contentsOf: url!)
let voiceData = data
let voice1 = JSQAudioMediaItem(data: voiceData)

let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, media: voice1)
self.messages.append(message!)

}else{

print("text msg")
let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, text: dictionary["text"] as? String ?? "")
self.messages.append(message!)

}

}; DispatchQueue.main.async {

self.collectionView?.reloadData()

}

self.finishReceivingMessage(animated: true)

}, withCancel: nil)

}, withCancel: nil)
}


Can someone help me out or is there any other way to do it or enhance it? Please help me










share|improve this question

























  • Why do you add a query and iterate over the messages using .childAdded then then go back and reload the same message from the ones just loaded? In other words, if you know the message you want to load, why have a query? Just load the message.

    – Jay
    Nov 13 '18 at 15:22













  • Also, you don't need DispatchQueue.main.async as UI functions run on the main thread inside Firebase closures.

    – Jay
    Nov 13 '18 at 15:31











  • So the reason this audio is loading slowly is because the entire message is loaded before it can be played. In other words you aren't "streaming" the audio. Depending on your file size / internet speed this will cause a delay of several seconds. Apps like Spotify avoid this by using HLS. However, this involves transcoding the audio and storage for this is not supported by Firebase.Your best bet would be not to show the user they have an audio message until after it's completely loaded. This avoids the user noticing the delay.

    – DoesData
    Nov 13 '18 at 15:37













  • so what should I do now? save the audio in local database? or use any lib to enhance it? @DoesData

    – mimi93
    Nov 14 '18 at 1:19


















0















Im using JSQMessageViewController for my real-time chatting app UI, and firebase for my chatting app database. For text message and image message, the loading still consider ok, but when it comes to audio message, it take very long time. Here is my code for retrieving audio message:



func observeMessages() {

let userMessagesRef = Database.database().reference().child("messages").child(Id).child(senderId).queryLimited(toLast: 50)
userMessagesRef.observe(.childAdded, with: { (snapshot) in

let messageId = snapshot.key
let messagesRef = Database.database().reference().child("messages").child(messageId)

messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in

if (dictionary["fromId"] as? String ?? "" == self.userId ? dictionary["toId"] as? String ?? "" : dictionary["fromId"] as? String ?? "") == self.contactLists.first?.id{

let date = NSDate(timeIntervalSince1970: dictionary["timestamp"] as? Double ?? 0.0)

let textString = dictionary["text"] as? String ?? ""
let imgString = dictionary["imageUrl"] as? String ?? ""
let voiceString = dictionary["voiceUrl"] as? String ?? ""

if textString == "image"{

let imageView = AsyncPhotoMediaItem(withURL: URL(string: imgString)!)
let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, media: imageView)
self.messages.append(message!)

}else if textString == "voice"{

print("voice msg")
let url = URL(string: voiceString)
let data = try? Data(contentsOf: url!)
let voiceData = data
let voice1 = JSQAudioMediaItem(data: voiceData)

let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, media: voice1)
self.messages.append(message!)

}else{

print("text msg")
let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, text: dictionary["text"] as? String ?? "")
self.messages.append(message!)

}

}; DispatchQueue.main.async {

self.collectionView?.reloadData()

}

self.finishReceivingMessage(animated: true)

}, withCancel: nil)

}, withCancel: nil)
}


Can someone help me out or is there any other way to do it or enhance it? Please help me










share|improve this question

























  • Why do you add a query and iterate over the messages using .childAdded then then go back and reload the same message from the ones just loaded? In other words, if you know the message you want to load, why have a query? Just load the message.

    – Jay
    Nov 13 '18 at 15:22













  • Also, you don't need DispatchQueue.main.async as UI functions run on the main thread inside Firebase closures.

    – Jay
    Nov 13 '18 at 15:31











  • So the reason this audio is loading slowly is because the entire message is loaded before it can be played. In other words you aren't "streaming" the audio. Depending on your file size / internet speed this will cause a delay of several seconds. Apps like Spotify avoid this by using HLS. However, this involves transcoding the audio and storage for this is not supported by Firebase.Your best bet would be not to show the user they have an audio message until after it's completely loaded. This avoids the user noticing the delay.

    – DoesData
    Nov 13 '18 at 15:37













  • so what should I do now? save the audio in local database? or use any lib to enhance it? @DoesData

    – mimi93
    Nov 14 '18 at 1:19
















0












0








0








Im using JSQMessageViewController for my real-time chatting app UI, and firebase for my chatting app database. For text message and image message, the loading still consider ok, but when it comes to audio message, it take very long time. Here is my code for retrieving audio message:



func observeMessages() {

let userMessagesRef = Database.database().reference().child("messages").child(Id).child(senderId).queryLimited(toLast: 50)
userMessagesRef.observe(.childAdded, with: { (snapshot) in

let messageId = snapshot.key
let messagesRef = Database.database().reference().child("messages").child(messageId)

messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in

if (dictionary["fromId"] as? String ?? "" == self.userId ? dictionary["toId"] as? String ?? "" : dictionary["fromId"] as? String ?? "") == self.contactLists.first?.id{

let date = NSDate(timeIntervalSince1970: dictionary["timestamp"] as? Double ?? 0.0)

let textString = dictionary["text"] as? String ?? ""
let imgString = dictionary["imageUrl"] as? String ?? ""
let voiceString = dictionary["voiceUrl"] as? String ?? ""

if textString == "image"{

let imageView = AsyncPhotoMediaItem(withURL: URL(string: imgString)!)
let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, media: imageView)
self.messages.append(message!)

}else if textString == "voice"{

print("voice msg")
let url = URL(string: voiceString)
let data = try? Data(contentsOf: url!)
let voiceData = data
let voice1 = JSQAudioMediaItem(data: voiceData)

let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, media: voice1)
self.messages.append(message!)

}else{

print("text msg")
let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, text: dictionary["text"] as? String ?? "")
self.messages.append(message!)

}

}; DispatchQueue.main.async {

self.collectionView?.reloadData()

}

self.finishReceivingMessage(animated: true)

}, withCancel: nil)

}, withCancel: nil)
}


Can someone help me out or is there any other way to do it or enhance it? Please help me










share|improve this question
















Im using JSQMessageViewController for my real-time chatting app UI, and firebase for my chatting app database. For text message and image message, the loading still consider ok, but when it comes to audio message, it take very long time. Here is my code for retrieving audio message:



func observeMessages() {

let userMessagesRef = Database.database().reference().child("messages").child(Id).child(senderId).queryLimited(toLast: 50)
userMessagesRef.observe(.childAdded, with: { (snapshot) in

let messageId = snapshot.key
let messagesRef = Database.database().reference().child("messages").child(messageId)

messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in

if (dictionary["fromId"] as? String ?? "" == self.userId ? dictionary["toId"] as? String ?? "" : dictionary["fromId"] as? String ?? "") == self.contactLists.first?.id{

let date = NSDate(timeIntervalSince1970: dictionary["timestamp"] as? Double ?? 0.0)

let textString = dictionary["text"] as? String ?? ""
let imgString = dictionary["imageUrl"] as? String ?? ""
let voiceString = dictionary["voiceUrl"] as? String ?? ""

if textString == "image"{

let imageView = AsyncPhotoMediaItem(withURL: URL(string: imgString)!)
let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, media: imageView)
self.messages.append(message!)

}else if textString == "voice"{

print("voice msg")
let url = URL(string: voiceString)
let data = try? Data(contentsOf: url!)
let voiceData = data
let voice1 = JSQAudioMediaItem(data: voiceData)

let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, media: voice1)
self.messages.append(message!)

}else{

print("text msg")
let message = JSQMessage(senderId: dictionary["toId"] as? String ?? "", senderDisplayName: "", date: date as Date, text: dictionary["text"] as? String ?? "")
self.messages.append(message!)

}

}; DispatchQueue.main.async {

self.collectionView?.reloadData()

}

self.finishReceivingMessage(animated: true)

}, withCancel: nil)

}, withCancel: nil)
}


Can someone help me out or is there any other way to do it or enhance it? Please help me







swift firebase jsqmessagesviewcontroller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:06







mimi93

















asked Nov 13 '18 at 9:49









mimi93mimi93

1341111




1341111













  • Why do you add a query and iterate over the messages using .childAdded then then go back and reload the same message from the ones just loaded? In other words, if you know the message you want to load, why have a query? Just load the message.

    – Jay
    Nov 13 '18 at 15:22













  • Also, you don't need DispatchQueue.main.async as UI functions run on the main thread inside Firebase closures.

    – Jay
    Nov 13 '18 at 15:31











  • So the reason this audio is loading slowly is because the entire message is loaded before it can be played. In other words you aren't "streaming" the audio. Depending on your file size / internet speed this will cause a delay of several seconds. Apps like Spotify avoid this by using HLS. However, this involves transcoding the audio and storage for this is not supported by Firebase.Your best bet would be not to show the user they have an audio message until after it's completely loaded. This avoids the user noticing the delay.

    – DoesData
    Nov 13 '18 at 15:37













  • so what should I do now? save the audio in local database? or use any lib to enhance it? @DoesData

    – mimi93
    Nov 14 '18 at 1:19





















  • Why do you add a query and iterate over the messages using .childAdded then then go back and reload the same message from the ones just loaded? In other words, if you know the message you want to load, why have a query? Just load the message.

    – Jay
    Nov 13 '18 at 15:22













  • Also, you don't need DispatchQueue.main.async as UI functions run on the main thread inside Firebase closures.

    – Jay
    Nov 13 '18 at 15:31











  • So the reason this audio is loading slowly is because the entire message is loaded before it can be played. In other words you aren't "streaming" the audio. Depending on your file size / internet speed this will cause a delay of several seconds. Apps like Spotify avoid this by using HLS. However, this involves transcoding the audio and storage for this is not supported by Firebase.Your best bet would be not to show the user they have an audio message until after it's completely loaded. This avoids the user noticing the delay.

    – DoesData
    Nov 13 '18 at 15:37













  • so what should I do now? save the audio in local database? or use any lib to enhance it? @DoesData

    – mimi93
    Nov 14 '18 at 1:19



















Why do you add a query and iterate over the messages using .childAdded then then go back and reload the same message from the ones just loaded? In other words, if you know the message you want to load, why have a query? Just load the message.

– Jay
Nov 13 '18 at 15:22







Why do you add a query and iterate over the messages using .childAdded then then go back and reload the same message from the ones just loaded? In other words, if you know the message you want to load, why have a query? Just load the message.

– Jay
Nov 13 '18 at 15:22















Also, you don't need DispatchQueue.main.async as UI functions run on the main thread inside Firebase closures.

– Jay
Nov 13 '18 at 15:31





Also, you don't need DispatchQueue.main.async as UI functions run on the main thread inside Firebase closures.

– Jay
Nov 13 '18 at 15:31













So the reason this audio is loading slowly is because the entire message is loaded before it can be played. In other words you aren't "streaming" the audio. Depending on your file size / internet speed this will cause a delay of several seconds. Apps like Spotify avoid this by using HLS. However, this involves transcoding the audio and storage for this is not supported by Firebase.Your best bet would be not to show the user they have an audio message until after it's completely loaded. This avoids the user noticing the delay.

– DoesData
Nov 13 '18 at 15:37







So the reason this audio is loading slowly is because the entire message is loaded before it can be played. In other words you aren't "streaming" the audio. Depending on your file size / internet speed this will cause a delay of several seconds. Apps like Spotify avoid this by using HLS. However, this involves transcoding the audio and storage for this is not supported by Firebase.Your best bet would be not to show the user they have an audio message until after it's completely loaded. This avoids the user noticing the delay.

– DoesData
Nov 13 '18 at 15:37















so what should I do now? save the audio in local database? or use any lib to enhance it? @DoesData

– mimi93
Nov 14 '18 at 1:19







so what should I do now? save the audio in local database? or use any lib to enhance it? @DoesData

– mimi93
Nov 14 '18 at 1:19














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278165%2floading-audio-message-takes-too-long%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278165%2floading-audio-message-takes-too-long%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation