BaseAudioChannel class abstract
BaseAudioChannel
An abstract base that encapsulates shared logic for all audio channels (e.g., BGM, SFX, Voice). Subclasses provide concrete configuration for playerMode and releaseMode.
Core Responsibilities:
- Maintain a single
AudioPlayerper channel - Control playback:
play,playFromSource,pause,resume,stop - Manage volume & mute state with cached target restoration
- Expose reactive
isPlayingvia onIsPlayingChanged - Provide tween-based volume fading with custom Curve
- Wrap arbitrary actions in fade-out → action → fade-in via withFade
Configurable:
- Override playerMode and releaseMode
- Swap how paths are resolved via changeSource
Usage Example:
class SfxChannel extends BaseAudioChannel {
SfxChannel() : super('sfx_channel');
@override PlayerMode get playerMode => PlayerMode.lowLatency;
@override ReleaseMode get releaseMode => ReleaseMode.stop;
}
final sfx = SfxChannel();
await sfx.play('assets/sfx/click.mp3'); // assets by default
sfx.changeSource((p) => UrlSource(p)); // swap loader at runtime
await sfx.play('https://example.com/alert.mp3');
await sfx.withFade(() async { // wrap any action in fades
await sfx.play('assets/sfx/alert.wav');
});
- Implementers
Constructors
- BaseAudioChannel(String playerId)
Properties
- currentAudioContext → AudioContext?
-
Current audio context getter (nullable if never set).
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasMultipleConcurrentPlayers → bool
-
Whether this channel may spawn multiple concurrent players.
Mixins like SFX pooling should override this automatically.
no setter
- isActive → bool
-
Active Flag
no setter
- isMuted → bool
-
Muted Flag
no setter
- isPlaying → bool
-
Current Status
no setter
-
onDurationChanged
→ Stream<
Duration> -
Duration Stream
no setter
-
onIsPlayingChanged
→ Stream<
bool> -
Reactive
isPlayingStreamno setter -
onPlayerComplete
→ Stream<
void> -
Completion Event
no setter
-
onPositionChanged
→ Stream<
Duration> -
Position Stream
no setter
- playerId → String
-
Stable Identifier
final
- playerMode → PlayerMode
-
Player Mode
no setter
- releaseMode → ReleaseMode
-
Release Mode
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- volume → double
-
Current Volume (0.0 → 1.0)
no setter
Methods
-
activate(
) → void - Activate
-
changeSource(
AudioSourceBuilder builder) → void - Swap Loader
-
deactivate(
) → void - Deactivate
-
disableLifecycle(
) → void - Disable lifecycle handling for this channel.
-
dispose(
) → Future< void> - Dispose
-
enableLifecycle(
[ChannelLifecycleConfig cfg = const ChannelLifecycleConfig()]) → void - Enable per-channel lifecycle handling
-
ensureWaitSupported(
String methodName) → void - Throws if waiting semantics are unsupported for this channel.
-
fadeIn(
{Duration duration = const Duration(seconds: 2), Curve curve = Curves.easeInOut}) → Future< void> - Fade In
-
fadeInVolume(
FadePreset preset) → Future< void> - Helper: Fade In via FadePreset
-
fadeOut(
{Duration duration = const Duration(seconds: 2), Curve curve = Curves.easeOut}) → Future< void> - Fade Out
-
fadeOutVolume(
FadePreset preset) → Future< void> - Helper: Fade Out via FadePreset
-
fadeTo(
double targetVolume, {Duration duration = const Duration(seconds: 2), Curve curve = Curves.linear}) → Future< void> - Fade To Target Volume
-
getDuration(
) → Future< Duration?> - Current Media Duration
-
mute(
) → Future< void> - Mute (volume → 0.0)
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onStateChanged(
PlayerState state) → void - Subclass Hook: Player State Changed
-
pause(
) → Future< void> - Pause
-
play(
String path) → Future< void> - Play by Path
-
playAndWait(
String path, {Duration startTimeout = const Duration(seconds: 3), Duration timeout = const Duration(minutes: 2), bool forceStopOnTimeout = true}) → Future< bool> - Convenience: play a path and wait for completion/stop
-
playFromSource(
Source source) → Future< void> - Play from Source
-
playFromSourceAndWait(
Source source, {Duration startTimeout = const Duration(seconds: 3), Duration timeout = const Duration(minutes: 2), bool forceStopOnTimeout = true}) → Future< bool> - Convenience: play a Source and wait for completion/stop
-
resolveSource(
String path) → Source - Resolve a path into a Source using the current builder (assets/files/urls). Override via changeSource to switch the strategy at runtime.
-
resume(
) → Future< void> - Resume
-
seek(
Duration position) → Future< void> - Seek
-
setAudioContext(
AudioContext audioContext) → Future< void> - Apply Platform Audio Context
-
setVolume(
double volume) → Future< void> - Set Volume
-
stop(
) → Future< void> - Stop
-
toggleActive(
) → void - Toggle Active
-
toString(
) → String -
A string representation of this object.
inherited
-
unmute(
) → Future< void> - Unmute (volume → 1.0)
-
waitUntilStarted(
{Duration timeout = const Duration(seconds: 3)}) → Future< bool> -
Wait until playback starts (first
true) -
waitUntilStopped(
{Duration startTimeout = const Duration(seconds: 3), Duration timeout = const Duration(minutes: 2), bool forceStopOnTimeout = true}) → Future< bool> - Wait until playback stops/completes
-
withFade(
Future< void> action(), {FadePreset fadeOut = FadePreset.fast, FadePreset fadeIn = FadePreset.normal}) → Future<void> - withFade
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited