flutter:: 有沒有辦法使用 audioplayer 包只播放一個音頻文件?

jiji

我將音頻文件保存在列表“文件”中。我想使用 audioplayers 包播放這個音頻文件。我已經使用 listview 列出了音頻文件,但我只想在多個音頻文件中播放一個選定的音頻文件。目前,記錄中的整個音頻文件是同時播放的,並且出現了錯誤。
我該如何解決這個問題?

這是我的代碼的一部分。

class AudioViewer extends StatefulWidget {
  @override
  _AudioViewerState createState() => _AudioViewerState();
}

class _AudioViewerState extends State<AudioViewer> {
  List file = [];
  var audioPath;
  var directory;
  AudioPlayer? audioPlayer;
  bool _isplaying = false;
  var _icon = Icons.play_arrow;
  var _deleteicon = Icons.delete;
  Color _color = Colors.deepOrangeAccent;
  Duration position = Duration();
  Duration duration = Duration(seconds: 1);

  int indexindex = 0;

  @override
  void initState() {
    super.initState();
    getFiles();
    _setupAudioPlayer();
  }

  void getFiles() async {
    directory = (await getExternalStorageDirectory())!.path;
    setState(() {
      file = Directory("$directory").listSync();
    });
    print(file.toString());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text(
          "Audio List",
        ),
      ),
      body: Container(
          child: ListView.builder(
            reverse: true,
            itemCount: widget.records.length,
            shrinkWrap: true,
            itemBuilder: (BuildContext context, int index) {
            return Card(
                elevation: 5,
                child: ExpansionTile(
                    title: Text(
                      widget.records[index].path.split('/').last.split(".").first,
                      style: TextStyle(color: Colors.black),
                    ),
                    onExpansionChanged: ((newState) {
                      if (newState) {
                        indexindex = index;
                        setState(() {
                    
                        });
                      }
                    }),
                    children: [
                      Container(
                        height: 100,
                        padding: EdgeInsets.fromLTRB(10,0,10,0),
                        child: Column(
                          children: <Widget>[
                                Row(
                                    children: <Widget>[
                                      Container(
                                        child: IconButton(
                                          iconSize: 40,
                                        onPressed: () {
                                            if (!_isplaying) {
                                              audioPlayer!.resume();
                         setState(() {
                                                _isplaying = true;
                                                _icon = Icons.pause;
                                                _color = Colors.blueGrey;

                                              });
                                            } else {
                                              audioPlayer!.release();
                                              setState(() {
                                                position = new Duration();
                                                _isplaying = false;
                                                _icon = Icons.play_arrow;
                                                _color = Colors.deepOrangeAccent;

                                              });
                                            }
                                          },
                                          icon: Icon(_icon),
                                          color: _color,
                                        ),
                                      ),
                                      Container(
                                        child: IconButton(
                                          iconSize: 30,
                                          onPressed: () {
                                            widget.records[index].delete(recursive: true);
                                            setState(() {
                                              widget.records.removeAt(index);
                                              position = new Duration();
                                            });
                                          },
                                          icon: Icon(_deleteicon),
                                        ),
                                      ),
                                    ]
                                ),
nvoigt

似乎您的狀態變量對於整個頁面/列表_isplaying只有一次每首歌都需要它們這似乎不是一個完整的例子(widget.records似乎丟失了)所以我在這裡真的幫不上什麼忙。

如果widget.records包含歌曲,您可以做的是擁有一個currentlyPlaying變量,即當前正在播放的記錄,而不僅僅是一個通用的_isplaying. 這樣,您就可以確定當前構建的列表條目是否正在播放。但是也許我誤解了一些東西,因為您的音頻播放器實際上並沒有播放那首歌,它只是恢復之前播放的內容。

也許從簡單的步驟開始,然後首先使圖標起作用。這應該可以通過我上面提到的方法實現:跟踪選擇了哪條記錄。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Flutter中使用Audioplayer插件播放本地mp3文件

有沒有辦法在包中只包含一個模塊?

有沒有辦法在使用 excel javascript api 時遍歷一個範圍?

有沒有辦法在另一個地圖函數中使用地圖?

有沒有辦法只使用 RestSharp 獲取訪問令牌?

有沒有辦法在 Next.js 的 getStaticProps 中使用 redux 工具包?

有沒有辦法優化這個功能?

有沒有辦法改變在這個項目中使用的 pip?

有沒有辦法使用 R 在整個數據幀中舍入因子?

有沒有辦法指定在舵圖中使用哪些值文件

有沒有辦法在文件中添加新行?

有沒有辦法在函數中使用一段可變的代碼?

發布了我的第一個 Flutter 包 - 視頻演示沒有出現在我的自述文件中

AudioPlayer无法在使用Java 1.6的Mac上播放声音

在Java中使用AudioPlayer播放24位WAV

Flutter Audioplayer 無法在 android 設備中獲得持續時間

有沒有辦法在 Flutter 的 TabBar 內製作下拉列表?

有沒有辦法只使用 sendgrid 的 API 來構建完整的 sendgrid 訂閱表單?

Flutter保存和恢复AudioPlayer的位置

有沒有辦法在 Python 中不使用循環生成多個正弦波?

有沒有辦法使用 purr 或 dplyr 基於 R 中的兩個組添加行?

有沒有辦法在某個點(C)提取for循環中使用的變量的值?

有沒有辦法在 Node.js 中使用兩個回調函數的值(布爾值)?

有沒有辦法每次雙擊使用我的 PowerShell 編譯腳本啟動文件?

有沒有辦法使用 for 循環根據文件名創建目錄?

有沒有辦法使用 Python Pandas 從另一行中減去特定行?

有沒有辦法在使用或不使用 GitHub 工作流的情況下在 Netlify 上的 flutter web 部署中使用機密?

有沒有辦法用“引號”快速括起一個單詞列表

有沒有辦法通過slurm找出節點上有多少個gpu?