【问题标题】:Playing the same audio file multiple times - Audioplayers Flutter多次播放同一个音频文件 - Audioplayers Flutter
【发布时间】:2020-10-22 11:18:32
【问题描述】:

我怎样才能根据需要多次播放相同的音频文件,而不是循环播放,但我希望能够精确地播放多少次,我按照tutorial 制作了播放/暂停功能的基础知识。我正在与其他人作斗争。 我使用audioplayers 包的最新版本。

这是我的代码:

import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:audioplayers/audio_cache.dart';


class Player extends StatefulWidget {
  @override
  _PlayerState createState() => _PlayerState();
}

AnimationController _animationIconController ;

AudioCache audioCache;
AudioPlayer audioPlayer;


bool issongplaying = false;
bool isplaying = false;



class _PlayerState extends State<Player> with TickerProviderStateMixin {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initPlayer();
  }

  void initPlayer(){
    _animationIconController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 750),
      reverseDuration: Duration(milliseconds: 750),
    );
    audioPlayer = new AudioPlayer();
    audioCache = new AudioCache(fixedPlayer: audioPlayer);
   
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            GestureDetector(
              onTap: (){
                setState((){
                  isplaying
                  ? _animationIconController.reverse()
                  : _animationIconController.forward();
                  isplaying = !isplaying;
                });

                if (issongplaying == false) {
                  audioCache.play('audio.wav');
                  setState(() {
                    issongplaying = true;
                  });
                } else {
                  audioPlayer.pause();
                  setState(() {
                    issongplaying = false;
                  });
                }
              },
              child: ClipOval(
                child: Container(
                  decoration: BoxDecoration(
                    border: Border.all(
                    width: 2,
                      color: Colors.greenAccent,
                    ),
                    borderRadius: BorderRadius.all(Radius.circular(50.0)
                    ),
                  ),
                  width: 75,
                  height: 75,
                  child: Center(
                    child: AnimatedIcon(
                      icon: AnimatedIcons.play_pause,
                      progress: _animationIconController,
                      color: Colors.grey,
                      size: 60,
                    )
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

谢谢

【问题讨论】:

  • 一些关于你编码的技巧(我想你刚刚开始编码),条件写得很奇怪,最好使用 if else,还有为什么使用 issongplaying == false,最好使用 !issongplaying。这些事情是不可原谅的。
  • 没错,我刚开始学习编码,谢谢你的评论!

标签: flutter dart audio audio-player


【解决方案1】:

这对您来说可能很复杂,但请尝试一下。

我可以看到 audioplayers 包中有 onPlayerCompletion 事件,每次播放完音频都会调用该事件。

您可以在此事件中告诉您的播放器跟踪它结束的次数并将音频设置为循环播放,当它重复 X 次时它将停止:

int timesPlayed = 0;
int timesToRepeat = 3; //The audio will repeat 3 times

//This method gets called each time your audio finishes playing.
player.onPlayerCompletion.listen((event) {
  //Here goes the code that will be called when the audio finishes
  onComplete();
  setState(() {
    position = duration;
    timesPlayed++;
    if(timesPlayed >= timesToRepeat) {
      timesPlayed = 0;
      await player.stop();
    }
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多