Flutter / Dart-如何在flutter_Swiper包中使用onPressed和flatButton播放列表中的声音?

问题描述 投票:-2回答:1

G'day Stackoverflowers!

我已经开始为个人项目学习Flutter / Dart,但遇到了无法解决的问题。请给我一点帮助,谢谢。

我正在构建一个学习英语的应用程序(我是一名英语老师和作家),并且该应用程序的页面之一是您可以看到带有名字的不同食物和饮料的图片以及可以收听的图片。食物或饮料的发音。这是到目前为止的代码片段:

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),
              ],
            ),
          );

您可能会猜到,我可以使用列表和此列表的索引显示所需的图片及其各自的名词。这是我获得的图片和名词列表

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',];

到目前为止是这样:

overview of the page

现在,我希望能够为每张卡播放声音。我的声音在以下目录中的资产中:资产:-assets / foodsounds /我希望能够播放我的声音列表中的声音。这是列表:

    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',];

我考虑过像这样使用audioplayers-0.14.0包:

    void playFoodSound(int foodSounds) {
    final player = AudioCache(prefix: 'foodsounds/');
    player.play('$foodSounds.mp3');
  }

并像下面这样在Swiper中使用它:

FlatButton(
              onPressed: () => {
                foodSounds[index],
              },

此方法无效。我尝试了一些其他方法,但我真的不知道如何播放这些声音...

我非常感谢大家的帮助,因为我真的不想对这些声音进行硬编码。

list flutter audio swiper
1个回答
0
投票

结果我终于找到了答案。

我使用assets_audio_player 1.2.3软件包。宣布以下空缺:

final AssetsAudioPlayer playFoodSound= AssetsAudioPlayer();

并且在我的代码中使用了以下语句:

FlatButton(onPressed: () =>{
                playFoodSound.open(foodSounds[index],),
          },

并且它像一种魅力。

© www.soinside.com 2019 - 2024. All rights reserved.