title: How to stop sounds from playing when I close Flutter app
body: <p>I was working on my android app with flutter and it has a bug right now where when the app is in the background, it still plays a sound. The idea of the app is to play a bell sound when you shake your phone. It also likes to slow down the phone when it is in the background as well. Here is the code for my home page. (the code might not be the best, this is my first app)</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:audioplayers/audio_cache.dart';
  import 'package:audioplayers/audioplayers.dart';
  import 'package:sensors/sensors.dart';
  import 'package:google_mobile_ads/google_mobile_ads.dart';
  import 'package:provider/provider.dart';
  import 'package:bell/ad_state.dart';
  import 'package:bell/globals.dart' as globals;
  
  class Home extends StatefulWidget {
    @override
    _HomeState createState() =&gt; _HomeState();
  }
  
  class _HomeState extends State&lt;Home&gt;{
    bool isBackground;
    BannerAd banner;
  
    Future&lt;AudioPlayer&gt; playLocalAsset() async {
      AudioCache cache = new AudioCache();
      return await cache.play(globals.playerBellSound);
    }
  
    Future&lt;AudioPlayer&gt; shakeBell() async {
      AudioCache cache1 = new AudioCache();
      return await cache1.play(globals.shakeBellSound);
    }
  
    @override
    void initState() {
      super.initState();
      accelerometerEvents.listen((AccelerometerEvent event) {
        if (event.x &gt; 5.0 || event.x &lt; -5.0) {
          shakeBell();
        }
      });
    }
    
    @override
    void dispose() {
      super.dispose();
    }
  
    void didChangeDependencies() {
      super.didChangeDependencies();
      final adState = Provider.of&lt;AdState&gt;(context);
      adState.initialization.then((status) {
        setState(() {
          banner = BannerAd(
            adUnitId: adState.bannerAdUnitId,
            size: AdSize.banner,
            request: AdRequest(),
            listener: adState.adListener,
          );
        });
      });
    }
  
    @override
    Widget build(BuildContext context) {
      banner.load();
      return Scaffold(
        appBar: AppBar(
          leading: GestureDetector(
            onTap: () {
              Navigator.pushNamed(context, '/settings');
            },
            child: Icon(
              Icons.settings,
            ),
          ),
          title: Text(&quot;Any Bell&quot;),
          centerTitle: true,
          backgroundColor: Colors.blue[300],
        ),
        body: Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/ocean.jpg'),
                fit: BoxFit.cover,
              ),
            ),
            child: Center(
              child: Column(
                children: &lt;Widget&gt;[
                  IconButton(
                    onPressed: () {
                      playLocalAsset();
                    },
                    icon: Icon(
                      Icons.notifications,
                      color: Colors.white,
                    ),
                    iconSize: 70,
                  ),
                  Flexible(
                    flex: 2,
                    child: Container(),
                  ),
                  Container(
                    height: 50,
                    child: AdWidget(ad: banner),
                  ),
                ],
              ),
            ),
        ),
      );
    }}
  </code></pre>