Sithira / FlutterRadioPlayer

Flutter Radio Player, A Plugin to handle streaming audio without a hassle
MIT License
139 stars 64 forks source link

Changing stations wont play straight away #27

Closed redimongo closed 4 years ago

redimongo commented 4 years ago

So Not sure what I am doing wrong, or if this is a known bug. But it seems that me using the following code does not change stream and play stream.

I am noticing it seem to be default to pause no matter what:

I/javaClass(25595): Attaching player as a foreground notification...
I/javaClass(25595): Received meta: ICY: title="Supernal S01E01 Part 2", url="null", rawMetadata.length="37"
I/javaClass(25595): Pushing Event: flutter_radio_paused
I/javaClass(25595): onPlayerStateChanged: PAUSED
I/javaClass(25595): Received status: flutter_radio_paused
I/javaClass(25595): Attaching player as a foreground notification...

instead I still have to manually click the play pause button.

  Future<void> changestation() async{
    try {
      await _flutterRadioPlayer.setUrl('http://stream.radiomedia.com.au:8015/stream', "false");
      await _flutterRadioPlayer.play();
    } on PlatformException {
      print("Exception occurred while trying to register the services.");
    }

  }

Full code:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_radio_player/flutter_radio_player.dart';
import './screens/trackswidget.dart';
import 'about_widget.dart';

class Home extends StatefulWidget {
  var playerState = FlutterRadioPlayer.flutter_radio_paused;

  var volume = 0.8;

  @override
  State<StatefulWidget> createState() {
    return _HomeState();
  }
}

class _HomeState extends State<Home> {

  bool check = true;
  int _currentIndex = 0;
  FlutterRadioPlayer _flutterRadioPlayer = new FlutterRadioPlayer();

  @override
  void initState() {
    super.initState();
    initRadioService();
  }

  Future<void> initRadioService() async {
    try {
      await _flutterRadioPlayer.init(
          "DRN1", "Live", "http://stream.radiomedia.com.au:8003/stream", "false");
    } on PlatformException {
      print("Exception occurred while trying to register the services.");
    }
    //await _flutterRadioPlayer.play();
  }

  Future<void> changestation() async{
    try {
      await _flutterRadioPlayer.setUrl('http://stream.radiomedia.com.au:8015/stream', "false");
      await _flutterRadioPlayer.play();
    } on PlatformException {
      print("Exception occurred while trying to register the services.");
    }

  }

  final List<Widget> _children = [TracksWidget(),
    AboutWidget()];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text('DRN',style: TextStyle(color: Colors.white, fontSize: 40.0),),
              const Text('1', style: TextStyle(color: Colors.red, fontSize: 40.0),)
            ],
        ),
      ),
      //body: _children[_currentIndex],
      body:
      Row(
        children: <Widget>[
          Expanded(
            child: new RaisedButton(
              padding: const EdgeInsets.all(8.0),
              textColor: Colors.white,
              color: Colors.blue,
              onPressed: () async { await changestation(); },

              child: new Text("Change Station"),
            ),
          ),
          Expanded(
            child: Text('Craft beautiful UIs', textAlign: TextAlign.center),
          ),
          Expanded(
            child: FittedBox(
              fit: BoxFit.contain, // otherwise the logo will be tiny
              child: const FlutterLogo(),
            ),
          ),
        ],
      ),

/*    body: Center(
        child: Column(
          children: <Widget>[
            StreamBuilder(
                stream: _flutterRadioPlayer.isPlayingStream,
                initialData: widget.playerState,
                builder:
                    (BuildContext context, AsyncSnapshot<String> snapshot) {
                  String returnData = snapshot.data;
                  print("object data: " + returnData);
                  switch (returnData) {
                    case FlutterRadioPlayer.flutter_radio_stopped:
                      return RaisedButton(
                          child: Text("Start listening now"),
                          onPressed: () async {
                            await initRadioService();
                          });
                      break;
                    case FlutterRadioPlayer.flutter_radio_loading:
                      return Text("Loading stream...");
                    case FlutterRadioPlayer.flutter_radio_error:
                      return RaisedButton(
                          child: Text("Retry ?"),
                          onPressed: () async {
                            await initRadioService();
                          });
                      break;
                    default:
                      return Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            IconButton(
                                onPressed: () async {
                                  print("button press data: " +
                                      snapshot.data.toString());
                                  await _flutterRadioPlayer.playOrPause();
                                },
                                icon: snapshot.data ==
                                    FlutterRadioPlayer
                                        .flutter_radio_playing
                                    ? Icon(Icons.pause)
                                    : Icon(Icons.play_arrow)),
                            IconButton(
                                onPressed: () async {
                                  await _flutterRadioPlayer.stop();
                                },
                                icon: Icon(Icons.stop))
                          ]);
                      break;
                  }
                }),
            Slider(
                value: widget.volume,
                min: 0,
                max: 1.0,
                onChanged: (value) => setState(() {
                  widget.volume = value;
                  _flutterRadioPlayer.setVolume(widget.volume);
                })),
            Text("Volume: " + (widget.volume * 100).toStringAsFixed(0))
          ],
        ),
      ),
*/

      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: Container(
        height: 80.0,
        width: 80.0,
        child: FittedBox(
          child:FloatingActionButton(
                  onPressed: () => {
                    setState((){
                      if(check)
                      {
                        check = false;
                         _flutterRadioPlayer.play();
                        print("false");
                      }
                      else
                      {
                        check = true;
                         _flutterRadioPlayer.pause();
                        print("true");
                      }

                    }),
                    },
         child: Icon(check ? Icons.play_arrow: Icons.pause),
                backgroundColor: Colors.black,
                mini: true,
              ),
        ),

      ),
      bottomNavigationBar: BottomAppBar(
        shape: CircularNotchedRectangle(),
        child: Container(
          decoration:
          new BoxDecoration(color: new Color(0xFFFF0000)),
          height: 75,
          child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(left: 28.0),
                icon: Icon(Icons.home),
                onPressed: () {
                  setState(() {

                    onTabTapped(0);
                  });
                },
              ),
          /*    IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(right: 28.0),
                icon: Icon(Icons.search),
                onPressed: () {
                  setState(() {
                    onTabTapped(1);
                  });
                },
              ),
              IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(left: 28.0),
                icon: Icon(Icons.notifications),
                onPressed: () {
                  setState(() {
                    onTabTapped(2);
                  });
                },
              ),*/
              IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(right: 28.0),
                icon: Icon(Icons.info_outline),
                onPressed: () {
                  setState(() {
                    onTabTapped(1);
                  });
                },
              )
            ],
          ),
        ),
      ),// new

    );
  }

  void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }
}
Sithira commented 4 years ago

await _flutterRadioPlayer.setUrl('http://stream.radiomedia.com.au:8015/stream', "true"); will do the trick for you 😄 .