radio_player 1.7.0 radio_player: ^1.7.0 copied to clipboard
A Flutter plugin to play streaming audio content with background support and lock screen controls.
/*
* main.dart
*
* Created by Ilia Chirkunov <xc@yar.net> on 28.12.2020.
*/
import 'package:flutter/material.dart';
import 'package:radio_player/radio_player.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
RadioPlayer _radioPlayer = RadioPlayer();
bool isPlaying = false;
List<String>? metadata;
@override
void initState() {
super.initState();
initRadioPlayer();
}
void initRadioPlayer() {
_radioPlayer.setChannel(
title: 'Radio Player',
url: 'http://stream-uk1.radioparadise.com/aac-320',
imagePath: 'assets/cover.jpg',
);
_radioPlayer.stateStream.listen((value) {
setState(() {
isPlaying = value;
});
});
_radioPlayer.metadataStream.listen((value) {
setState(() {
metadata = value;
});
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('Radio Player'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FutureBuilder(
future: _radioPlayer.getArtworkImage(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
Image artwork;
if (snapshot.hasData) {
artwork = snapshot.data;
} else {
artwork = Image.asset(
'assets/cover.jpg',
fit: BoxFit.cover,
);
}
return Container(
height: 180,
width: 180,
child: ClipRRect(
child: artwork,
borderRadius: BorderRadius.circular(10.0),
),
);
},
),
SizedBox(height: 20),
Text(
metadata?[0] ?? 'Metadata',
softWrap: false,
overflow: TextOverflow.fade,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 24),
),
Text(
metadata?[1] ?? '',
softWrap: false,
overflow: TextOverflow.fade,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
SizedBox(height: 20),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
isPlaying ? _radioPlayer.pause() : _radioPlayer.play();
},
tooltip: 'Control button',
child: Icon(
isPlaying ? Icons.pause_rounded : Icons.play_arrow_rounded,
),
),
),
);
}
}