【问题标题】:Flutter: How to play and pause audio using the same button as countdown start button?Flutter:如何使用与倒计时开始按钮相同的按钮播放和暂停音频?
【发布时间】:2020-09-01 17:06:57
【问题描述】:

我有一个用于倒计时的播放暂停按钮。当我按下播放时,计时器启动,同时我也是要播放的音频。我应该如何实现这个?

注意:大部分计数器代码在另一个文件中。只是想让音频的播放/暂停首先在同一个 onPressed 中工作。

以下是我目前所拥有的:

更新:我想我在某个地方搞砸了。我现在无法暂停音频。我一直在尝试一些颤振音频播放器教程,但我无法弄清楚如何使用单个播放暂停按钮来实现倒计时和音频操作

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

import 'package:audioplayers/audioplayers.dart';

class Jiddu extends StatefulWidget {
  const Jiddu({Key key}) : super(key: key);

  @override
  _JidduState createState() => _JidduState();
}

class _JidduState extends State<Jiddu> with TickerProviderStateMixin {
  AnimationController controller;
  AudioPlayer _audioPlayer = AudioPlayer();
  bool isPlaying = false;
  @override
  void initState() {
    super.initState();
    controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 60),
    );
      _audioCache = AudioCache(
        prefix: "audio/",
        fixedPlayer: AudioPlayer()..setReleaseMode(ReleaseMode.STOP));
  }

  playme() async {
  AudioPlayer audioplay = await _audioCache.play('30sec.mp3');
  }

  AudioCache _audioCache;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Audio')),
      body: Column(
        children: <Widget>[
          Center(
              child: RaisedButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text('Go Back'))),
          Container(
              margin: EdgeInsets.all(20.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  FloatingActionButton(
                    child: AnimatedBuilder(
                      animation: controller,
                      builder: (BuildContext context, Widget child) {
                        return new Icon(controller.isAnimating
                            ? Icons.pause
                            : Icons.play_arrow);
                      },
                    ),
                    onPressed: () {
                      if (controller.isAnimating && isPlaying){
                        controller.stop();

                     } else {
                        controller.reverse(
                            from: controller.value == 0.0
                                ? 1.0
                                : controller.value);    
                        playme();
                      }
                    },
                  ),
                ],
              )),
        ],
      ),
    );
  }
}


【问题讨论】:

    标签: flutter


    【解决方案1】:

    只需在 onPressed 中根据您的条件定义您的函数。

    可能是这样的:

     onPressed: () {
                            if (controller.isAnimating)
                              controller.stop();
                            else {
                              playaudio(); // or what is your function for this
                              controller.reverse(
                                  from: controller.value == 0.0
                                      ? 1.0
                                      : controller.value);
                            }
                            setState(() {
                              _visible = !_visible;
                            });
                          },
    

    【讨论】:

    • 嘿!我刚刚用我的完整代码更新了我的原始帖子。请看一看。
    猜你喜欢
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多