2021-06-23 14:52:08 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
@objc public class VoiceMessageMediaServiceProvider: NSObject, VoiceMessageAudioPlayerDelegate, VoiceMessageAudioRecorderDelegate {
|
|
|
|
|
2021-07-12 12:30:48 +00:00
|
|
|
private let audioPlayers: NSHashTable<VoiceMessageAudioPlayer>
|
|
|
|
private let audioRecorders: NSHashTable<VoiceMessageAudioRecorder>
|
|
|
|
|
2021-06-23 14:52:08 +00:00
|
|
|
@objc public static let sharedProvider = VoiceMessageMediaServiceProvider()
|
|
|
|
|
|
|
|
private override init() {
|
2021-07-12 12:30:48 +00:00
|
|
|
audioPlayers = NSHashTable<VoiceMessageAudioPlayer>(options: .weakMemory)
|
|
|
|
audioRecorders = NSHashTable<VoiceMessageAudioRecorder>(options: .weakMemory)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func audioPlayer() -> VoiceMessageAudioPlayer {
|
|
|
|
let audioPlayer = VoiceMessageAudioPlayer()
|
2021-06-23 14:52:08 +00:00
|
|
|
audioPlayer.registerDelegate(self)
|
2021-07-12 12:30:48 +00:00
|
|
|
audioPlayers.add(audioPlayer)
|
|
|
|
return audioPlayer
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func audioRecorder() -> VoiceMessageAudioRecorder {
|
|
|
|
let audioRecorder = VoiceMessageAudioRecorder()
|
2021-06-23 14:52:08 +00:00
|
|
|
audioRecorder.registerDelegate(self)
|
2021-07-12 12:30:48 +00:00
|
|
|
audioRecorders.add(audioRecorder)
|
|
|
|
return audioRecorder
|
2021-06-23 14:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func stopAllServices() {
|
2021-07-12 12:30:48 +00:00
|
|
|
stopAllServicesExcept(nil)
|
2021-06-23 14:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - VoiceMessageAudioPlayerDelegate
|
|
|
|
|
|
|
|
func audioPlayerDidStartPlaying(_ audioPlayer: VoiceMessageAudioPlayer) {
|
2021-07-12 12:30:48 +00:00
|
|
|
stopAllServicesExcept(audioPlayer)
|
2021-06-23 14:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - VoiceMessageAudioRecorderDelegate
|
|
|
|
|
|
|
|
func audioRecorderDidStartRecording(_ audioRecorder: VoiceMessageAudioRecorder) {
|
2021-07-12 12:30:48 +00:00
|
|
|
stopAllServicesExcept(audioRecorder)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private func stopAllServicesExcept(_ service: AnyObject?) {
|
|
|
|
for audioPlayer in audioPlayers.allObjects {
|
|
|
|
if audioPlayer === service {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
audioPlayer.pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
for audioRecoder in audioRecorders.allObjects {
|
|
|
|
if audioRecoder === service {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
audioRecoder.stopRecording()
|
|
|
|
}
|
2021-06-23 14:52:08 +00:00
|
|
|
}
|
|
|
|
}
|