title: Flutter audio_service play audio in background when mobile screen is locked
body: <p>I made a radio player app with the code below. Everything works fine when the mobile screen is turned on. But when i turn off my mobile screen the radio stops playing at about 5-8 minutes. I got some tips about using flutter audio_service. (<a href="https://pub.dev/packages/audio_service" rel="nofollow noreferrer">https://pub.dev/packages/audio_service</a>) But i am confused from where should i start. Should i recode again or i can modify this code. Somebody please help me. It would be a grace. Thankyou in advance.</p>
  <pre><code>import 'package:audioplayers/audioplayers.dart';
  import 'package:flutter/material.dart';
  
  class Radio1 extends StatefulWidget {
    @override
    _Radio1State createState() =&gt; _Radio1State();
  }
  
  class _Radio1State extends State&lt;Radio1&gt; {
    AudioPlayer audioPlayer = AudioPlayer();
  
    @override
    void initState() {
      super.initState();
      AudioPlayer.logEnabled = true;
    }
  
    bool _isPlaying = false;
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Container(
          width: MediaQuery.of(context).size.width,
          child: SingleChildScrollView(
            child: Column(
              children: &lt;Widget&gt;[
                //new
                SizedBox(
                  height: 50,
                ),
  
                //Icon(
                // Icons.arrow_drop_down,
                //size: 40,
                //),
  
                //new
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 20, vertical: 50),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    boxShadow: [
                      BoxShadow(
                        color: Color(0x46000000),
                        offset: Offset(0, 20),
                        spreadRadius: 0,
                        blurRadius: 30,
                      ),
                      BoxShadow(
                        color: Color(0x11000000),
                        offset: Offset(0, 10),
                        spreadRadius: 0,
                        blurRadius: 30,
                      ),
                    ],
                  ),
  //new
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(20),
                    child: Image(
                      image: AssetImage(&quot;assets/radiologo.jpg&quot;),
                      width: MediaQuery.of(context).size.width * 0.7,
                      height: MediaQuery.of(context).size.width * 0.7,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                Text(
                  &quot;sample text&quot;,
                  style: TextStyle(fontSize: 30, fontWeight: FontWeight.w500),
                ),
                Text(
                  &quot;(sample text)&quot;,
                  style: TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
                ),
                /* Slider(
                  value: 10,
                  onChanged: (v) {},
                  max: 170,
                  min: 0,
                  activeColor: Color(0xFF5E35B1),
                ), */
                Text(
                  &quot;sample text.&quot;,
                  style: TextStyle(fontSize: 10, fontWeight: FontWeight.w500),
                ),
  
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    IconButton(
                      icon: _isPlaying == false
                          ? Icon(Icons.play_circle_outline)
                          : Icon(Icons.pause_circle_outline),
                      iconSize: 60.0,
                      onPressed: () {
                        getAudio();
                      },
                    ),
                    IconButton(
                      icon: Icon(Icons.stop),
                      iconSize: 40,
                      onPressed: () {
                        stopAudio();
                      },
                    ),
                    //new line
                  ],
                ),
              ],
            ),
          ),
        ),
      );
    }
  
    void getAudio() async {
      var url = &quot;http://ia802708.us.archive.org/3/items/count_monte_cristo_0711_librivox/count_of_monte_cristo_001_dumas.mp3&quot;;
      if (_isPlaying) {
        var res = await audioPlayer.pause();
        if (res == 1) {
          setState(() {
            _isPlaying = false;
          });
        }
      } else {
        var res = await audioPlayer.play(url);
        if (res == 1) {
          setState(() {
            _isPlaying = true;
          });
        }
      }
    }
  
    void stopAudio() async {
      int res = await audioPlayer.stop();
  
      if (res == 1) {
        setState(() {
          _isPlaying = false;
        });
      }
    }
  
    void releaseAUdio() async {
      await audioPlayer.stop();
      await audioPlayer.release();
    }
  
    @override
    void dispose() {
      super.dispose();
      releaseAUdio();
    }
  }
  </code></pre>