Search code examples
flutterdartaudio-streamingsetstatebottombar

The radio starts. But it doesn't stop. It doesn't stop when I switch pages in the project


I want the radio to stop when switching between pages. It works the first time I press the stop and start button, but when I press it again when I want to stop it does not stop. The radio is always on and never turns off. When I close the app completely, the radio stops. I want to do both stop and resume operation on the same button, and I want it to stop when I switch between pages. how can i solve?

code here:

import 'package:flutter/material.dart';
import 'package:flutter_radio/flutter_radio.dart';
class RadioSayfasi extends StatefulWidget {
  @override
  _RadioSayfasiState createState() => _RadioSayfasiState();
}

class _RadioSayfasiState extends State<RadioSayfasi> {
  String url= "https://player.web.tr/listen/d695bfdfb2710f7c53feb05550da66ef";

  bool isPlaying= false;
  bool isVisible= true;
  
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    audioStart();
  }

  Future <void> audioStart() async {
    await FlutterRadio.audioStart();
    print("Radio Başladı");
  }
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: 'IndieXL Online Radio',
        debugShowCheckedModeBanner: false,
        home: new Scaffold(
          appBar: new AppBar(
            title: const Text('FM Radio'),
            backgroundColor: Colors.blueGrey.shade900,
            centerTitle: true,
          ),
          body: Container(
            color: Colors.blueGrey.shade900,
            child: new Column(
              children: <Widget>[
                Expanded(
                  flex: 7,
                  child: Icon(
                    Icons.radio, size: 250,
                    color: Colors.white,
                  ),
                ),
                Expanded(
                  flex: 2,
                  child: Padding(
                    padding: const EdgeInsets.only(right: 40),
                    child: Align(
                      alignment: FractionalOffset.center,
                      child: IconButton(icon: isPlaying? Icon(
                        Icons.pause_circle_outline,
                        size: 80,
                        color: Colors.white,
                      )
                          : Icon(
                        Icons.play_circle_outline,
                        color: Colors.white,
                        size: 80,
                      ),
                        onPressed: (){
                          setState(() {
                            FlutterRadio.play(url: url);
                            isPlaying = !isPlaying;
                            isVisible = !isVisible;
                            print("tıkladı");
                          });
                        },
                      ),
                    ),
                  ),
                ),
                SizedBox(height: 50,)
              ],
            ),
          ),
        ));
  }

}

Solution

  • I'm guessing the library you mention, https://github.com/thyagoluciano/flutter_radio

    To stop radio stream when leaving the screen just override the dispose method

    https://github.com/thyagoluciano/flutter_radio/blob/master/lib/flutter_radio.dart#L67

    @action
    dispose() async {
        await FlutterRadio.stop();
    }
    

    To start or pause radio stream https://github.com/thyagoluciano/flutter_radio/blob/master/lib/flutter_radio.dart#L22

    onPressed: (){ 
        setState(() { 
            //FlutterRadio.play(url: url); 
            FlutterRadio.playOrPause(url: url); 
            isPlaying = !isPlaying; 
            isVisible = !isVisible; 
            print("tıkladı"); 
        }); 
    },