Files
August 762e0ad842 feat(audio): Set audio context AudioContextConfigFocus.mixWithOthers by default (#3483)
# Description

AudioPlayers used to mix their sounds with other audio on the device,
which is not the proposed experience on Mobile devices. So this behavior
was changed in AudioPlayers, but it wasn't enforced due to a bug.
Obviously this behavior is not wanted for Games as multiple sounds
should play simultaneously, so this recovers the old functionality
again.

## Checklist
<!--
Before you create this PR confirm that it meets all requirements listed
below by checking the
relevant checkboxes with `[x]`. If some checkbox is not applicable, mark
it as `[-]`.
-->

- [x] I have followed the [Contributor Guide] when preparing my PR.
- [ ] I have updated/added tests for ALL new/updated/fixed
functionality.
- [ ] I have updated/added relevant documentation in `docs` and added
dartdoc comments with `///`.
- [x] I have updated/added relevant examples in `examples` or `docs`.

Not sure, if I can test this properly, as it's on a system level. I
could ensure the values are set though (?)

## Breaking Change?
I'm actually not sure, if this is breaking, I think it's not, as
`audioplayers` used to work like `mixWithOthers` due to a bug, but is
not anymore.

- [ ] Yes, this PR is a breaking change.
- [x] No, this PR is not a breaking change.

<!--
### Migration instructions

If the PR is breaking, uncomment this header and add instructions for
how to migrate from the
currently released version in-between the two following tags:
-->
<!-- End of exclude from commit message -->

---------

Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
2025-02-10 08:46:54 +00:00

86 lines
2.1 KiB
Dart

import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame/palette.dart';
import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/widgets.dart' hide Animation;
void main() {
runApp(GameWidget(game: AudioGame()));
}
/// This example game showcases three possible use cases:
///
/// 1. Use the static FlameAudio class to easily fire a sfx using the default
/// configs for the button tap.
/// 2. Uses a custom AudioPool for extremely efficient audio loading and pooling
/// for tapping elsewhere.
/// 3. Uses the Bgm utility for background music.
class AudioGame extends FlameGame with TapDetector {
static final Paint black = BasicPalette.black.paint();
static final Paint gray = const PaletteEntry(Color(0xFFCCCCCC)).paint();
static final TextPaint text = TextPaint(
style: TextStyle(color: BasicPalette.white.color),
);
late AudioPool pool;
@override
Future<void> onLoad() async {
pool = await FlameAudio.createPool(
'sfx/fire_2.mp3',
minPlayers: 3,
maxPlayers: 4,
);
startBgmMusic();
}
Rect get button => Rect.fromLTWH(20, size.y - 300, size.x - 40, 200);
Future<void> startBgmMusic() async {
await FlameAudio.bgm.initialize();
await FlameAudio.bgm.play('music/bg_music.ogg');
}
void fireOne() {
FlameAudio.play('sfx/fire_1.mp3');
}
void fireTwo() {
pool.start();
}
@override
void render(Canvas canvas) {
super.render(canvas);
canvas.drawRect(size.toRect(), black);
text.render(
canvas,
'(click anywhere for 1)',
Vector2(size.x / 2, 200),
anchor: Anchor.topCenter,
);
canvas.drawRect(button, gray);
text.render(
canvas,
'click here for 2',
Vector2(size.x / 2, size.y - 200),
anchor: Anchor.bottomCenter,
);
}
@override
void onTapDown(TapDownInfo info) {
if (button.containsPoint(info.eventPosition.widget)) {
fireTwo();
} else {
fireOne();
}
}
}