audioplayers icon indicating copy to clipboard operation
audioplayers copied to clipboard

Audio played with 'AudioCache' is delayed the first time even if preloaded.

Open wujek-srujek opened this issue 3 years ago • 4 comments

The first time audio is played with AudioCache, even if IAudioCache.loadAll is used beforehand, is played with a slight delay; second and further attempts do not have any delay.

Description

It seems like loadAll or load are not enough the first time. A possible workaround would be to play each sound with volume set to 0 to 'worm up', but it is a pretty ugly hack.

Code to Reproduce

import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await initPlayer();

  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: TextButton(
            onPressed: playAlarm,
            child: Text('Play'),
          ),
        ),
      ),
    );
  }
}

// ----------
  
const _timerAlarmAudioFile = 'timer_alarm.mp3';

late final AudioCache _audioCache;

Future<void> initPlayer() {
  _audioCache = AudioCache(
    prefix: 'assets/audio/',
    fixedPlayer: AudioPlayer(),
  );

  return _audioCache.loadAll([_timerAlarmAudioFile]);
}

void playAlarm() {
  _audioCache.play(_timerAlarmAudioFile, mode: PlayerMode.LOW_LATENCY);
}

Platforms

  • iPhone Xs + iOS 14.5.
  • Pixel 3XL + Android 11.
  • Flutter version 2.2.1 stable channel.
  • audioplayers version: 0.19.0
  • App run in release mode run with the following command: flutter run --release -d <device id>
  • The sound I'm playing is this: https://www.zedge.net/ringtone/99a9a13c-2881-32ff-aa8b-84302fb3532c

wujek-srujek avatar Jun 27 '21 16:06 wujek-srujek

same here, I'm trying to get sounds to seamlessly run one after the other, but there is this delay..

mzohren avatar Sep 14 '21 19:09 mzohren

I have the same issue, the player cannot play a sound instantly even if the needed sound is preloaded using AudioCache

alectogeek avatar Oct 27 '21 15:10 alectogeek

I also had the same problem

phamdanh avatar Oct 31 '21 03:10 phamdanh

I'm also facing the same problem

KuoyHuot avatar Nov 20 '21 03:11 KuoyHuot

Can you retest with audioplayers: ^1.1.0 ?

Gustl22 avatar Sep 27 '22 15:09 Gustl22

Marking as stale. May have a look at #1469 or we may need to replace the Android implementation with ExoPlayer. Feel free to open a new issue with current AP version.

Gustl22 avatar Apr 24 '23 11:04 Gustl22