From 26deec9485b9072477224cd872039fab6d00eedb Mon Sep 17 00:00:00 2001 From: himanshu Date: Mon, 2 Dec 2024 17:02:13 +0530 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Add=20a=20method=20to=20pau?= =?UTF-8?q?se=20all=20players=20at=20once?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt | 7 ++++--- .../com/simform/audio_waveforms/AudioWaveformsPlugin.kt | 7 +++++++ .../src/main/kotlin/com/simform/audio_waveforms/Utils.kt | 1 + ios/Classes/AudioPlayer.swift | 6 ++++-- ios/Classes/SwiftAudioWaveformsPlugin.swift | 6 ++++++ ios/Classes/Utils.swift | 1 + lib/src/base/audio_waveforms_interface.dart | 5 +++++ lib/src/base/constants.dart | 1 + lib/src/controllers/player_controller.dart | 7 +++++++ 10 files changed, 37 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcd46a3d..fd6f408b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fixed [#364](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/pull/364) - seekTo position issue where onDrag of waveform at initial position first wave outside the seekLine. - Fixed [#301](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/301) - Cannot catch error of preparePlayer - Fixes [#228](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/228) - Added feature to setReleaseMode for player controller. +- Fixes [#325](https://github.com/SimformSolutionsPvtLtd/audio_waveforms/issues/325) - Added feature to pause all player controller at once. ## 1.1.1 diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt b/android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt index 3b937cb7..281943c4 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/AudioPlayer.kt @@ -140,15 +140,16 @@ class AudioPlayer( } - fun pause(result: MethodChannel.Result) { + fun pause(result: MethodChannel.Result, isPauseAllPlayer: Boolean = false) { try { stopListening() player?.pause() - result.success(true) + if(!isPauseAllPlayer) { + result.success(true) + } } catch (e: Exception) { result.error(Constants.LOG_TAG, "Failed to pause the player", e.toString()) } - } fun release(result: MethodChannel.Result) { diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt b/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt index c3811881..e69ddd42 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/AudioWaveformsPlugin.kt @@ -202,6 +202,13 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } } + Constants.pauseAllPlayers -> { + for ((key,_) in audioPlayers){ + audioPlayers[key]?.pause(result,true) + } + result.success(true) + } + else -> result.notImplemented() } } diff --git a/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt b/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt index 63977db8..d3d02371 100644 --- a/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt +++ b/android/src/main/kotlin/com/simform/audio_waveforms/Utils.kt @@ -69,6 +69,7 @@ object Constants { const val resultFilePath = "resultFilePath" const val resultDuration = "resultDuration" + const val pauseAllPlayers = "pauseAllPlayers" } enum class FinishMode(val value: Int) { diff --git a/ios/Classes/AudioPlayer.swift b/ios/Classes/AudioPlayer.swift index d9094b8c..0569e9bc 100644 --- a/ios/Classes/AudioPlayer.swift +++ b/ios/Classes/AudioPlayer.swift @@ -91,10 +91,12 @@ class AudioPlayer: NSObject, AVAudioPlayerDelegate { } - func pausePlayer(result: @escaping FlutterResult) { + func pausePlayer(result: @escaping FlutterResult, isPauseAllPlayer: Bool = false) { stopListening() player?.pause() - result(true) + if(!isPauseAllPlayer){ + result(true) + } } func stopPlayer(result: @escaping FlutterResult) { diff --git a/ios/Classes/SwiftAudioWaveformsPlugin.swift b/ios/Classes/SwiftAudioWaveformsPlugin.swift index 65724f57..284eb198 100644 --- a/ios/Classes/SwiftAudioWaveformsPlugin.swift +++ b/ios/Classes/SwiftAudioWaveformsPlugin.swift @@ -147,6 +147,12 @@ public class SwiftAudioWaveformsPlugin: NSObject, FlutterPlugin { } else { result(FlutterError(code: Constants.audioWaveforms, message: "Can not get waveform data", details: "Player key is null")) } + case Constants.pauseAllPlayers: + for(playerKey,_) in audioPlayers{ + audioPlayers[playerKey]?.pausePlayer(result: result,isPauseAllPlayer: true) + } + result(true) + break default: result(FlutterMethodNotImplemented) break diff --git a/ios/Classes/Utils.swift b/ios/Classes/Utils.swift index 04ceda42..7c71159c 100644 --- a/ios/Classes/Utils.swift +++ b/ios/Classes/Utils.swift @@ -63,6 +63,7 @@ struct Constants { static let overrideAudioSession = "overrideAudioSession" static let resultFilePath = "resultFilePath" static let resultDuration = "resultDuration" + static let pauseAllPlayers = "pauseAllPlayers" } diff --git a/lib/src/base/audio_waveforms_interface.dart b/lib/src/base/audio_waveforms_interface.dart index c3691773..26a93da8 100644 --- a/lib/src/base/audio_waveforms_interface.dart +++ b/lib/src/base/audio_waveforms_interface.dart @@ -203,6 +203,11 @@ class AudioWaveformsInterface { return result ?? false; } + Future pauseAllPlayers() async { + var result = await _methodChannel.invokeMethod(Constants.pauseAllPlayers); + return result ?? false; + } + Future setMethodCallHandler() async { _methodChannel.setMethodCallHandler((call) async { switch (call.method) { diff --git a/lib/src/base/constants.dart b/lib/src/base/constants.dart index 885e274a..309b9510 100644 --- a/lib/src/base/constants.dart +++ b/lib/src/base/constants.dart @@ -39,6 +39,7 @@ class Constants { static const String current = "current"; static const String onCurrentDuration = "onCurrentDuration"; static const String stopAllPlayers = "stopAllPlayers"; + static const String pauseAllPlayers = "pauseAllPlayers"; static const String onDidFinishPlayingAudio = "onDidFinishPlayingAudio"; static const String extractWaveformData = "extractWaveformData"; static const String noOfSamples = "noOfSamples"; diff --git a/lib/src/controllers/player_controller.dart b/lib/src/controllers/player_controller.dart index 25a723e0..a2dd99c9 100644 --- a/lib/src/controllers/player_controller.dart +++ b/lib/src/controllers/player_controller.dart @@ -324,6 +324,13 @@ class PlayerController extends ChangeNotifier { PlatformStreams.instance.playerControllerFactory.clear(); } + /// This method is to pause all players at once. + /// + /// This method will only pause the players it will not release the resources. + Future pauseAllPlayers() async { + await AudioWaveformsInterface.instance.pauseAllPlayers(); + } + /// Sets [_shouldRefresh] flag with provided boolean parameter. void _setRefresh(bool refresh) { _shouldRefresh = refresh;