From 4ef84f2758bb10d96bc1a0ed7bef5c2ed28fda68 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Fri, 27 Nov 2015 10:30:23 +0800 Subject: [PATCH] playing progress of feed audio --- Yep/Realm/Models.swift | 12 +++ .../ConversationViewController.swift | 8 +- .../Feeds/FeedsViewController.swift | 100 +++++++++++++++++- .../ChatLeftAudio/ChatLeftAudioCell.swift | 2 +- .../FeedSocialWork/FeedSocialWorkCell.swift | 17 +++ 5 files changed, 133 insertions(+), 6 deletions(-) diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index 8fa7ea77..db7585ac 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -695,6 +695,18 @@ class FeedAudio: Object { return realm.objects(FeedAudio).filter(predicate).first } + var audioMetaInfo: (duration: Double, samples: [CGFloat])? { + + if let metaDataInfo = decodeJSON(metadata) { + if let + duration = metaDataInfo[YepConfig.MetaData.audioDuration] as? Double, + samples = metaDataInfo[YepConfig.MetaData.audioSamples] as? [CGFloat] { + return (duration, samples) + } + } + + return nil + } } class Feed: Object { diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 1c334b6b..8d58d410 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -1691,9 +1691,9 @@ class ConversationViewController: BaseViewController { return width } - private var audioPlayedDurations = [String: Double]() + private var audioPlayedDurations = [String: NSTimeInterval]() - private func audioPlayedDurationOfMessage(message: Message) -> Double { + private func audioPlayedDurationOfMessage(message: Message) -> NSTimeInterval { let key = message.messageID if !key.isEmpty { @@ -1705,7 +1705,7 @@ class ConversationViewController: BaseViewController { return 0 } - private func setAudioPlayedDuration(audioPlayedDuration: Double, ofMessage message: Message) { + private func setAudioPlayedDuration(audioPlayedDuration: NSTimeInterval, ofMessage message: Message) { let key = message.messageID if !key.isEmpty { audioPlayedDurations[key] = audioPlayedDuration @@ -2544,7 +2544,7 @@ class ConversationViewController: BaseViewController { } if let message = message { - let audioPlayedDuration = audioPlayedDurationOfMessage(message) as NSTimeInterval + let audioPlayedDuration = audioPlayedDurationOfMessage(message) YepAudioService.sharedManager.playAudioWithMessage(message, beginFromTime: audioPlayedDuration, delegate: self) { let playbackTimer = NSTimer.scheduledTimerWithTimeInterval(0.02, target: self, selector: "updateAudioPlaybackProgress:", userInfo: nil, repeats: true) YepAudioService.sharedManager.playbackTimer = playbackTimer diff --git a/Yep/ViewControllers/Feeds/FeedsViewController.swift b/Yep/ViewControllers/Feeds/FeedsViewController.swift index 73aa7180..c1e576c2 100644 --- a/Yep/ViewControllers/Feeds/FeedsViewController.swift +++ b/Yep/ViewControllers/Feeds/FeedsViewController.swift @@ -103,6 +103,48 @@ class FeedsViewController: BaseViewController { var feeds = [DiscoveredFeed]() + private var audioPlayedDurations = [String: NSTimeInterval]() + + private func audioPlayedDurationOfFeedAudio(feedAudio: FeedAudio) -> NSTimeInterval { + let key = feedAudio.feedID + + if !key.isEmpty { + if let playedDuration = audioPlayedDurations[key] { + return playedDuration + } + } + + return 0 + } + + private func setAudioPlayedDuration(audioPlayedDuration: NSTimeInterval, ofFeedAudio feedAudio: FeedAudio) { + let key = feedAudio.feedID + if !key.isEmpty { + audioPlayedDurations[key] = audioPlayedDuration + } + +// // recover audio cells' UI +// +// if audioPlayedDuration == 0 { +// +// if let sender = message.fromFriend, index = messages.indexOf(message) { +// +// let indexPath = NSIndexPath(forItem: index - displayedMessagesRange.location, inSection: 0) +// +// if sender.friendState != UserFriendState.Me.rawValue { // from Friend +// if let cell = conversationCollectionView.cellForItemAtIndexPath(indexPath) as? ChatLeftAudioCell { +// cell.audioPlayedDuration = 0 +// } +// +// } else { +// if let cell = conversationCollectionView.cellForItemAtIndexPath(indexPath) as? ChatRightAudioCell { +// cell.audioPlayedDuration = 0 +// } +// } +// } +// } + } + private func updateFeedsTableViewOrInsertWithIndexPaths(indexPaths: [NSIndexPath]?) { // refresh skillUsers @@ -427,6 +469,58 @@ class FeedsViewController: BaseViewController { } } + func updateAudioPlaybackProgress(timer: NSTimer) { + + func updateCellOfFeedAudio(feedAudio: FeedAudio, withCurrentTime currentTime: NSTimeInterval) { + + let feedID = feedAudio.feedID + + for index in 0.. Void)? + var audioPlayedDuration: NSTimeInterval = 0 { + willSet { + updateVoiceContainerView() + } + } + private func updateVoiceContainerView() { + + guard let feed = feed, realm = try? Realm(), feedAudio = FeedAudio.feedAudioWithFeedID(feed.id, inRealm: realm) else { + return + } + + if let (audioDuration, audioSamples) = feedAudio.audioMetaInfo { + + voiceSampleView.samples = audioSamples + voiceSampleView.progress = CGFloat(audioPlayedDuration / audioDuration) + } + } static let messageTextViewMaxWidth: CGFloat = { let maxWidth = UIScreen.mainScreen().bounds.width - (15 + 40 + 10 + 15)