flutter_radio_player 5.0.0-dev.1
flutter_radio_player: ^5.0.0-dev.1 copied to clipboard
Online Radio Player for Flutter with background playback, lock screen controls, and streaming support for Android and iOS.
example/lib/main.dart
import 'package:material_ui/material_ui.dart';
import 'package:flutter_radio_player/flutter_radio_player.dart';
const _sources = [
RadioSource(
url: 'https://s2-webradio.antenne.de/chillout?icy=https',
title: 'Antenne Chillout',
),
RadioSource(
url:
'https://radio.lotustechnologieslk.net:2020/stream/sunfmgarden?icy=https',
title: 'SunFM - Sri Lanka',
),
];
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Radio Player',
theme: ThemeData(
colorSchemeSeed: Colors.deepPurple,
brightness: Brightness.dark,
useMaterial3: true,
),
home: const PlayerScreen(),
);
}
}
class PlayerScreen extends StatefulWidget {
const PlayerScreen({super.key});
@override
State<PlayerScreen> createState() => _PlayerScreenState();
}
class _PlayerScreenState extends State<PlayerScreen> {
final _player = FlutterRadioPlayer.instance;
double _volume = 0.5;
@override
void initState() {
super.initState();
_player.initialize(_sources, playWhenReady: true);
}
@override
void dispose() {
_player.dispose();
super.dispose();
}
void _jumpBy(RadioPlayerState state, int offset) {
final current = state.currentIndex ?? 0;
final target = (current + offset + _sources.length) % _sources.length;
_player.jumpToSourceAtIndex(target);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flutter Radio Player')),
body: StreamBuilder<RadioPlayerState>(
initialData: _player.state,
stream: _player.stateStream,
builder: (context, snapshot) {
final state = snapshot.requireData;
final isPlaying = state.status == PlaybackStatus.playing ||
state.status == PlaybackStatus.loading;
final error = state.error;
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Text(
state.nowPlaying?.title ??
state.currentSource?.title ??
'No track info',
style: Theme.of(context).textTheme.titleLarge,
),
),
if (error != null)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
'${error.code.name}: ${error.message}',
style:
TextStyle(color: Theme.of(context).colorScheme.error),
textAlign: TextAlign.center,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
iconSize: 36,
onPressed: () => _jumpBy(state, -1),
icon: const Icon(Icons.skip_previous_rounded),
),
IconButton(
iconSize: 48,
onPressed: () =>
isPlaying ? _player.pause() : _player.play(),
icon: Icon(
isPlaying
? Icons.pause_rounded
: Icons.play_arrow_rounded,
),
),
IconButton(
iconSize: 36,
onPressed: () => _jumpBy(state, 1),
icon: const Icon(Icons.skip_next_rounded),
),
],
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 32),
child: Row(
children: [
const Icon(Icons.volume_down_rounded),
Expanded(
child: Slider(
value: _volume,
onChanged: (value) {
setState(() => _volume = value);
_player.setVolume(value);
},
),
),
const Icon(Icons.volume_up_rounded),
],
),
),
],
);
},
),
);
}
}