广播开始。但它并没有停止。当我在项目中切换页面时它不会停止
The radio starts. But it doesn't stop. It doesn't stop when I switch pages in the project
我希望在切换页面时收音机停止。它在我第一次按下停止和开始按钮时起作用,但是当我想停止时再次按下它时它不会停止。收音机一直开着,从不关掉。当我完全关闭应用程序时,收音机停止了。我想对同一个按钮进行停止和恢复操作,并且我希望它在我切换页面时停止。我该如何解决?
代码在这里:
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,)
],
),
),
));
}
}
我猜你提到的图书馆,https://github.com/thyagoluciano/flutter_radio
要在离开屏幕时停止广播流,只需重写 dispose 方法
https://github.com/thyagoluciano/flutter_radio/blob/master/lib/flutter_radio.dart#L67
@action
dispose() async {
await FlutterRadio.stop();
}
开始或暂停广播流
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ı");
});
},
我希望在切换页面时收音机停止。它在我第一次按下停止和开始按钮时起作用,但是当我想停止时再次按下它时它不会停止。收音机一直开着,从不关掉。当我完全关闭应用程序时,收音机停止了。我想对同一个按钮进行停止和恢复操作,并且我希望它在我切换页面时停止。我该如何解决?
代码在这里:
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,)
],
),
),
));
}
}
我猜你提到的图书馆,https://github.com/thyagoluciano/flutter_radio
要在离开屏幕时停止广播流,只需重写 dispose 方法
https://github.com/thyagoluciano/flutter_radio/blob/master/lib/flutter_radio.dart#L67
@action
dispose() async {
await FlutterRadio.stop();
}
开始或暂停广播流 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ı");
});
},