如何在录制另一种颜色时停止音频播放器,并在另一种颜色开始播放时停止该音乐?IN=> 音频播放器:^0.19.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'),
],
),
),
),
);
}
}

///我需要能够在按下某些颜色时停止声音,并且当前正在播放的音乐在按下新颜色时停止,我该如何实现这一点???我使用过音频播放器:^0.19.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 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'),
],
),
),
),
);
}
}

相关内容

  • 没有找到相关文章

最新更新