florent37 / Flutter-AssetsAudioPlayer

Play simultaneously music/audio from assets/network/file directly from Flutter, compatible with android / ios / web / macos, displays notifications
https://pub.dartlang.org/packages/assets_audio_player
Apache License 2.0
752 stars 357 forks source link

playing multiple audios in same time in background #763

Open mohamedsalah83 opened 1 year ago

mohamedsalah83 commented 1 year ago

Flutter Version

My version :

Lib Version

My version :

Platform (Android / iOS / web) + version

Platform : android

Describe the bug

some times when i press next or previous button another audios playing in same time in background how can i fix this ?? could anyone help please.

Small code to reproduce

import 'package:assets_audio_player/assets_audio_player.dart'; import 'package:flutter/material.dart'; import 'package:palette_generator/palette_generator.dart'; import 'package:sleek_circular_slider/sleek_circular_slider.dart';

`class PlayerPage extends StatefulWidget { PlayerPage({required this.player, Key? key}) : super(key: key); final AssetsAudioPlayer player;

@override State createState() => _PlayerPageState(); }

class _PlayerPageState extends State {

@override void dispose() {

super.dispose();

}

Duration duration = Duration.zero; Duration position = Duration.zero; bool isPlaying = true; @override void initState() { widget.player.isPlaying.listen((event) { if (mounted) { setState(() { isPlaying = event; }); } });

widget.player.onReadyToPlay.listen((newDuration) {
  if (mounted) {
    setState(() {
      duration = newDuration?.duration ?? Duration.zero;
    });
  }
});

widget.player.currentPosition.listen((newPosition) {
  if (mounted) {
    setState(() {
      position = newPosition;
    });
  }
});
super.initState();    

}

@override Widget build(BuildContext context) {

return Scaffold(
  backgroundColor: Colors.white,
  appBar: AppBar(
    backgroundColor: const Color(0xffA56169),
    leading: Padding(
      padding: const EdgeInsets.only(left: 10),
      child: IconButton(
          onPressed: () => Navigator.pop(context),
          icon: const Icon(
            Icons.arrow_back,
            size: 30,
            color: Colors.white,
          )),
    ),
  ),
  extendBodyBehindAppBar: true,
  body: Stack(
    alignment: Alignment.center,
    children: [
      FutureBuilder<PaletteGenerator>(
        future: getImageColors(widget.player),
        builder: (context, snapshot) {
          return Container(
            color: snapshot.data?.mutedColor?.color,
          );
        },
      ),
      Align(
        alignment: Alignment.bottomCenter,
        child: Container(
          decoration: const BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.topRight,
                  colors: [
                //Colors.transparent,
                    Color(0xffA56169),
                    Color(0xff83565A),

              ])),
        ),
      ),
      Positioned(
        height: MediaQuery.of(context).size.height / 1.5,
        child: Column(
          children: [
            Text(
              widget.player.getCurrentAudioTitle,
              style: const TextStyle(
                  fontSize: 24, fontWeight: FontWeight.bold),
            ),
            const SizedBox(
              height: 5,
            ),
            Text(
              widget.player.getCurrentAudioArtist,
              style: const TextStyle(fontSize: 20, color: Colors.white70),
            ),
            SizedBox(
              height: MediaQuery.of(context).size.height / 25,
            ),
            IntrinsicHeight(
              child: Row(
                children: [
                  Text(
                    durationFormat(position),
                    style: const TextStyle(color: Colors.white70),
                  ),
                  const VerticalDivider(
                    color: Colors.white54,
                    thickness: 2,
                    width: 25,
                    indent: 2,
                    endIndent: 2,
                  ),
                  Text(
                    durationFormat(duration - position),
                    style: const TextStyle(color: kPrimaryColor),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
      Center(
          child: SleekCircularSlider(
        min: 0,
        max: duration.inSeconds.toDouble(),
        initialValue: position.inSeconds.toDouble(),
        onChange: (value) async {
          await widget.player.seek(Duration(seconds: value.toInt()));
        },
        innerWidget: (percentage) {
          return Padding(
            padding: const EdgeInsets.all(25.0),
            child: CircleAvatar(
              backgroundColor: Colors.grey[200],
              backgroundImage: AssetImage(
                  widget.player.getCurrentAudioImage?.path ?? ''),
            ),
          );
        },
        appearance: CircularSliderAppearance(
            size: 330,
            angleRange: 300,
            startAngle: 300,
            customColors: CustomSliderColors(
                progressBarColor: Colors.red[200],
                dotColor: Colors.red[200],
                trackColor: Colors.grey.withOpacity(.4)),
            customWidths: CustomSliderWidths(
                trackWidth: 6, handlerSize: 10, progressBarWidth: 6)),
      )),
      Positioned(
        top: MediaQuery.of(context).size.height / 1.3,
        left: 0,
        right: 0,
        child: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              IconButton(
                  onPressed: () async {
                    await widget.player.next();
                  },
                  icon: const Icon(
                    Icons.next_rounded,
                    size: 50,
                    color: Colors.white,
                  )),
              IconButton(
                  onPressed: ()async{
                  await widget.player.playOrPause();
                  },
                icon: isPlaying
                  ? const Icon(Icons.pause, size: 50,color: Colors.white,)
                  : const Icon(Icons.play_arrow, size: 50,color: Colors.white,),
              ),
              IconButton(
                  onPressed: () async {
                    await widget.player.previous();
                  },
                  icon: const Icon( Icons.skip_previous_rounded,
                    size: 50,
                    color: Colors.white,
                  )),
            ],
          ),
        ),
      ),
    ],
  ),
);

} }`

mt633 commented 1 year ago

See my comment here: https://github.com/florent37/Flutter-AssetsAudioPlayer/issues/765#issuecomment-1468110135