[英]Flutter/Dart - How can I play sounds from a list using onPressed with a flatButton in flutter_Swiper package?
G'day Stackoverflowers! G'day Stackoverflowers!
I have started learning Flutter/Dart for a personal project and I came across a problem I can't solve.我已经开始为个人项目学习 Flutter/Dart,但遇到了一个我无法解决的问题。 I would really appreciate a bit of help, please.我真的很感激一点帮助,请。
I'm building an application to learn English (I'm an English teacher and author) and one of the pages of the app is where you can see pictures of different foods and drinks with their name written and the possibility to listen to the pronunciation of the food or drink.我正在构建一个学习英语的应用程序(我是一名英语老师和作家),该应用程序的其中一个页面是您可以看到写有名字的不同食物和饮料的图片,以及听发音的可能性食物或饮料。 here is a snippet of the code so far:这是到目前为止的代码片段:
Swiper(
itemWidth: MediaQuery.of(context).size.width,
itemHeight: MediaQuery.of(context).size.height,
layout: SwiperLayout.TINDER,
itemCount: 43,
indicatorLayout: PageIndicatorLayout.WARM,
itemBuilder: (BuildContext context, int index,) {
return Card(
elevation: 5.0,
child: Column(
children: <Widget>[
FlatButton(
onPressed: () => {
foodSounds[index],
},
child: Row(
children: <Widget>[
SizedBox(
width: 70.0,
),
Icon(Icons.volume_up),
SizedBox(
width: 30.0,
),
Text(foodNouns[index],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w500,
)),
],
),
),
Image.asset(foodPictures[index], fit: BoxFit.contain),
],
),
);
As you can probably guess, I have no problem displaying the pictures I want and their respective nouns using a list and the index of this list.正如您可能猜到的那样,我可以使用列表和该列表的索引来显示我想要的图片及其各自的名词。 Here are the lists I've got for the pictures and nouns这是我为图片和名词准备的列表
final List foodPictures = [
'images/foodnouns/apple.jpg',
'images/foodnouns/banana.jpg',
'images/foodnouns/bar.jpg',
'images/foodnouns/beer.jpg',
'images/foodnouns/biscuits.jpg',
'images/foodnouns/bread.jpg',
'images/foodnouns/breakfast.jpg',
'images/foodnouns/butter.jpg',
'images/foodnouns/cafe.jpg',
'images/foodnouns/cake.jpg',
'images/foodnouns/cheese.jpg',
'images/foodnouns/chocolate.jpg',
'images/foodnouns/coffee.jpg',
'images/foodnouns/cup.jpg',
'images/foodnouns/cup_of_coffee.jpg',
'images/foodnouns/dinner.jpg',
'images/foodnouns/egg.jpg',
'images/foodnouns/fish.jpg',
'images/foodnouns/food.jpg',
'images/foodnouns/fries.jpg',
'images/foodnouns/fruits.jpg',
'images/foodnouns/glass.jpg',
'images/foodnouns/ice_cream.jpg',
'images/foodnouns/juice.jpg',
'images/foodnouns/knife.jpg',
'images/foodnouns/lunch.jpg',
'images/foodnouns/meal.jpg',
'images/foodnouns/meat.jpg',
'images/foodnouns/milk.jpg',
'images/foodnouns/orange.jpg',
'images/foodnouns/picnic.jpg',
'images/foodnouns/pizza.jpg',
'images/foodnouns/plate.jpg',
'images/foodnouns/potatoes.jpg',
'images/foodnouns/rice.jpg',
'images/foodnouns/salt.jpg',
'images/foodnouns/sandwich.jpg',
'images/foodnouns/soup.jpg',
'images/foodnouns/sugar.jpg',
'images/foodnouns/tea.jpg',
'images/foodnouns/tomato.jpg',
'images/foodnouns/vegetables.jpg',
'images/foodnouns/wine.jpg',];
final List foodNouns =[
'apple',
'banana',
'bar',
'beer',
'biscuits',
'bread',
'breakfast',
'butter',
'cafe',
'cake',
'cheese',
'chocolate',
'coffee',
'cup',
'cup_of_coffee',
'dinner',
'egg',
'fish',
'food',
'fries',
'fruits',
'glass',
'ice_cream',
'juice',
'knife',
'lunch',
'meal',
'meat',
'milk',
'orange',
'picnic',
'pizza',
'plate',
'potatoes',
'rice',
'salt',
'sandwich',
'soup',
'sugar',
'tea',
'tomato',
'vegetables',
'wine',];
This is how it looks like so far:这是到目前为止的样子:
Now I would like to be able to play a sound for each card.现在我希望能够为每张卡播放声音。 I have the sounds in my assets in the following directory : assets: -assets/foodsounds/ I want to be able to play the sound from a list of my sounds.我的资产中的声音位于以下目录中:资产:-assets/foodsounds/ 我希望能够从我的声音列表中播放声音。 here is the list :这是清单:
final List foodSounds=[
'assets/foodsounds/apple.mp3',
'assets/foodsounds/banana.mp3',
'assets/foodsounds/bar.mp3',
'assets/foodsounds/beer.mp3',
'assets/foodsounds/biscuits.mp3',
'assets/foodsounds/bread.mp3',
'assets/foodsounds/breakfast.mp3',
'assets/foodsounds/butter.mp3',
'assets/foodsounds/café.mp3',
'assets/foodsounds/cake.mp3',
'assets/foodsounds/cheese.mp3',
'assets/foodsounds/chocolate.mp3',
'assets/foodsounds/coffee.mp3',
'assets/foodsounds/cup.mp3',
'assets/foodsounds/cup_of_coffee.mp3',
'assets/foodsounds/dinner.mp3',
'assets/foodsounds/egg.mp3',
'assets/foodsounds/fish.mp3',
'assets/foodsounds/food.mp3',
'assets/foodsounds/fries.mp3',
'assets/foodsounds/fruits.mp3',
'assets/foodsounds/glass.mp3',
'assets/foodsounds/ice_cream.mp3',
'assets/foodsounds/juice.mp3',
'assets/foodsounds/knife.mp3',
'assets/foodsounds/lunch.mp3',
'assets/foodsounds/meal.mp3',
'assets/foodsounds/meat.mp3',
'assets/foodsounds/milk.mp3',
'assets/foodsounds/orange.mp3',
'assets/foodsounds/picnic.mp3',
'assets/foodsounds/pizza.mp3',
'assets/foodsounds/plate.mp3',
'assets/foodsounds/potatoes.mp3',
'assets/foodsounds/rice.mp3',
'assets/foodsounds/salt.mp3',
'assets/foodsounds/sandwich.mp3',
'assets/foodsounds/soup.mp3',
'assets/foodsounds/sugar.mp3',
'assets/foodsounds/tea.mp3',
'assets/foodsounds/tomato.mp3',
'assets/foodsounds/vegetables.mp3',
'assets/foodsounds/wine.mp3',];
I thought about using the audioplayers-0.14.0 package like so :我想过像这样使用 audioplayers-0.14.0 包:
void playFoodSound(int foodSounds) {
final player = AudioCache(prefix: 'foodsounds/');
player.play('$foodSounds.mp3');
}
and use it in Swiper like the following:并在 Swiper 中使用它,如下所示:
FlatButton(
onPressed: () => {
foodSounds[index],
},
This method doesn't work.这个方法行不通。 I have tried a few others and I really can't figure out how to play those sounds...我尝试了其他一些,但我真的不知道如何播放这些声音......
I thank you all very much in advance for your help because I really don't want to have to hardcode those sounds.非常感谢大家的帮助,因为我真的不想对这些声音进行硬编码。
Turns out I finally found my answer.原来我终于找到了我的答案。
I used the assets_audio_player 1.2.3 package.我使用了 assets_audio_player 1.2.3 包。 Declared the following vaiable:声明以下变量:
final AssetsAudioPlayer playFoodSound= AssetsAudioPlayer();
and used the following statement in my code :并在我的代码中使用了以下语句:
FlatButton(onPressed: () =>{
playFoodSound.open(foodSounds[index],),
},
And it works like a charm.它就像一个魅力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.