Salih Balsever
Salih Balsever

Reputation: 199

To prevent playing 2 radios at the same time. Making only 1 radio play

When I come to the radio page, I can start it automatically. The radio continues to play when switching between pages. When we come to the radio page again, the same radio starts again, that is, 2 radios are playing at the same time. If the radio is playing when I come back to the radio page, let's not start a new radio, but keep the radio playing. So I want no change. How can I do that.

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://fm898.online/mystream.mp3";

  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ı");
    FlutterRadio.play(url: url);
    isPlaying = !isPlaying;
    isVisible = !isVisible;
    
  }
  Future<bool>_onBackPressed(){
    FlutterRadio.stop();
    Navigator.pop(
        context, MaterialPageRoute(builder: (_) => Profil()));
  }
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onBackPressed,
      child: 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.playOrPause(url: url);
                              isPlaying = !isPlaying;
                              isVisible = !isVisible;
                              print("tıkladı");
                            });
                          },
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 50,)
                ],
              ),
            ),
          )),
    );
  }

}

Upvotes: 1

Views: 100

Answers (1)

Jonathan Ixcayau
Jonathan Ixcayau

Reputation: 713

I Think you have two options

1: Use a static ver to save the value if the radio is playing and validate in audioStart function to avoid play again

2: Use a provider, likewise to save the value globally

Also you can try adding this changes

 String url= "https://fm898.online/mystream.mp3";

  static 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ı");
   
    if(!isPlaying){
       FlutterRadio.play(url: url);
       isPlaying = !isPlaying;
    }
    isVisible = !isVisible;
    
  }

Upvotes: 1

Related Questions