playing progress of feed audio

This commit is contained in:
nixzhu
2015-11-27 10:30:23 +08:00
parent 0ebb7ae7c4
commit 4ef84f2758
5 changed files with 133 additions and 6 deletions
+12
View File
@@ -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 {
@@ -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
@@ -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..<feeds.count {
let feed = feeds[index]
if feed.id == feedID {
let indexPath = NSIndexPath(forRow: index, inSection: Section.Feed.rawValue)
if let cell = feedsTableView.cellForRowAtIndexPath(indexPath) as? FeedSocialWorkCell {
cell.audioPlayedDuration = currentTime
}
break
}
}
// if let messageIndex = messages.indexOf(message) {
//
// let indexPath = NSIndexPath(forItem: messageIndex - displayedMessagesRange.location, inSection: 0)
//
// if let sender = message.fromFriend {
// if sender.friendState != UserFriendState.Me.rawValue {
// if let cell = conversationCollectionView.cellForItemAtIndexPath(indexPath) as? ChatLeftAudioCell {
// cell.audioPlayedDuration = currentTime
// }
//
// } else {
// if let cell = conversationCollectionView.cellForItemAtIndexPath(indexPath) as? ChatRightAudioCell {
// cell.audioPlayedDuration = currentTime
// }
// }
// }
// }
}
if let audioPlayer = YepAudioService.sharedManager.audioPlayer {
if let playingFeedAudio = YepAudioService.sharedManager.playingFeedAudio {
let currentTime = audioPlayer.currentTime
setAudioPlayedDuration(currentTime, ofFeedAudio: playingFeedAudio )
updateCellOfFeedAudio(playingFeedAudio, withCurrentTime: currentTime)
}
}
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
@@ -764,9 +858,13 @@ extension FeedsViewController: UITableViewDataSource, UITableViewDelegate {
cell.audioPlaying = false
} else {
YepAudioService.sharedManager.playAudioWithFeedAudio(feedAudio, beginFromTime: 0, delegate: strongSelf, success: {
let audioPlayedDuration = strongSelf.audioPlayedDurationOfFeedAudio(feedAudio)
YepAudioService.sharedManager.playAudioWithFeedAudio(feedAudio, beginFromTime: audioPlayedDuration, delegate: strongSelf, success: {
println("playAudioWithFeedAudio success!")
let playbackTimer = NSTimer.scheduledTimerWithTimeInterval(0.02, target: strongSelf, selector: "updateAudioPlaybackProgress:", userInfo: nil, repeats: true)
YepAudioService.sharedManager.playbackTimer = playbackTimer
cell.audioPlaying = true
})
}
@@ -12,7 +12,7 @@ class ChatLeftAudioCell: ChatBaseCell {
var message: Message?
var audioPlayedDuration: Double = 0 {
var audioPlayedDuration: NSTimeInterval = 0 {
willSet {
updateAudioInfoViews()
}
@@ -70,6 +70,23 @@ class FeedSocialWorkCell: FeedBasicCell {
}
}
var playOrPauseAudioAction: (FeedSocialWorkCell -> 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)