完成音频后如何防止音频破坏?

时间:2020-05-22 13:14:46

标签: flutter audio-player flutter-packages

您好,我试图防止音频播放结束时对其造成破坏。颤振包是

https://pub.dev/packages/assets_audio_player#-readme-tab-

我尝试做一个循环,但是在循环播放和暂停后,这些控件将无法正常工作,并且也不会破坏自身。

任何想法我应该如何实施?

我的代码是:

class MusicInteractions extends StatefulWidget {
  @override
  _MusicInteractionsState createState() => _MusicInteractionsState();
}

class _MusicInteractionsState extends State<MusicInteractions> {
  final AssetsAudioPlayer _assetsAudioPlayer = AssetsAudioPlayer();

  @override
  void initState() {
    _assetsAudioPlayer.open(
      Audio(
        'assets/sample/audioSample.mp3',
        metas: Metas(
          title: "Break my soul",
          artist: "Julie Howard",
          album: "",
          image: MetasImage.network(
              "https://images.unsplash.com/photo-1581289098325-fd41f57a9d4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1301&q=80"),
        ),
      ),
      autoStart: false,
      respectSilentMode: true,
      showNotification:
          false, // TODO: all the songs are reproduced at the same time
    );
    super.initState();
  }

  @override
  void dispose() {
    _assetsAudioPlayer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        BackgroundImage(
          imageUrl:
              'https://images.unsplash.com/photo-1581289098325-fd41f57a9d4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1301&q=80',
          height: kPostBackgroundImageHeight,
        ),
        Positioned(
          top: kCommonSeparation,
          left: kCommonSeparation,
          child: Text(
            'Break my soul',
            style: kBigWhiteBoldText,
          ),
        ),
        Positioned(
          top: kPostBackgroundImageHeight / 2 - 50,
          left: MediaQuery.of(context).size.width / 2 - 65,
          child: StreamBuilder(
              stream: _assetsAudioPlayer.current,
              builder: (context, snapshot) {
                final Playing playing = snapshot.data;
                final bool isPlaying = _assetsAudioPlayer.isPlaying.value;
                return Opacity(
                  opacity: kOpacity,
                  child: InkWell(
                    onTap: () {
                      setState(() {
                        _assetsAudioPlayer.playOrPause();
                      });
                    },
                    child: Icon(
                      isPlaying
                          ? Icons.pause_circle_filled
                          : Icons.play_circle_filled,
                      color: Colors.white,
                      size: 100,
                    ),
                  ),
                );
              }),
        ),
        Positioned(
          bottom: kSmallSeparation,
          right: kSmallSeparation,
          child: Container(
            height: 30.0,
            child: Row(
              children: <Widget>[
                BubblePost(
                  icon: kCommentIcon,
                  number: 212,
                  action: () => print('Comment post'),
                ),
                const SizedBox(width: kSmallSeparation),
                BubblePost(
                  icon: kLikeIcon,
                  number: 2032,
                  action: () => print('Liked post'),
                ),
              ],
            ),
          ),
        ),
        Positioned(
          bottom: kCommonSeparation,
          left: kSmallSeparation,
          child: _assetsAudioPlayer.current.value == null
              ? SizedBox(width: 0)
              : StreamBuilder(
                  stream: _assetsAudioPlayer.currentPosition,
                  builder: (context, asyncSnapshot) {
                    final Duration songPostion =
                        asyncSnapshot.data ?? Duration(seconds: 0);
                    final Duration songDuration =
                        _assetsAudioPlayer.current.value.audio.duration ??
                            Duration(seconds: 0);
                    format(Duration d) => d
                        .toString()
                        .split('.')
                        .first
                        .padLeft(8, '0')
                        .substring('00:'.length, '00:00:00'.length);
                    return Text(
                      format(songPostion) + ' / ' + format(songDuration),
                      style: TextStyle(color: Colors.white),
                    );
                  }),
        ),
        Positioned(
          bottom: -kCommonSeparation - 2,
          left: -kBigSeparation,
          child: _assetsAudioPlayer.current.value == null
              ? SizedBox(width: 0)
              : Container(
                  width: kPostBackgroundImageHeight + 10,
                  child: StreamBuilder(
                      stream: _assetsAudioPlayer.currentPosition,
                      builder: (context, asyncSnapshot) {
                        final songDuration =
                            _assetsAudioPlayer.current.value.audio.duration;
                        return SliderTheme(
                          data: SliderTheme.of(context).copyWith(
                            activeTrackColor: kPink,
                            inactiveTrackColor: Colors.white70,
                            thumbColor: Colors.transparent,
                            trackHeight: 5.0,
                            thumbShape:
                                RoundSliderThumbShape(enabledThumbRadius: 0.0),
                          ),
                          child: Slider(
                              min: 0,
                              max: songDuration.inSeconds.toDouble(),
                              value: _assetsAudioPlayer
                                  .currentPosition.value.inSeconds
                                  .toDouble(),
                              onChanged: (value) {
                                setState(() {
                                  _assetsAudioPlayer
                                      .seek(Duration(seconds: value.toInt()));
                                });
                              }),
                        );
                      }),
                ),
        ),
      ],
    );
  }
}

0 个答案:

没有答案