-
Notifications
You must be signed in to change notification settings - Fork 162
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
feat: ✨ Add a method to pause all players at once #372
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -357,6 +357,13 @@ playerController.release(); | |
playerController.stopAllPlayer(); | ||
``` | ||
There could be any number of players but you can just call this function from any **one** player and it will stop all the players. | ||
|
||
#### Pausing players all at once | ||
```dart | ||
playerController.pauseAllPlayers(); | ||
``` | ||
There could be any number of players but you can just call this function from any **one** player and it will pause all the players. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just write, this function works similar to stopAllPlayer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jay-simformsolutions update to this |
||
|
||
#### Disposing the controller | ||
```dart | ||
playerController.dispose(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,26 +129,19 @@ class AudioPlayer( | |
} | ||
} | ||
|
||
fun stop(result: MethodChannel.Result) { | ||
fun stop() { | ||
stopListening() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add try catch |
||
if (playerListener != null) { | ||
player?.removeListener(playerListener!!) | ||
} | ||
isPlayerPrepared = false | ||
player?.stop() | ||
result.success(true) | ||
} | ||
|
||
|
||
fun pause(result: MethodChannel.Result) { | ||
try { | ||
stopListening() | ||
player?.pause() | ||
result.success(true) | ||
} catch (e: Exception) { | ||
result.error(Constants.LOG_TAG, "Failed to pause the player", e.toString()) | ||
} | ||
|
||
fun pause() { | ||
stopListening() | ||
player?.pause() | ||
Comment on lines
+143
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add try catch |
||
} | ||
|
||
fun release(result: MethodChannel.Result) { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,7 +7,6 @@ import android.os.Build | |||||
import android.util.Log | ||||||
import androidx.annotation.NonNull | ||||||
import androidx.annotation.RequiresApi | ||||||
|
||||||
import io.flutter.embedding.engine.plugins.FlutterPlugin | ||||||
import io.flutter.embedding.engine.plugins.activity.ActivityAware | ||||||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding | ||||||
|
@@ -18,7 +17,8 @@ import io.flutter.plugin.common.MethodChannel.Result | |||||
import java.io.File | ||||||
import java.io.IOException | ||||||
import java.text.SimpleDateFormat | ||||||
import java.util.* | ||||||
import java.util.Date | ||||||
import java.util.Locale | ||||||
|
||||||
|
||||||
/** AudioWaveformsPlugin */ | ||||||
|
@@ -102,7 +102,8 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { | |||||
Constants.stopPlayer -> { | ||||||
val key = call.argument(Constants.playerKey) as String? | ||||||
if (key != null) { | ||||||
audioPlayers[key]?.stop(result) | ||||||
audioPlayers[key]?.stop() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add try catch |
||||||
result.success(true) | ||||||
} else { | ||||||
result.error(Constants.LOG_TAG, "Player key can't be null", "") | ||||||
} | ||||||
|
@@ -111,7 +112,8 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { | |||||
Constants.pausePlayer -> { | ||||||
val key = call.argument(Constants.playerKey) as String? | ||||||
if (key != null) { | ||||||
audioPlayers[key]?.pause(result) | ||||||
audioPlayers[key]?.pause() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add try catch |
||||||
result.success(true) | ||||||
} else { | ||||||
result.error(Constants.LOG_TAG, "Player key can't be null", "") | ||||||
} | ||||||
|
@@ -187,11 +189,7 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { | |||||
} | ||||||
|
||||||
Constants.stopAllPlayers -> { | ||||||
for ((key, _) in audioPlayers) { | ||||||
audioPlayers[key]?.stop(result) | ||||||
audioPlayers[key] = null | ||||||
} | ||||||
result.success(true) | ||||||
stopAllPlayer(result) | ||||||
} | ||||||
|
||||||
Constants.finishMode -> { | ||||||
|
@@ -202,6 +200,10 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { | |||||
} | ||||||
} | ||||||
|
||||||
Constants.pauseAllPlayers -> { | ||||||
pauseAllPlayer(result) | ||||||
} | ||||||
|
||||||
else -> result.notImplemented() | ||||||
} | ||||||
} | ||||||
|
@@ -322,4 +324,27 @@ class AudioWaveformsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { | |||||
pluginBinding!!.removeRequestPermissionsResultListener(this.audioRecorder) | ||||||
} | ||||||
} | ||||||
|
||||||
private fun stopAllPlayer(result: MethodChannel.Result) { | ||||||
try { | ||||||
for ((key, _) in audioPlayers) { | ||||||
audioPlayers[key]?.stop() | ||||||
audioPlayers[key] = null | ||||||
} | ||||||
result.success(true) | ||||||
} catch (e: Exception) { | ||||||
result.error(Constants.LOG_TAG, "Failed to stop player", e.message) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
|
||||||
private fun pauseAllPlayer(result: MethodChannel.Result) { | ||||||
try { | ||||||
for ((key, _) in audioPlayers) { | ||||||
audioPlayers[key]?.pause() | ||||||
} | ||||||
result.success(true) | ||||||
} catch (e: Exception) { | ||||||
result.error(Constants.LOG_TAG, "Failed to pause player", e.message) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,7 +152,7 @@ class _WaveBubbleState extends State<WaveBubble> { | |
IconButton( | ||
onPressed: () async { | ||
controller.playerState.isPlaying | ||
? await controller.pausePlayer() | ||
? await controller.stopAllPlayers() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this change |
||
: await controller.startPlayer(); | ||
controller.setFinishMode(finishMode: FinishMode.loop); | ||
}, | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -318,10 +318,32 @@ class PlayerController extends ChangeNotifier { | |||||
/// | ||||||
/// This method will close the stream and free resources taken by all | ||||||
/// players. This method will not dispose controller. | ||||||
Future<void> stopAllPlayers() async { | ||||||
/// | ||||||
/// Returns true if all player stopped otherwise return false. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check grammar |
||||||
Future<bool> stopAllPlayers() async { | ||||||
PlatformStreams.instance.dispose(); | ||||||
await AudioWaveformsInterface.instance.stopAllPlayers(); | ||||||
PlatformStreams.instance.playerControllerFactory.clear(); | ||||||
var isAllPlayersStopped = | ||||||
await AudioWaveformsInterface.instance.stopAllPlayers(); | ||||||
if (isAllPlayersStopped) { | ||||||
PlatformStreams.instance.playerControllerFactory | ||||||
.forEach((playKey, controller) { | ||||||
controller._setPlayerState(PlayerState.stopped); | ||||||
}); | ||||||
} | ||||||
return isAllPlayersStopped; | ||||||
} | ||||||
|
||||||
/// This function works similar to stopAllPlayer. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Future<bool> pauseAllPlayers() async { | ||||||
var isAllPlayersPaused = | ||||||
await AudioWaveformsInterface.instance.pauseAllPlayers(); | ||||||
if (isAllPlayersPaused) { | ||||||
PlatformStreams.instance.playerControllerFactory | ||||||
.forEach((playKey, controller) { | ||||||
controller._setPlayerState(PlayerState.paused); | ||||||
}); | ||||||
} | ||||||
return isAllPlayersPaused; | ||||||
} | ||||||
|
||||||
/// Sets [_shouldRefresh] flag with provided boolean parameter. | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use issue link.