audioplayers icon indicating copy to clipboard operation
audioplayers copied to clipboard

[v1.0.0] Can't play the same AssetSource twice in a row

Open 12people opened this issue 2 years ago • 3 comments

I want to play a short sound effect using an AssetSource several times in a row, one each second, as a countdown. This worked in v0.20.1 (with AudioCache), but doesn't work in v1.0.0 — only the first sound effect is played there.

Full Description The platform I'm running this on is macOS on ARM.

Aside from the info above, it's worth noting that playing a different AssetSource right after works.

In my app, I have a 3-second countdown to an exercise, and then a sound announcing the exercise. One second after the other, these sound effects should be played:

  • Tick
  • Tick
  • Tick
  • Gong

But instead, there's only one Tick, nothing, nothing, and then a Gong.

Code to Reproduce

import 'dart:async';

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int countdown = 3;
  final player = AudioPlayer();

  @override
  Widget build(BuildContext context) {
    final timer = Timer.periodic(const Duration(seconds: 1), _countSecond);

    return Scaffold();
  }

  void _countSecond(_) {
    if (countdown == 0) {
      player.play(AssetSource("sound_exercise.mp3"));
    } else {
      player.play(AssetSource("sound_tick.mp3"));
    }
  }
}

Files/URLs/Sources Relevant assets:

  • https://gitlab.com/enjoyingfoss/feeel/-/blob/master/assets/sound_tick.mp3
  • https://gitlab.com/enjoyingfoss/feeel/-/blob/master/assets/sound_exercise.mp3

Platforms * OS: macOS

  • OS version: v12.4
  • Device: MacBook Air (M1, 2020)
  • flutter version: 3.0.2
  • audioplayers version: 1.0.0
  • release or not release: debug

12people avatar Jun 14 '22 21:06 12people

Could you please try calling:

await player.stop();

before playing? I think I have a hunch as to way

luanpotter avatar Jun 16 '22 13:06 luanpotter

Calling await player.stop(); partly fixes it: the sounds are all played now, but the second and third "Tick" are much quieter than the initial one.

12people avatar Jun 16 '22 17:06 12people

Doesn't work for me either on v1.0.2 Also there is a noticeable click noise on playback start Downgraded to v0.20.1 meanwhile

G0retZ avatar Jun 19 '22 16:06 G0retZ

Encountered the same problem, also reduced the version to 0.20.1.

john990 avatar Sep 08 '22 07:09 john990

@G0retZ Does the click noise also appear in 0.20.1 ?

Gustl22 avatar Sep 08 '22 10:09 Gustl22

I believe I have a similar problem.

I copied this PlayerWidget() widget (https://github.com/bluefireteam/audioplayers/blob/main/packages/audioplayers/example/lib/components/player_widget.dart) from the examples repo to use in a project, and it works for the first time I hit "play"... it plays my audio file till the end, I can pause it and resume it, no issues.

However, after the audio ends, nothing happens when I hit the "play" button again (that is, I cannot play the same audio a second time). The same thing happens when I hit the "stop" button and then hit "play" again.

Could anyone help me with this one?

Thanks a lot!

fabriciocarraro avatar Jun 24 '23 16:06 fabriciocarraro

@fabriciocarraro you have to set the Release mode to stop

Gustl22 avatar Jun 26 '23 12:06 Gustl22

@fabriciocarraro you have to set the Release mode to stop

Thank you @Gustl22 !

fabriciocarraro avatar Jun 26 '23 12:06 fabriciocarraro