Skip to content

Commit

Permalink
fix: start music on button press (#580)
Browse files Browse the repository at this point in the history
* fix: start music on get started button press and remove unused code

* fix: remove extra space
  • Loading branch information
marwfair authored Jun 19, 2024
1 parent 73e1782 commit 2f20697
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 278 deletions.
96 changes: 41 additions & 55 deletions lib/audio/audio_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class AudioController {
}

/// Enables the [AudioController] to track changes to settings.
/// Namely, when any of [SettingsController.muted],
/// [SettingsController.musicOn] or [SettingsController.soundsOn] changes,
/// Namely, when [SettingsController.muted] changes,
/// the audio controller will act accordingly.
void attachSettings(SettingsController settingsController) {
if (_settings == settingsController) {
Expand All @@ -70,19 +69,37 @@ class AudioController {
final oldSettings = _settings;
if (oldSettings != null) {
oldSettings.muted.removeListener(_mutedHandler);
oldSettings.musicOn.removeListener(_musicOnHandler);
oldSettings.soundsOn.removeListener(_soundsOnHandler);
}

_settings = settingsController;

// Add handlers to the new settings controller
settingsController.muted.addListener(_mutedHandler);
settingsController.musicOn.addListener(_musicOnHandler);
settingsController.soundsOn.addListener(_soundsOnHandler);
}

Future<void> _configureBackgroundMusicPlayer() async {
if (_musicPlayer.source != null) {
return;
}

await _musicPlayer.setSource(
AssetSource(
_replaceUrl(
Assets.music.backgroundMusicCrossword,
),
),
);

await _musicPlayer.setVolume(
0.3,
);
}

Future<void> startMusic() async {
await _configureBackgroundMusicPlayer();

if (!settingsController.muted.value && settingsController.musicOn.value) {
_startMusic();
if (_settings != null && !_settings!.muted.value) {
await _playBackgroundMusic();
}
}

Expand Down Expand Up @@ -113,8 +130,7 @@ class AudioController {
/// Plays a single sound effect.
///
/// The controller will ignore this call when the attached settings'
/// [SettingsController.muted] is `true` or if its
/// [SettingsController.soundsOn] is `false`.
/// [SettingsController.muted] is `true`.
void playSfx(String sfx) {
if (!Assets.music.values.contains(sfx)) {
throw ArgumentError.value(
Expand All @@ -128,10 +144,6 @@ class AudioController {
if (muted) {
return;
}
final soundsOn = _settings?.soundsOn.value ?? false;
if (!soundsOn) {
return;
}

_sfxPlayers[_currentSfxPlayer].play(
AssetSource(_replaceUrl(sfx)),
Expand All @@ -142,7 +154,7 @@ class AudioController {
Future<void> _loopSound(void _) async {
//Loop the sound forever.

await _playBackgroundSound(Assets.music.backgroundMusicCrossword);
await _playBackgroundMusic();
}

void _handleAppLifecycle() {
Expand All @@ -152,7 +164,7 @@ class AudioController {
_stopAllSound();

case AppLifecycleState.resumed:
if (!_settings!.muted.value && _settings!.musicOn.value) {
if (!_settings!.muted.value) {
_resumeMusic();
}

Expand All @@ -163,35 +175,20 @@ class AudioController {
}
}

void _musicOnHandler() {
if (_settings!.musicOn.value) {
// Music got turned on.
if (!_settings!.muted.value) {
_resumeMusic();
}
} else {
// Music got turned off.
_stopMusic();
}
}

void _mutedHandler() {
if (_settings!.muted.value) {
// All sound just got muted.
_stopAllSound();
} else {
// All sound just got un-muted.
if (_settings!.musicOn.value) {
if (_musicPlayer.source != null) {
_resumeMusic();
}
}
}

Future<void> _playBackgroundSound(String sound) async {
await _musicPlayer.play(
AssetSource(_replaceUrl(sound)),
volume: 0.3,
);
Future<void> _playBackgroundMusic() async {
await _musicPlayer.resume();
}

Future<void> _resumeMusic() async {
Expand All @@ -200,33 +197,28 @@ class AudioController {
try {
await _musicPlayer.resume();
} catch (e) {
await _playBackgroundSound(Assets.music.backgroundMusicCrossword);
await _musicPlayer.play(
AssetSource(
_replaceUrl(
Assets.music.backgroundMusicCrossword,
),
),
volume: 0.3,
);
}
case PlayerState.stopped:
await _musicPlayer.stop();
await _playBackgroundSound(Assets.music.backgroundMusicCrossword);
await _playBackgroundMusic();

case PlayerState.playing:
break;
case PlayerState.completed:
await _playBackgroundSound(Assets.music.backgroundMusicCrossword);
await _playBackgroundMusic();
case PlayerState.disposed:
break;
}
}

void _soundsOnHandler() {
for (final player in _sfxPlayers) {
if (player.state == PlayerState.playing) {
player.stop();
}
}
}

void _startMusic() {
_playBackgroundSound(Assets.music.backgroundMusicCrossword);
}

void _stopAllSound() {
if (_musicPlayer.state == PlayerState.playing) {
_musicPlayer.pause();
Expand All @@ -235,10 +227,4 @@ class AudioController {
player.stop();
}
}

void _stopMusic() {
if (_musicPlayer.state == PlayerState.playing) {
_musicPlayer.pause();
}
}
}
14 changes: 1 addition & 13 deletions lib/settings/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/foundation.dart';

/// An class that holds settings like [muted] or [musicOn],
/// A class that holds settings like [muted].
class SettingsController {
/// Creates a new instance of [SettingsController] .
SettingsController();
Expand All @@ -12,19 +12,7 @@ class SettingsController {
/// and sound.
ValueNotifier<bool> muted = ValueNotifier(false);

ValueNotifier<bool> soundsOn = ValueNotifier(true);

ValueNotifier<bool> musicOn = ValueNotifier(true);

void toggleMusicOn() {
musicOn.value = !musicOn.value;
}

void toggleMuted() {
muted.value = !muted.value;
}

void toggleSoundsOn() {
soundsOn.value = !soundsOn.value;
}
}
2 changes: 2 additions & 0 deletions lib/welcome/view/welcome_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:io_crossword/assets/assets.gen.dart';
import 'package:io_crossword/audio/audio.dart';
import 'package:io_crossword/challenge/challenge.dart';
Expand Down Expand Up @@ -112,6 +113,7 @@ class WelcomeBody extends StatelessWidget {
const WelcomeBody({super.key});

void _onGetStarted(BuildContext context) {
context.read<AudioController>().startMusic();
Navigator.of(context).push<void>(TeamSelectionPage.route());
}

Expand Down
Loading

0 comments on commit 2f20697

Please sign in to comment.