Przeglądaj źródła

show audio file duration in audio message bubbles

cyberta 4 lat temu
rodzic
commit
58e869487f

+ 26 - 0
deltachat-ios/Chat/AudioController.swift

@@ -85,6 +85,32 @@ open class AudioController: NSObject, AVAudioPlayerDelegate, AudioMessageCellDel
             cell.audioPlayerView.setDuration(duration: player.currentTime)
         }
     }
+    
+    public func getAudioDuration(messageId: Int, successHandler: @escaping (Int, Double) -> Void) {
+        let message = DcMsg(id: messageId)
+        if playingMessage?.id == messageId {
+            // irgnore messages that are currently playing or recently paused
+            return
+        }
+        
+        if let fileURL = message.fileURL {
+            let audioAsset = AVURLAsset.init(url: fileURL, options: nil)
+
+            audioAsset.loadValuesAsynchronously(forKeys: ["duration"]) {
+                var error: NSError?
+                let status = audioAsset.statusOfValue(forKey: "duration", error: &error)
+                switch status {
+                case .loaded:
+                    let duration = audioAsset.duration
+                    let durationInSeconds = CMTimeGetSeconds(duration)
+                    DispatchQueue.main.async {
+                        successHandler(messageId, Double(durationInSeconds))
+                    }
+                default: break
+                }
+            }
+        }
+    }
 
     public func playButtonTapped(cell: AudioMessageCell, messageId: Int) {
             let message = DcMsg(id: messageId)

+ 10 - 1
deltachat-ios/Chat/Views/Cells/AudioMessageCell.swift

@@ -5,6 +5,8 @@ import DcCore
 // do not confuse with BaseMessageCellDelegate that is for sending events to ChatViewControllerNew.
 public protocol AudioMessageCellDelegate: AnyObject {
     func playButtonTapped(cell: AudioMessageCell, messageId: Int)
+    func getAudioDuration(messageId: Int, successHandler: @escaping (Int, Double) -> Void)
+
 }
 
 public class AudioMessageCell: BaseMessageCell {
@@ -38,7 +40,6 @@ public class AudioMessageCell: BaseMessageCell {
     }
 
     override func update(msg: DcMsg, messageStyle: UIRectCorner, showAvatar: Bool, showName: Bool) {
-        //audioPlayerView.reset()
         messageId = msg.id
         if let text = msg.text {
             mainContentView.spacing = text.isEmpty ? 0 : 8
@@ -51,6 +52,14 @@ public class AudioMessageCell: BaseMessageCell {
         } else {
             accessibilityLabel = String.localized("audio")
         }
+        
+        delegate?.getAudioDuration(messageId: messageId, successHandler: { [weak self] (messageId, duration) -> Void in
+            if let self = self,
+               messageId == self.messageId {
+                self.audioPlayerView.setDuration(duration: duration)
+            }
+        })
+        
 
         super.update(msg: msg, messageStyle: messageStyle, showAvatar: showAvatar, showName: showName)
     }