thyagoluciano / flutter_radio

Radio station streaming example with ExoPlayer and Flutter.
GNU General Public License v3.0
86 stars 41 forks source link

Player State management issue #44

Open ashishpatel1992 opened 4 years ago

ashishpatel1992 commented 4 years ago

I am using bottom navigation for switching tabs. On my First tab i have the flutter_radio player. When i start my player it works perfectly and I switch to another tab (it works in background perfectly) But When I switch back this flutter_radio tab it looses its state and when i click play it starts playing the stream again, instead of pausing it, due to which I see two different streams playing at same time.

here is my code:

import 'package:flutter/material.dart';
import 'package:flutter_radio/flutter_radio.dart';

class RadioPlayer extends StatefulWidget {
  @override
  _RadioPlayerState createState() => _RadioPlayerState();
}

class _RadioPlayerState extends State<RadioPlayer> {

  static const streamUrl =
      "http://ia802708.us.archive.org/3/items/count_monte_cristo_0711_librivox/count_of_monte_cristo_001_dumas.mp3";
  static bool isPlayingStatus = false;
  static bool isPlaying = false;
  final Color _playerButtonColor = Colors.white;
  final _playerSize = 90.0;

  @override
  void initState() {
    super.initState();
    audioStart();
    playingStatus();
  }

  Future<void> audioStart() async {
    await FlutterRadio.audioStart();
    print('Audio Start OK');
  }

  Future playingStatus() async {
    bool isP = await FlutterRadio.isPlaying();
    setState(() {
      isPlayingStatus = isP;
    });
  }

  IconData _playerIcon = Icons.play_circle_filled;

  @override
  Widget build(BuildContext context) {
    return Column(
//      crossAxisAlignment: CrossAxisAlignment.center,
//      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
//        Text("Live TSQ Radio"),
        FlatButton(
          child: Icon(
            _playerIcon,
            size: _playerSize,
            color: _playerButtonColor,
          ),
          onPressed: () {
            playingStatus();
            print(isPlaying);
            setState(() {
              isPlaying = isPlaying ? false : true;
              _playerIcon = isPlaying ? Icons.stop : Icons.play_circle_filled;
              FlutterRadio.playOrPause(url: streamUrl);

              print(isPlaying);
            });
          },
        ),
      ],
    );
  }

}
grupoinnovar commented 4 years ago

hi bro try commenting in your onPressed: () method

//playingStatus();

cheers