【问题标题】:flutter:: Is there a way to play only one audio file using the audioplayer package?flutter:: 有没有办法使用 audioplayer 包只播放一个音频文件?
【发布时间】:2021-12-09 09:48:07
【问题描述】:

我将音频文件保存在列表“文件”中。我想使用 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),
                                        ),
                                      ),
                                    ]
                                ),

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    看来你的状态变量像_isplaying 只有一次 用于整个页面/列表。你需要它们每首歌。这似乎不是一个完整的例子(widget.records 似乎不见了)所以我在这里帮不上什么忙。

    如果widget.records 包含歌曲,您可以做的是拥有一个currentlyPlaying 变量,它是当前正在播放的唱片,而不仅仅是一个通用的_isplaying。这样,您可以确定当前构建列表条目是否正在播放。但是也许我误解了一些东西,因为您的音频播放器实际上并没有播放那首歌,它只是恢复之前播放的任何内容。

    也许从简单的步骤开始,首先让图标工作。使用我上面提到的方法应该可以做到这一点:跟踪选择了哪条记录。

    【讨论】:

      【解决方案2】:

      你需要像这样创建一个 globals.dart 文件:

      AudioPlayer? _player;  
      
      Future<void> playAudio(AudioPlayer player, AudioSource source) async {  
          if (_player != null && _player!.playing) {
            await _player!.stop();
          }
          _player = player;
          player.setAudioSource(audioSource);
          player.play();
        }
      }
      

      并在某处使用它:

      playAudio(currentAudio, audioSource);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-28
        • 1970-01-01
        • 2019-10-19
        • 1970-01-01
        • 1970-01-01
        • 2019-12-05
        相关资源
        最近更新 更多