繁体   English   中英

flutter:: 有没有办法使用音频播放器 package 只播放一个音频文件?

[英]flutter:: Is there a way to play only one audio file using the audioplayer package?

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

看来你有你的 state 变量,比如_isplaying整个页面/列表只有一次 每首歌都需要它们。 这似乎不是一个完整的例子( widget.records似乎丢失了)所以我在这里帮不上什么忙。

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

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

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

AudioPlayer? _player;  

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

}

并在某处使用它:

playAudio(currentAudio, audioSource);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM