如何在flutter应用程序中的其他页面导航后停止音乐

7fhtutme  于 2022-12-30  发布在  Flutter
关注(0)|答案(1)|浏览(156)

嗨,我是新的Flutter,我得到了这个问题,使用我的代码时,我改变了页面,我不能停止下面的音乐是我的代码,我应该添加什么

'嗨,我是新的Flutter,我得到了这个问题,使用我的代码,当我改变页面,我不能停止下面的音乐是我的代码,我应该添加什么

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';

AudioPlayer? player;

class Hom extends StatelessWidget {
  const Hom({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("title")),
      body: const Center(
        child: AudioPlayers(),
      ),
    );
  }
}

Duration _position = Duration(seconds: 0);
var _progress = 0.0;

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

  @override
  State<AudioPlayers> createState() => _AudioPlayersState();
}

class _AudioPlayersState extends State<AudioPlayers> {
  Timer? timer2;
  final player = AudioPlayer();

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
            image: AssetImage("images/Night-Sky-Backgrounds.jpg"),
            fit: BoxFit.cover),
      ),
      padding: EdgeInsets.all(8),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          LinearProgressIndicator(
            value: _progress,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              IconButton(
                  onPressed: () {
                    player!.setAsset('assets/Meditation1.mp3').then((value) {
                      return {
                        _position = value!,
                        player!.playerStateStream.listen((state) {
                          if (state.playing) {
                            setState(() {
                              _progress = .1;
                            });
                          } else
                            switch (state.processingState) {
                              case ProcessingState.idle:
                                break;
                              case ProcessingState.loading:
                                break;
                              case ProcessingState.buffering:
                                break;
                              case ProcessingState.ready:
                                setState(() {
                                  _progress = 0;
                                  timer2!.cancel();
                                });
                                break;
                              case ProcessingState.completed:
                                setState(() {
                                  _progress = 1;
                                });
                                break;
                            }
                        }),
                        player!.play(),
                        timer2 =
                            Timer.periodic(new Duration(seconds: 1), (timer) {
                          setState(() {
                            _progress += .05;
                          });
                        })
                      };
                    });
                  },
                  icon: Icon(
                    _progress > 0 ? Icons.pause : Icons.play_circle_fill,
                    size: 45,
                  )),
              SizedBox(
                width: 45,
              ),
              IconButton(
                  onPressed: () {
                    player!.stop();
                    timer2!.cancel();
                  },
                  icon: Icon(
                    Icons.stop,
                    size: 45,
                  )),
            ],
          ),
        ],
      ),
    );
  }
}

第一个月
我正试图浏览别人的页面,然后回到我的音乐页面,以阻止音乐轻松播放

e4yzc0pl

e4yzc0pl1#

试试这个:

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';

AudioPlayer? player;

class Hom extends StatelessWidget {
  const Hom({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("title")),
      body: const Center(
        child: AudioPlayers(),
      ),
    );
  }
}

Duration _position = Duration(seconds: 0);
var _progress = 0.0;

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

  @override
  State<AudioPlayers> createState() => _AudioPlayersState();
}

class _AudioPlayersState extends State<AudioPlayers> {
  Timer? timer2;
  final player = AudioPlayer();

  @override
  void dispose() {

    player!.stop();
    timer2!.cancel();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
            image: AssetImage("images/Night-Sky-Backgrounds.jpg"),
            fit: BoxFit.cover),
      ),
      padding: EdgeInsets.all(8),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          LinearProgressIndicator(
            value: _progress,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              IconButton(
                  onPressed: () {
                    player!.setAsset('assets/Meditation1.mp3').then((value) {
                      return {
                        _position = value!,
                        player!.playerStateStream.listen((state) {
                          if (state.playing) {
                            setState(() {
                              _progress = .1;
                            });
                          } else
                            switch (state.processingState) {
                              case ProcessingState.idle:
                                break;
                              case ProcessingState.loading:
                                break;
                              case ProcessingState.buffering:
                                break;
                              case ProcessingState.ready:
                                setState(() {
                                  _progress = 0;
                                  timer2!.cancel();
                                });
                                break;
                              case ProcessingState.completed:
                                setState(() {
                                  _progress = 1;
                                });
                                break;
                            }
                        }),
                        player!.play(),
                        timer2 =
                            Timer.periodic(new Duration(seconds: 1), (timer) {
                          setState(() {
                            _progress += .05;
                          });
                        })
                      };
                    });
                  },
                  icon: Icon(
                    _progress > 0 ? Icons.pause : Icons.play_circle_fill,
                    size: 45,
                  )),
              SizedBox(
                width: 45,
              ),
              IconButton(
                  onPressed: () {
                    player!.stop();
                    timer2!.cancel();
                  },
                  icon: Icon(
                    Icons.stop,
                    size: 45,
                  )),
            ],
          ),
        ],
      ),
    );
  }
}

我已经将此方法添加到您的代码中,您也需要添加此方法:

@override
  void dispose() {

    player!.stop();
    timer2!.cancel();

    super.dispose();
  }

相关问题