0
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:audioplayers/src/audio_cache.dart';

void main() => runApp(XylophoneApp());
 final player = AudioCache();
class XylophoneApp extends StatelessWidget {
  void playSound(String soundNumber) {
   
    player.play('beerpour.mp3');
  }
  
  Expanded buildKey({required Color color, required String soundNumber}) {
    return Expanded(
      child: FlatButton(
        color: color,
        onPressed: () {
          // create a new player
          // Audio cache is for local assets
          if (color == Colors.red) {
            playSound(soundNumber);//here it is played when tapped,I want no currently running sound to stop when new sound is played.
          } else {
            //I want to stop when other is pressed.
          }
        },
        child: Text(
          '',
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.black,
        body: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              buildKey(color: Colors.red, soundNumber: 'beerpour.mp3'),
              buildKey(color: Colors.green, soundNumber: 'beerpour.mp3'),
              buildKey(color: Colors.blue, soundNumber: 'beerpour.mp3'),
              buildKey(color: Colors.teal, soundNumber: 'beerpour.mp3'),
              buildKey(color: Colors.orange, soundNumber: 'beerpour.mp3'),
              buildKey(color: Colors.purple, soundNumber: 'beerpour.mp3'),
              buildKey(color: Colors.yellow, soundNumber: 'beerpour.mp3'),
            ],
          ),
        ),
      ),
    );
  }
}

///I need to be able to stop the sound when I pressed some color, and also currently playing music to be stopped when new one is pressed, how can I achieve that??? I have used audioplayers:^0.19.0, if any other version is more easy then please say so!

bird
  • 1
  • 2

1 Answers1

0

I am not sure I get what you want to achieve. but from what I understand, you want to stop any sound before playing a new sound. And some colors don't have to play sounds, so for those colors you want the sound to stop.

Check if this is what you want.

    import 'package:audioplayers/audioplayers.dart';
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:audioplayers/src/audio_cache.dart';
    
    void main() => runApp(XylophoneApp());
     
     final cache = AudioCache();
     AudioPlayer player; // edit use to control playback, you can also use two players with this, one for old sound, one for new sound. you stop the old one and play new one. but i'm using a single player here

    class XylophoneApp extends StatelessWidget {
      void playSound(String soundNumber) async {
       // stop player here before you play any new sound
       // edit
       await player?.stop();
        player = await cache.play('beerpour.mp3');
      }
      
      Expanded buildKey({required Color color, required String soundNumber}) {
        return Expanded(
          child: FlatButton(
            color: color,
            onPressed: () {
              // create a new player
              // Audio cache is for local assets
              if (color == Colors.red) {
                playSound(soundNumber);//here it is played when tapped,I want no currently running sound to stop when new sound is played.
              } else {
              // stop player here
              player?.stop(); // edit added
                //I want to stop when other is pressed.
              }
            },
            child: Text(
              '',
            ),
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            backgroundColor: Colors.black,
            body: SafeArea(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  buildKey(color: Colors.red, soundNumber: 'beerpour.mp3'),
                  buildKey(color: Colors.green, soundNumber: 'beerpour.mp3'),
                  buildKey(color: Colors.blue, soundNumber: 'beerpour.mp3'),
                  buildKey(color: Colors.teal, soundNumber: 'beerpour.mp3'),
                  buildKey(color: Colors.orange, soundNumber: 'beerpour.mp3'),
                  buildKey(color: Colors.purple, soundNumber: 'beerpour.mp3'),
                  buildKey(color: Colors.yellow, soundNumber: 'beerpour.mp3'),
                ],
              ),
            ),
          ),
        );
      }
    }
ayinloya
  • 134
  • 2
  • 6