Skip to content
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

Game starts lagging after 5 minutes of playback on all devices. #16

Open
mayank-yogify opened this issue Jan 20, 2025 · 3 comments
Open

Comments

@mayank-yogify
Copy link

mayank-yogify commented Jan 20, 2025

Game starts lagging after 5 minutes of playback on all devices, On low end devices lags start faster and high end devices it takes some time. Please fix this issue and teach us about the same.

@mayank-yogify
Copy link
Author

Bro Audio player is leaking, like Bullet sound leaks , Please change or optimize audio player. Suggest quick solution soon.

@mayank-yogify
Copy link
Author

I got the solution, Use soundpool: ^2.4.1 Library instead of Flame audio. to play quick fire sounds.
You can use Flame soundpool option too but in android Tv it's not working properly,
Also dont use ogg audio files , in IOS it's not supported instead use wav , it is uncompressed audio file and faster to decode on low end devices . Thank You.

@mayank-yogify
Copy link
Author

//Here is the update code of AudioPlayerCoponent with sound pool

class AudioPlayerComponent extends Component
with HasGameReference {

static bool soundEffect = true;

static final Map<String, int> _audioPools = {};

static final audiosForPool = ['assets/audio/laser1.wav', 'assets/audio/powerUp6.wav', 'assets/audio/laserSmall_001.wav'];
static late Soundpool pool;

// This method initializes the pool for a specific sound file if it hasn't been initialized yet.
static Future _initAudioPool() async{
final SoundpoolOptions _soundpoolOptions = SoundpoolOptions(
maxStreams: 5, // Allow up to 10 simultaneous streams
streamType: StreamType.notification,
);
pool = Soundpool.fromOptions(options: _soundpoolOptions);

for (var item in audiosForPool) {
  await rootBundle.load(item).then((ByteData soundData) async {
       _audioPools[item] =  await   pool.load(soundData);
  });
}

}

@OverRide
Future? onLoad() async {
FlameAudio.bgm.initialize();

_initAudioPool();

try {
  await FlameAudio.audioCache.load(
    '9. Space Invaders.wav',
  );
} catch (_) {
  // ignore: avoid_print
  print('Missing VOiD1 Gaming music pack: '
      'https://void1gaming.itch.io/free-synthwave-music-pack '
      'See assets/audio/README.md for more information.');
}

return super.onLoad();

}

void playBgm(String filename) {
if (!FlameAudio.audioCache.loadedFiles.containsKey(filename)) return;

if (game.buildContext != null) {
  soundEffect = Provider
      .of<Settings>(game.buildContext!, listen: false)
      .soundEffects;

  if (Provider
      .of<Settings>(game.buildContext!, listen: false)
      .backgroundMusic) {
    FlameAudio.bgm.play(filename);
  }
}

}

static Future playSfx(String filename) async {
if (soundEffect) {
// Wait for the pool to be ready and play the sound
await pool.play(_audioPools["assets/audio/$filename"]??-1);
}
}

void stopBgm() {
FlameAudio.bgm.stop();
pool.dispose();
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant