Skip to content

Commit

Permalink
feat: ✨ Add a method to pause all players at once
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshuGandhiSimform committed Dec 2, 2024
1 parent af6bc5a commit 26deec9
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ object Constants {

const val resultFilePath = "resultFilePath"
const val resultDuration = "resultDuration"
const val pauseAllPlayers = "pauseAllPlayers"
}

enum class FinishMode(val value: Int) {
Expand Down
6 changes: 4 additions & 2 deletions ios/Classes/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions ios/Classes/SwiftAudioWaveformsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct Constants {
static let overrideAudioSession = "overrideAudioSession"
static let resultFilePath = "resultFilePath"
static let resultDuration = "resultDuration"
static let pauseAllPlayers = "pauseAllPlayers"
}


Expand Down
5 changes: 5 additions & 0 deletions lib/src/base/audio_waveforms_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ class AudioWaveformsInterface {
return result ?? false;
}

Future<bool> pauseAllPlayers() async {
var result = await _methodChannel.invokeMethod(Constants.pauseAllPlayers);
return result ?? false;
}

Future<void> setMethodCallHandler() async {
_methodChannel.setMethodCallHandler((call) async {
switch (call.method) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/base/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
7 changes: 7 additions & 0 deletions lib/src/controllers/player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> pauseAllPlayers() async {
await AudioWaveformsInterface.instance.pauseAllPlayers();
}

/// Sets [_shouldRefresh] flag with provided boolean parameter.
void _setRefresh(bool refresh) {
_shouldRefresh = refresh;
Expand Down

0 comments on commit 26deec9

Please sign in to comment.