Audio message length shows 00:00

Hi, we are using IOS UI kit, and when we send audio message then its displays recording length time is 00:00 always,
can you please tell us how to fix this.

Screenshot below:

@vibhorMaheshwari From your issue, could I request the full code that you’ve implemented for review?

@SocialPlus_Support

Code Snap:

import UIKit
import AmitySDK

final class AmityMessageAudioTableViewCell: AmityMessageTableViewCell {
    
    @IBOutlet private var durationLabel: UILabel!
    @IBOutlet private var actionImageView: UIImageView!
    @IBOutlet private var activityIndicatorView: UIActivityIndicatorView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupView()
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        durationLabel.text = "0:00"
        actionImageView.image = AmityIconSet.Chat.iconPlay
    }
    
    func setupView() {
        durationLabel.text = "0:00"
        durationLabel.font = AmityFontSet.body
        durationLabel.textAlignment = .right
        actionImageView.image = AmityIconSet.Chat.iconPlay
        
        activityIndicatorView.hidesWhenStopped = true
        
    }
    
    override func display(message: AmityMessageModel) {
        
        if !message.isDeleted {
            if message.isOwner {
                durationLabel.textColor = AmityColorSet.baseInverse
                actionImageView.tintColor = AmityColorSet.baseInverse
                activityIndicatorView.style = .medium
                switch message.syncState {
                case .syncing:
                    durationLabel.alpha = 0
                    activityIndicatorView.startAnimating()
                case .synced, .default, .error:
                    durationLabel.alpha = 1
                    activityIndicatorView.stopAnimating()
                @unknown default:
                    break
                }
            } else {
                durationLabel.textColor = AmityColorSet.base
                actionImageView.tintColor = AmityColorSet.base
                activityIndicatorView.style = .medium
            }
            
            if AmityAudioPlayer.shared.isPlaying(), AmityAudioPlayer.shared.fileName == message.messageId {
                actionImageView.image = AmityIconSet.Chat.iconPause
            } else {
                actionImageView.image = AmityIconSet.Chat.iconPlay
            }
        }
        super.display(message: message)
    }
    
    override class func height(for message: AmityMessageModel, boundingWidth: CGFloat) -> CGFloat {
        let displaynameHeight: CGFloat = message.isOwner ? 0 : 22
        if message.isDeleted {
            return AmityMessageTableViewCell.deletedMessageCellHeight + displaynameHeight
        }
        return 90 + displaynameHeight
    }
    
}

extension AmityMessageAudioTableViewCell {
    @IBAction func playTap(_ sender: UIButton) {
        if !message.isDeleted && message.syncState == .synced {
            sender.isEnabled = false
            AmityUIKitManagerInternal.shared.messageMediaService.download(for: message.object) { [weak self] in
                self?.durationLabel.alpha = 0
                self?.activityIndicatorView.startAnimating()
            } completion: { [weak self] (result) in
                guard let strongSelf = self else { return }
                switch result {
                case .success(let url):
                    AmityAudioPlayer.shared.delegate = self
                    AmityAudioPlayer.shared.fileName = strongSelf.message.messageId
                    AmityAudioPlayer.shared.path = url
                    AmityAudioPlayer.shared.play()
                    sender.isEnabled = true
                    strongSelf.activityIndicatorView.stopAnimating()
                    strongSelf.durationLabel.alpha = 1
                case .failure(let error):
                    Log.add(error.localizedDescription)
                }
            }
        }
    }
}

extension AmityMessageAudioTableViewCell: AmityAudioPlayerDelegate {
    func playing() {
        actionImageView.image = AmityIconSet.Chat.iconPause
        delegate?.performEvent(self, events: .audioPlaying)
    }
    
    func stopPlaying() {
        actionImageView.image = AmityIconSet.Chat.iconPlay
        durationLabel.text = "0:00"
    }
    
    func finishPlaying() {
        actionImageView.image = AmityIconSet.Chat.iconPlay
        durationLabel.text = "0:00"
    }
    
    func displayDuration(_ duration: String) {
        durationLabel.text = duration
    }
}

@vibhorMaheshwari We have checked with the relevant team and found that, by design, the SDK does not provide the duration of audio files. However, there is a workaround to display the audio duration:

  1. After completing the audio recording, save the audio duration in the message metadata.
  2. To display the audio duration, extract this information from the message metadata.

@SocialPlus_Support
Thanks for your support, we have fixed this by using metadata.

1 Like