0

I've got a problem with my first flutter application since one week.

I was trying to build an app that consist of clicking on some movie phrases and the app play the corresponding audio phrase. Everything is in the app, the only audio phrase (for the moment) is in "assets/audio/soundname.mp3".

The problem is, when I click on the textual phrase, I wait between 8 and 9 seconds before the audio phrase starts to play.

So I've heard about using the audio_cache feature to "preload" the audio during app launching, and be able to get an instant response when I click on the text.

But it doesn't work.

I did everything right like importing, managing assets in pub yaml, checking folders etc, but I still have the problem.

I'm not accommodate to stack overflow, I know they are more strict than in Reddit in terms of writing an issue in a good way, so be gentle with ahah.

Anyway, this is an extract of my code, from the importation of packages, to the playing function :

import 'package:flutter/material.dart';

import 'package:audioplayers/audioplayers.dart';


class RepliqueWidget extends StatefulWidget {
  final int backgroundColor;
  final int shadowColor;
  String imageAssetPath;
  String nomPersonnage;
  String texteReplique;
  String audio;

  RepliqueWidget(
      {Key? key,
      required this.backgroundColor,
      required this.shadowColor,
      required this.imageAssetPath,
      required this.nomPersonnage,
      required this.texteReplique,
      required this.audio})
      : super(key: key);

  @override
  _RepliqueWidgetState createState() => _RepliqueWidgetState();
}

class _RepliqueWidgetState extends State<RepliqueWidget> {
  
  static AudioCache player = new AudioCache();

  @override
  void initState() {
    super.initState();
    player.load(widget.audio);
  }

  @override
  Widget build(BuildContext context) {
    final phraseAudioPath = widget.audio;
    return GestureDetector(
      onTap: () => player.play(phraseAudioPath),
      child: Container(

I've already check like 5 posts about small audio delays with flutter but nothing related to my problem.

Thx in advance !

remsB
  • 21
  • 6
  • 1
    Have you tried with a different file? This is not a caching issue. 8-9 seconds delay is too long even for a large file over the Internet. A local file should start playing immediately. – Andrei Volgin Mar 23 '23 at 14:48
  • oh my god. Man, like, usually in my code I relook to everything to avoid any stupid errors you know. But in this case, I've, like, didn't even thought about a problem in the sound, like his size or his format (483kb and MP3). I've randomly downloaded a 23 seconds sound (my initial sound is less in terms of time) and the sound started immediately. Same size but started immediately. So maybe it's a codec problem or something ? Anyway, thx a lot for thinking about a sound problem, you solved the issue ! – remsB Mar 23 '23 at 15:26
  • 1
    It's also possible that there are 8 seconds of silence in that file :) You can add a listener to your player and print the elapsed time. – Andrei Volgin Mar 23 '23 at 15:46
  • You know what ? I want to kill myself. I had 3 versions, 3 cut of this sound, and I've put the wrong and uncut version, with 8 seconds of silence, in my project ... Sorry for wasting your time guys. I was too focus on a possible code problem, without thinking about the possibility that I could have put the wrong version of the song in the code. – remsB Mar 23 '23 at 22:03

0 Answers0