일모도원(日暮途遠) 개발자
[Flutter Flame] 오디오 재생시 점점 느려지는 버그 본문
반응형
flame오디오를 사용해서 총알이 발사될때 소리를 내게 했는데, 점점 소리나는 시점이 늦어지더니 어느순간 총알이 나가도 소리는 안나간다. 자세히 보니 소리가 아주 늦게 나오는것이다.
반복되는 소리는 AudioPool을 쓰라고 해서 이런씩으로 쓰고 있었다.
AudioPool? _laserPool;
_laserPool = await AudioPool.createFromAsset(
path: 'sfx_laser1.ogg',
minPlayers: 1,
maxPlayers: 5,
audioCache: FlameAudio.audioCache,
);
await _laserPool?.start()
디버깅을 해보니 음원파일은 1.2초짜리이고 재생은 0.6초 단위로 계속 재생을 시키고 있었다. maxPlayers를 5정도로 주면 될줄 알았는데,
며칠을 고생하다고 아래처럼 하니 해결되었다. flutter문서를 보면 start는 Future<StopFunction>를 반환하는데 이걸실행하면 기존에 재생중인 사운드를 중지시킨다고 한다.
Using an AudioPool
Once you’ve created an AudioPool, you can start playing sounds:
// Play the sound with default volume (1.0)
final stopFunction = await audioPool.start();
// Later, you can stop the sound if needed
await stopFunction();
The start() method returns a StopFunction that you can call to stop the sound before it completes naturally.
그래서 아래처럼 StopFunction을 받아서 start전에 이걸 먼저 실행시키니 해결되었다. 맨처음에는 StopFunction이 없으니 null이므로옵셔널로 선언해서 사용하였다.
AudioPool? _laserPool;
StopFunction? _lastLaserStopFn;
await _lastLaserStopFn?.call();
_lastLaserStopFn = await _laserPool?.start();
'Flutter > Flutter개발' 카테고리의 다른 글
[Flutter개발] Flavor만들기 (3) | 2025.07.18 |
---|---|
[Flame] 폰트색상이 빨간색에 노랑 밑줄이 발생하는 경우 (0) | 2025.05.17 |
[Flame] Flame의 Route에러 (0) | 2025.05.15 |
[Flutter개발] 화면에 표시되는 언어 변경하기. (1) | 2025.02.02 |
[플러터 개발] 위젯 2개를 한개는 정가운데 두고 하나는 오른쪽에 배치하기. (0) | 2025.01.18 |