system_ringtone 0.0.1
system_ringtone: ^0.0.1 copied to clipboard
A production-ready Flutter plugin to fetch and play Android system ringtones, alarms, and notification sounds using RingtoneManager.
import 'package:flutter/material.dart';
import 'package:system_ringtone/system_ringtone.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<RingtoneItem> _sounds = [];
bool _isLoading = false;
RingtoneItem? _playingSound;
double _volume = 1.0;
@override
void initState() {
super.initState();
_fetchSounds();
}
Future<void> _fetchSounds() async {
setState(() => _isLoading = true);
try {
final sounds = await AndroidRingtonePlayer.getRingtones(
notification: true,
alarm: true,
ringtone: true,
);
setState(() {
_sounds = sounds;
});
} finally {
setState(() => _isLoading = false);
}
}
Future<void> _playSound(RingtoneItem sound) async {
setState(() => _playingSound = sound);
await AndroidRingtonePlayer.play(sound.uri, volume: _volume);
}
Future<void> _stopSound() async {
setState(() => _playingSound = null);
await AndroidRingtonePlayer.stop();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(colorSchemeSeed: Colors.blue, useMaterial3: true),
home: Scaffold(
appBar: AppBar(
title: const Text('Android Ringtone Plugin'),
actions: [
IconButton(
icon: const Icon(Icons.refresh),
onPressed: _fetchSounds,
),
],
),
body: Column(
children: [
if (_playingSound != null)
Container(
color: Theme.of(context).colorScheme.primaryContainer,
padding: const EdgeInsets.all(16),
child: Row(
children: [
const Icon(Icons.music_note),
const SizedBox(width: 8),
Expanded(
child: Text(
'Playing: ${_playingSound!.title}',
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
IconButton(
icon: const Icon(Icons.stop),
onPressed: _stopSound,
),
],
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
const Text('Volume:'),
Expanded(
child: Slider(
value: _volume,
onChanged: (val) {
setState(() {
_volume = val;
});
if (_playingSound != null) {
_playSound(_playingSound!);
}
},
),
),
],
),
),
const Divider(),
Expanded(
child: _isLoading
? const Center(child: CircularProgressIndicator())
: _sounds.isEmpty
? const Center(child: Text('No sounds found'))
: ListView.builder(
itemCount: _sounds.length,
itemBuilder: (context, index) {
final sound = _sounds[index];
final isPlaying = _playingSound?.uri == sound.uri;
return ListTile(
leading: Icon(
isPlaying ? Icons.volume_up : Icons.notifications,
),
title: Text(sound.title),
subtitle: Text(
sound.uri,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
onTap: () => _playSound(sound),
trailing: IconButton(
icon: Icon(
isPlaying ? Icons.stop : Icons.play_arrow,
),
onPressed: () {
if (isPlaying) {
_stopSound();
} else {
_playSound(sound);
}
},
),
);
},
),
),
],
),
),
);
}
}