skip to Main Content

`hi i am new in flutter and i got this problem using my codes when i change the page i can not stop the music below are my codes what should i add

`hi i am new in flutter and i got this problem using my codes when i change the page i can not stop the music below are my codes what should i add

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,
                  )),
            ],
          ),
        ],
      ),
    );
  }
}

your text

I am trying to navigate others’ pages and come back to my music pages to stop the music from playing easily

2

Answers


  1. Try this :

    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,
                      )),
                ],
              ),
            ],
          ),
        );
      }
    }
    

    I have added this method to your code which you need to add as well :

    @override
      void dispose() {
    
        player!.stop();
        timer2!.cancel();
    
        super.dispose();
      }
    
    Login or Signup to reply.
  2. initialize player in second page and call for stop method

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search