-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#4094 - Added multiple observation on media services and a mediaServi…
…ceProvider that prevents simultaneous playback from multiple player instances.
- Loading branch information
1 parent
fc7d311
commit 667d590
Showing
8 changed files
with
219 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
Riot/Modules/Room/VoiceMessages/VoiceMessageMediaServiceProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// 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 { | ||
|
||
private let audioPlayers: NSHashTable<VoiceMessageAudioPlayer> | ||
private let audioRecorders: NSHashTable<VoiceMessageAudioRecorder> | ||
|
||
@objc public static let sharedProvider = VoiceMessageMediaServiceProvider() | ||
|
||
private override init() { | ||
audioPlayers = NSHashTable<VoiceMessageAudioPlayer>(options: .weakMemory) | ||
audioRecorders = NSHashTable<VoiceMessageAudioRecorder>(options: .weakMemory) | ||
} | ||
|
||
@objc func audioPlayer() -> VoiceMessageAudioPlayer { | ||
let audioPlayer = VoiceMessageAudioPlayer() | ||
audioPlayer.registerDelegate(self) | ||
audioPlayers.add(audioPlayer) | ||
return audioPlayer | ||
} | ||
|
||
@objc func audioRecorder() -> VoiceMessageAudioRecorder { | ||
let audioRecorder = VoiceMessageAudioRecorder() | ||
audioRecorder.registerDelegate(self) | ||
audioRecorders.add(audioRecorder) | ||
return audioRecorder | ||
} | ||
|
||
@objc func pauseAllServices() { | ||
pauseAllServicesExcept(nil) | ||
} | ||
|
||
// MARK: - VoiceMessageAudioPlayerDelegate | ||
|
||
func audioPlayerDidStartPlaying(_ audioPlayer: VoiceMessageAudioPlayer) { | ||
pauseAllServicesExcept(audioPlayer) | ||
} | ||
|
||
// MARK: - VoiceMessageAudioRecorderDelegate | ||
|
||
func audioRecorderDidStartRecording(_ audioRecorder: VoiceMessageAudioRecorder) { | ||
pauseAllServicesExcept(audioRecorder) | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private func pauseAllServicesExcept(_ service: AnyObject?) { | ||
for audioPlayer in audioPlayers.allObjects { | ||
if audioPlayer === service { | ||
break | ||
} | ||
|
||
audioPlayer.pause() | ||
} | ||
|
||
for audioRecoder in audioRecorders.allObjects { | ||
if audioRecoder === service { | ||
break | ||
} | ||
|
||
audioRecoder.stopRecording() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.