Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

voice broadcast slider #7010

Merged
merged 11 commits into from
Nov 14, 2022
1 change: 1 addition & 0 deletions Riot/Modules/Room/RoomViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ - (void)viewWillDisappear:(BOOL)animated

[VoiceMessageMediaServiceProvider.sharedProvider pauseAllServices];
[VoiceBroadcastRecorderProvider.shared pauseRecording];
[VoiceBroadcastPlaybackProvider.shared pausePlaying];

// Stop the loading indicator even if the session is still in progress
[self stopLoadingUserIndicator];
Expand Down
4 changes: 4 additions & 0 deletions Riot/Modules/Room/VoiceMessages/VoiceMessageAudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class VoiceMessageAudioPlayer: NSObject {
return audioPlayer.items()
}

var currentUrl: URL? {
return (audioPlayer?.currentItem?.asset as? AVURLAsset)?.url
}

private(set) var isStopped = true

deinit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,25 @@ struct VoiceBroadcastBuilder {

var voiceBroadcast = VoiceBroadcast()

voiceBroadcast.chunks = Set(events.compactMap { event in
let chunks = Set(events.compactMap { event in
buildChunk(event: event, mediaManager: mediaManager, voiceBroadcastStartEventId: voiceBroadcastStartEventId)
})

voiceBroadcast.chunks = chunks
voiceBroadcast.duration = chunks.reduce(0) { $0 + $1.duration}

return voiceBroadcast
}

func buildChunk(event: MXEvent, mediaManager: MXMediaManager, voiceBroadcastStartEventId: String) -> VoiceBroadcastChunk? {
guard let attachment = MXKAttachment(event: event, andMediaManager: mediaManager),
let chunkInfo = event.content[VoiceBroadcastSettings.voiceBroadcastContentKeyChunkType] as? [String: UInt],
let sequence = chunkInfo[VoiceBroadcastSettings.voiceBroadcastContentKeyChunkSequence] else {
let sequence = chunkInfo[VoiceBroadcastSettings.voiceBroadcastContentKeyChunkSequence],
let audio = event.content[kMXMessageContentKeyExtensibleAudioMSC1767] as? [String: UInt],
let duration = audio[kMXMessageContentKeyExtensibleAudioDuration] else {
return nil
}

return VoiceBroadcastChunk(voiceBroadcastInfoEventId: voiceBroadcastStartEventId, sequence: sequence, attachment: attachment)
return VoiceBroadcastChunk(voiceBroadcastInfoEventId: voiceBroadcastStartEventId, sequence: sequence, attachment: attachment, duration: duration)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ public class VoiceBroadcastChunk: NSObject {
public private(set) var voiceBroadcastInfoEventId: String
public private(set) var sequence: UInt
public private(set) var attachment: MXKAttachment
public private(set) var duration: UInt

public init(voiceBroadcastInfoEventId: String,
sequence: UInt,
attachment: MXKAttachment) {
attachment: MXKAttachment,
duration: UInt) {
self.voiceBroadcastInfoEventId = voiceBroadcastInfoEventId
self.sequence = sequence
self.attachment = attachment
self.duration = duration
}

public static func == (lhs: VoiceBroadcastChunk, rhs: VoiceBroadcastChunk) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ public enum VoiceBroadcastKind {
public struct VoiceBroadcast {
var chunks: Set<VoiceBroadcastChunk> = []
var kind: VoiceBroadcastKind = .player
var duration: UInt = 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ import Foundation
class TimelinePollProvider {
static let shared = TimelinePollProvider()

var session: MXSession?
var session: MXSession? {
willSet {
guard let currentSession = self.session else { return }

if currentSession != newValue {
// Clear all stored coordinators on new session
coordinatorsForEventIdentifiers.removeAll()
}
}
}
var coordinatorsForEventIdentifiers = [String: TimelinePollCoordinator]()

private init() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ final class VoiceBroadcastPlaybackCoordinator: Coordinator, Presentable {
}

func endVoiceBroadcast() {}

func pausePlaying() {
viewModel.context.send(viewAction: .pause)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@

import Foundation

class VoiceBroadcastPlaybackProvider {
static let shared = VoiceBroadcastPlaybackProvider()
@objc class VoiceBroadcastPlaybackProvider: NSObject {
@objc static let shared = VoiceBroadcastPlaybackProvider()

var session: MXSession?
var session: MXSession? {
willSet {
guard let currentSession = self.session else { return }

if currentSession != newValue {
// Clear all stored coordinators on new session
coordinatorsForEventIdentifiers.removeAll()
}
}
}
var coordinatorsForEventIdentifiers = [String: VoiceBroadcastPlaybackCoordinator]()

private init() { }
private override init() { }

/// Create or retrieve the voiceBroadcast timeline coordinator for this event and return
/// a view to be displayed in the timeline
Expand Down Expand Up @@ -54,4 +63,11 @@ class VoiceBroadcastPlaybackProvider {
func voiceBroadcastPlaybackCoordinatorForEventIdentifier(_ eventIdentifier: String) -> VoiceBroadcastPlaybackCoordinator? {
coordinatorsForEventIdentifiers[eventIdentifier]
}

/// Pause current voice broadcast playback.
@objc public func pausePlaying() {
coordinatorsForEventIdentifiers.forEach { _, coordinator in
coordinator.pausePlaying()
}
}
}
Loading