Compare commits
2 Commits
e9d1f67aa4
...
287295a345
| Author | SHA1 | Date | |
|---|---|---|---|
| 287295a345 | |||
|
|
81db383a47 |
20
CHANGELOG.md
@@ -1,5 +1,25 @@
|
|||||||
# Changelog — PluriWave
|
# Changelog — PluriWave
|
||||||
|
|
||||||
|
## [0.3.0] — 2026-04-04
|
||||||
|
|
||||||
|
### Fixes (prioridad alta — petición WhikY)
|
||||||
|
|
||||||
|
- **Audio en background** — `ServicioAudio` refactorizado para delegar toda la reproducción a `PluriWaveAudioHandler` (audio_service). La notificación foreground de Android mantiene el audio vivo al apagar pantalla. Handler inicializado en `main.dart` con `AudioService.init()` y registrado globalmente. `onTaskRemoved` libera recursos al cerrar la app. `mediaItem` propagado con nombre, artista y artwork de la emisora.
|
||||||
|
- **Filtrar emisoras rotas** — `ServicioRadio` añade `lastcheckok=1` en todas las peticiones a la API. Solo se devuelven emisoras verificadas como funcionales por Radio Browser.
|
||||||
|
- **Errores como SnackBar** — `EstadoRadio` emite errores de reproducción y búsqueda por `errorStream` (StreamController broadcast). `_PaginaPrincipalState.didChangeDependencies` suscribe al stream y muestra `SnackBar` flotante de 3 segundos. Los errores de carga de lista siguen como banner inline (no bloquean la UI).
|
||||||
|
- **Icono de app** — Generado con Stable Diffusion XL: diseño morado, ondas de radio blancas, estilo Material You. Todos los tamaños Android generados (mdpi/hdpi/xhdpi/xxhdpi/xxxhdpi, 48-192px). `ic_launcher_round` añadido. `android:roundIcon` en AndroidManifest.
|
||||||
|
|
||||||
|
### Ficheros modificados
|
||||||
|
| Fichero | Cambio |
|
||||||
|
|---|---|
|
||||||
|
| `lib/main.dart` | `AudioService.init()` + `registrarHandler()` |
|
||||||
|
| `lib/servicios/servicio_audio.dart` | Arquitectura background completa |
|
||||||
|
| `lib/servicios/servicio_radio.dart` | `lastcheckok=1` en todas las peticiones |
|
||||||
|
| `lib/estado/estado_radio.dart` | `errorStream` en lugar de campo `_error` |
|
||||||
|
| `lib/app.dart` | Listener `errorStream` → SnackBar + theme SnackBar |
|
||||||
|
| `android/app/src/main/AndroidManifest.xml` | `roundIcon` |
|
||||||
|
| `android/app/src/main/res/mipmap-*/` | Iconos generados (5 densidades) |
|
||||||
|
|
||||||
## [0.2.0] — 2026-04-04
|
## [0.2.0] — 2026-04-04
|
||||||
|
|
||||||
### Añadido
|
### Añadido
|
||||||
|
|||||||
@@ -8,7 +8,8 @@
|
|||||||
<application
|
<application
|
||||||
android:label="PluriWave"
|
android:label="PluriWave"
|
||||||
android:name="${applicationName}"
|
android:name="${applicationName}"
|
||||||
android:icon="@mipmap/ic_launcher">
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
android:roundIcon="@mipmap/ic_launcher_round">
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 544 B After Width: | Height: | Size: 3.2 KiB |
BIN
android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 3.2 KiB |
|
Before Width: | Height: | Size: 442 B After Width: | Height: | Size: 2.0 KiB |
BIN
android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 2.0 KiB |
|
Before Width: | Height: | Size: 721 B After Width: | Height: | Size: 4.6 KiB |
BIN
android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 4.6 KiB |
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 8.5 KiB |
BIN
android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 8.5 KiB |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 13 KiB |
BIN
android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
assets/images/ic_launcher_source.png
Normal file
|
After Width: | Height: | Size: 89 KiB |
35
lib/app.dart
@@ -27,7 +27,7 @@ class PluriWaveApp extends StatelessWidget {
|
|||||||
|
|
||||||
ThemeData _buildTheme(Brightness brightness) {
|
ThemeData _buildTheme(Brightness brightness) {
|
||||||
final colorScheme = ColorScheme.fromSeed(
|
final colorScheme = ColorScheme.fromSeed(
|
||||||
seedColor: const Color(0xFF6750A4), // Morado Material You
|
seedColor: const Color(0xFF6750A4),
|
||||||
brightness: brightness,
|
brightness: brightness,
|
||||||
);
|
);
|
||||||
return ThemeData(
|
return ThemeData(
|
||||||
@@ -41,6 +41,10 @@ class PluriWaveApp extends StatelessWidget {
|
|||||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||||
color: colorScheme.surfaceContainerLow,
|
color: colorScheme.surfaceContainerLow,
|
||||||
),
|
),
|
||||||
|
snackBarTheme: SnackBarThemeData(
|
||||||
|
behavior: SnackBarBehavior.floating,
|
||||||
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,6 +83,22 @@ class _PaginaPrincipalState extends State<_PaginaPrincipal> {
|
|||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didChangeDependencies() {
|
||||||
|
super.didChangeDependencies();
|
||||||
|
// Suscribir al stream de errores → SnackBar flotante
|
||||||
|
context.read<EstadoRadio>().errorStream.listen((msg) {
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(msg),
|
||||||
|
duration: const Duration(seconds: 3),
|
||||||
|
action: SnackBarAction(label: 'OK', onPressed: () {}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@@ -125,12 +145,15 @@ class _PaginaPrincipalState extends State<_PaginaPrincipal> {
|
|||||||
stream: estado.timer.tiempoRestanteStream,
|
stream: estado.timer.tiempoRestanteStream,
|
||||||
builder: (ctx, snap) {
|
builder: (ctx, snap) {
|
||||||
final t = snap.data ?? Duration.zero;
|
final t = snap.data ?? Duration.zero;
|
||||||
|
final h = t.inHours;
|
||||||
final m = t.inMinutes.remainder(60).toString().padLeft(2, '0');
|
final m = t.inMinutes.remainder(60).toString().padLeft(2, '0');
|
||||||
final s = t.inSeconds.remainder(60).toString().padLeft(2, '0');
|
final s = t.inSeconds.remainder(60).toString().padLeft(2, '0');
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
Text('${t.inHours > 0 ? "${t.inHours}h " : ""}${m}m ${s}s',
|
Text(
|
||||||
style: Theme.of(ctx).textTheme.headlineMedium),
|
'${h > 0 ? "${h}h " : ""}${m}m ${s}s',
|
||||||
|
style: Theme.of(ctx).textTheme.headlineMedium,
|
||||||
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
FilledButton.tonal(
|
FilledButton.tonal(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
@@ -146,13 +169,15 @@ class _PaginaPrincipalState extends State<_PaginaPrincipal> {
|
|||||||
else
|
else
|
||||||
Wrap(
|
Wrap(
|
||||||
spacing: 8,
|
spacing: 8,
|
||||||
children: [15, 30, 60, 90].map((min) => ActionChip(
|
children: [15, 30, 60, 90]
|
||||||
|
.map((min) => ActionChip(
|
||||||
label: Text('$min min'),
|
label: Text('$min min'),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
estado.iniciarTimer(min);
|
estado.iniciarTimer(min);
|
||||||
Navigator.pop(ctx);
|
Navigator.pop(ctx);
|
||||||
},
|
},
|
||||||
)).toList(),
|
))
|
||||||
|
.toList(),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'dart:async';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import '../modelos/emisora.dart';
|
import '../modelos/emisora.dart';
|
||||||
import '../servicios/servicio_audio.dart';
|
import '../servicios/servicio_audio.dart';
|
||||||
@@ -7,13 +8,18 @@ import '../servicios/servicio_timer.dart';
|
|||||||
|
|
||||||
/// Estado global de la app con ChangeNotifier (Provider).
|
/// Estado global de la app con ChangeNotifier (Provider).
|
||||||
///
|
///
|
||||||
/// Centraliza: reproductoor, favoritos, búsqueda, timer.
|
/// Errores de reproducción se emiten por [errorStream] para mostrar como
|
||||||
|
/// SnackBar — no bloquean la UI.
|
||||||
class EstadoRadio extends ChangeNotifier {
|
class EstadoRadio extends ChangeNotifier {
|
||||||
final ServicioAudio audio = ServicioAudio();
|
final ServicioAudio audio = ServicioAudio();
|
||||||
final ServicioFavoritos favoritos = ServicioFavoritos();
|
final ServicioFavoritos favoritos = ServicioFavoritos();
|
||||||
final ServicioRadio radio = ServicioRadio();
|
final ServicioRadio radio = ServicioRadio();
|
||||||
late final ServicioTimer timer;
|
late final ServicioTimer timer;
|
||||||
|
|
||||||
|
// Errores de reproducción → SnackBar en el UI
|
||||||
|
final _errorController = StreamController<String>.broadcast();
|
||||||
|
Stream<String> get errorStream => _errorController.stream;
|
||||||
|
|
||||||
List<Emisora> _populares = [];
|
List<Emisora> _populares = [];
|
||||||
List<Emisora> _tendencias = [];
|
List<Emisora> _tendencias = [];
|
||||||
List<Emisora> _resultadosBusqueda = [];
|
List<Emisora> _resultadosBusqueda = [];
|
||||||
@@ -21,7 +27,7 @@ class EstadoRadio extends ChangeNotifier {
|
|||||||
|
|
||||||
bool _cargandoPopulares = false;
|
bool _cargandoPopulares = false;
|
||||||
bool _cargandoBusqueda = false;
|
bool _cargandoBusqueda = false;
|
||||||
String? _error;
|
String? _errorCarga; // solo para errores de carga de lista (banner estático)
|
||||||
|
|
||||||
EstadoRadio() {
|
EstadoRadio() {
|
||||||
timer = ServicioTimer(audio);
|
timer = ServicioTimer(audio);
|
||||||
@@ -34,7 +40,7 @@ class EstadoRadio extends ChangeNotifier {
|
|||||||
List<Emisora> get listaFavoritos => _listafavoritos;
|
List<Emisora> get listaFavoritos => _listafavoritos;
|
||||||
bool get cargandoPopulares => _cargandoPopulares;
|
bool get cargandoPopulares => _cargandoPopulares;
|
||||||
bool get cargandoBusqueda => _cargandoBusqueda;
|
bool get cargandoBusqueda => _cargandoBusqueda;
|
||||||
String? get error => _error;
|
String? get error => _errorCarga;
|
||||||
Emisora? get emisoraActual => audio.emisoraActual;
|
Emisora? get emisoraActual => audio.emisoraActual;
|
||||||
Stream<EstadoReproduccion> get estadoStream => audio.estadoStream;
|
Stream<EstadoReproduccion> get estadoStream => audio.estadoStream;
|
||||||
|
|
||||||
@@ -47,7 +53,7 @@ class EstadoRadio extends ChangeNotifier {
|
|||||||
|
|
||||||
Future<void> cargarPopulares() async {
|
Future<void> cargarPopulares() async {
|
||||||
_cargandoPopulares = true;
|
_cargandoPopulares = true;
|
||||||
_error = null;
|
_errorCarga = null;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
try {
|
try {
|
||||||
final results = await Future.wait([
|
final results = await Future.wait([
|
||||||
@@ -57,7 +63,7 @@ class EstadoRadio extends ChangeNotifier {
|
|||||||
_populares = results[0];
|
_populares = results[0];
|
||||||
_tendencias = results[1];
|
_tendencias = results[1];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_error = 'Error al cargar emisoras: $e';
|
_errorCarga = 'Sin conexión a la API de radio';
|
||||||
} finally {
|
} finally {
|
||||||
_cargandoPopulares = false;
|
_cargandoPopulares = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
@@ -86,7 +92,8 @@ class EstadoRadio extends ChangeNotifier {
|
|||||||
tag: tag,
|
tag: tag,
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_error = 'Error en búsqueda: $e';
|
// Error de búsqueda → toast, no bloquear pantalla
|
||||||
|
_errorController.add('Error en la búsqueda. Comprueba tu conexión.');
|
||||||
} finally {
|
} finally {
|
||||||
_cargandoBusqueda = false;
|
_cargandoBusqueda = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
@@ -99,8 +106,8 @@ class EstadoRadio extends ChangeNotifier {
|
|||||||
radio.registrarClick(emisora.uuid); // fire & forget
|
radio.registrarClick(emisora.uuid); // fire & forget
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_error = 'No se puede reproducir esta emisora';
|
// Error de reproducción → SnackBar, no pintar en medio de la UI
|
||||||
notifyListeners();
|
_errorController.add('No se puede reproducir "${emisora.nombre}"');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,6 +136,7 @@ class EstadoRadio extends ChangeNotifier {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
|
_errorController.close();
|
||||||
audio.dispose();
|
audio.dispose();
|
||||||
timer.dispose();
|
timer.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
|
|||||||
@@ -1,7 +1,24 @@
|
|||||||
|
import 'package:audio_service/audio_service.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'app.dart';
|
import 'app.dart';
|
||||||
|
import 'servicios/servicio_audio.dart';
|
||||||
|
|
||||||
void main() {
|
Future<void> main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
// Inicializar audio_service para reproducción en background.
|
||||||
|
// El handler se registra globalmente para que ServicioAudio lo use.
|
||||||
|
final handler = await AudioService.init(
|
||||||
|
builder: () => PluriWaveAudioHandler(),
|
||||||
|
config: const AudioServiceConfig(
|
||||||
|
androidNotificationChannelId: 'es.freetimelab.pluriwave.audio',
|
||||||
|
androidNotificationChannelName: 'PluriWave Radio',
|
||||||
|
androidNotificationOngoing: true,
|
||||||
|
androidStopForegroundOnPause: true,
|
||||||
|
notificationColor: Color(0xFF6750A4),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
registrarHandler(handler);
|
||||||
|
|
||||||
runApp(const PluriWaveApp());
|
runApp(const PluriWaveApp());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,115 +5,89 @@ import '../modelos/emisora.dart';
|
|||||||
/// Estado de reproducción expuesto al UI.
|
/// Estado de reproducción expuesto al UI.
|
||||||
enum EstadoReproduccion { detenido, cargando, reproduciendo, pausado, error }
|
enum EstadoReproduccion { detenido, cargando, reproduciendo, pausado, error }
|
||||||
|
|
||||||
/// Wrapper sobre just_audio + audio_service para reproducción de radio en streaming.
|
// ─────────────────────────────────────────────────────────────
|
||||||
|
// Handler global — inicializado en main.dart con AudioService.init
|
||||||
|
// ─────────────────────────────────────────────────────────────
|
||||||
|
PluriWaveAudioHandler? _handlerGlobal;
|
||||||
|
|
||||||
|
/// Registra el handler. Llamar desde main.dart tras AudioService.init.
|
||||||
|
void registrarHandler(PluriWaveAudioHandler handler) {
|
||||||
|
_handlerGlobal = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wrapper de alto nivel para el UI.
|
||||||
///
|
///
|
||||||
/// ### Uso
|
/// Delega TODA la reproducción al [PluriWaveAudioHandler] para garantizar
|
||||||
/// ```dart
|
/// que el audio siga vivo en background con notificación foreground.
|
||||||
/// final servicio = ServicioAudio();
|
|
||||||
/// await servicio.inicializar();
|
|
||||||
/// await servicio.reproducir(emisora);
|
|
||||||
/// await servicio.pausar();
|
|
||||||
/// await servicio.detener();
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// ### Background audio
|
|
||||||
/// Para habilitar reproducción en background, el handler [PluriWaveAudioHandler]
|
|
||||||
/// debe registrarse en main.dart con [AudioService.init]. Si no está registrado,
|
|
||||||
/// just_audio seguirá funcionando en foreground.
|
|
||||||
class ServicioAudio {
|
class ServicioAudio {
|
||||||
final AudioPlayer _player = AudioPlayer();
|
PluriWaveAudioHandler get _handler {
|
||||||
Emisora? _emisoraActual;
|
assert(_handlerGlobal != null,
|
||||||
|
'ServicioAudio: handler no registrado. '
|
||||||
|
'Llama registrarHandler() en main.dart tras AudioService.init.');
|
||||||
|
return _handlerGlobal!;
|
||||||
|
}
|
||||||
|
|
||||||
EstadoReproduccion _estado = EstadoReproduccion.detenido;
|
Emisora? get emisoraActual => _handler.emisoraActual;
|
||||||
EstadoReproduccion get estado => _estado;
|
|
||||||
Emisora? get emisoraActual => _emisoraActual;
|
|
||||||
|
|
||||||
/// Stream de cambios de estado para el UI.
|
Stream<EstadoReproduccion> get estadoStream =>
|
||||||
Stream<EstadoReproduccion> get estadoStream => _player.playerStateStream.map(
|
_handler.playbackState.map((s) {
|
||||||
(s) {
|
if (s.processingState == AudioProcessingState.loading ||
|
||||||
if (s.processingState == ProcessingState.loading ||
|
s.processingState == AudioProcessingState.buffering) {
|
||||||
s.processingState == ProcessingState.buffering) {
|
|
||||||
return EstadoReproduccion.cargando;
|
return EstadoReproduccion.cargando;
|
||||||
}
|
}
|
||||||
if (s.playing) return EstadoReproduccion.reproduciendo;
|
if (s.playing) return EstadoReproduccion.reproduciendo;
|
||||||
if (s.processingState == ProcessingState.idle) return EstadoReproduccion.detenido;
|
if (s.processingState == AudioProcessingState.idle) {
|
||||||
|
return EstadoReproduccion.detenido;
|
||||||
|
}
|
||||||
return EstadoReproduccion.pausado;
|
return EstadoReproduccion.pausado;
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
/// Inicia la reproducción de la [emisora] indicada.
|
|
||||||
Future<void> reproducir(Emisora emisora) async {
|
Future<void> reproducir(Emisora emisora) async {
|
||||||
try {
|
final item = MediaItem(
|
||||||
_estado = EstadoReproduccion.cargando;
|
id: emisora.url,
|
||||||
|
title: emisora.nombre,
|
||||||
// Si es la misma emisora, reanudar sin recargar
|
artist: emisora.pais ?? '',
|
||||||
if (_emisoraActual?.uuid == emisora.uuid && _player.audioSource != null) {
|
album: 'PluriWave',
|
||||||
await _player.play();
|
artUri: emisora.favicon != null && emisora.favicon!.isNotEmpty
|
||||||
_estado = EstadoReproduccion.reproduciendo;
|
? Uri.tryParse(emisora.favicon!)
|
||||||
return;
|
: null,
|
||||||
|
extras: {'uuid': emisora.uuid},
|
||||||
|
);
|
||||||
|
await _handler.playMediaItem(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
_emisoraActual = emisora;
|
Future<void> pausar() => _handler.pause();
|
||||||
await _player.stop();
|
Future<void> reanudar() => _handler.play();
|
||||||
await _player.setUrl(emisora.url);
|
|
||||||
await _player.play();
|
|
||||||
_estado = EstadoReproduccion.reproduciendo;
|
|
||||||
} on PlayerException catch (_) {
|
|
||||||
_estado = EstadoReproduccion.error;
|
|
||||||
rethrow;
|
|
||||||
} catch (e) {
|
|
||||||
_estado = EstadoReproduccion.error;
|
|
||||||
rethrow;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Pausa la reproducción actual.
|
|
||||||
Future<void> pausar() async {
|
|
||||||
await _player.pause();
|
|
||||||
_estado = EstadoReproduccion.pausado;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reanuda si estaba pausado.
|
|
||||||
Future<void> reanudar() async {
|
|
||||||
if (_player.audioSource != null) {
|
|
||||||
await _player.play();
|
|
||||||
_estado = EstadoReproduccion.reproduciendo;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Alterna entre pausa y reproducción.
|
|
||||||
Future<void> togglePlay() async {
|
Future<void> togglePlay() async {
|
||||||
if (_player.playing) {
|
if (_handler.playbackState.value.playing) {
|
||||||
await pausar();
|
await pausar();
|
||||||
} else {
|
} else {
|
||||||
await reanudar();
|
await reanudar();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Detiene la reproducción y libera la fuente.
|
Future<void> detener() => _handler.stop();
|
||||||
Future<void> detener() async {
|
|
||||||
await _player.stop();
|
Future<void> setVolumen(double vol) => _handler.setVolume(vol.clamp(0.0, 1.0));
|
||||||
_emisoraActual = null;
|
|
||||||
_estado = EstadoReproduccion.detenido;
|
double get volumen => _handler.volumen;
|
||||||
|
bool get estaSonando => _handler.playbackState.value.playing;
|
||||||
|
|
||||||
|
/// No-op: el handler se limpia en main.dart al cerrar la app.
|
||||||
|
Future<void> dispose() async {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ajusta el volumen (0.0 - 1.0).
|
// ─────────────────────────────────────────────────────────────
|
||||||
Future<void> setVolumen(double volumen) async {
|
// AudioHandler — núcleo del audio en background
|
||||||
await _player.setVolume(volumen.clamp(0.0, 1.0));
|
// ─────────────────────────────────────────────────────────────
|
||||||
}
|
|
||||||
|
|
||||||
double get volumen => _player.volume;
|
/// Handler de audio_service.
|
||||||
bool get estaSonando => _player.playing;
|
|
||||||
|
|
||||||
/// Libera recursos. Llamar al destruir la pantalla raíz.
|
|
||||||
Future<void> dispose() async {
|
|
||||||
await _player.dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handler de audio_service para reproducción en background con notificación.
|
|
||||||
///
|
///
|
||||||
/// Registrar en main.dart:
|
/// Gestiona la reproducción con `just_audio` y mantiene la notificación
|
||||||
|
/// foreground activa mientras hay audio reproduciéndose.
|
||||||
|
///
|
||||||
|
/// ### Inicialización en main.dart
|
||||||
/// ```dart
|
/// ```dart
|
||||||
/// final handler = await AudioService.init(
|
/// final handler = await AudioService.init(
|
||||||
/// builder: () => PluriWaveAudioHandler(),
|
/// builder: () => PluriWaveAudioHandler(),
|
||||||
@@ -122,40 +96,72 @@ class ServicioAudio {
|
|||||||
/// androidNotificationChannelName: 'PluriWave Radio',
|
/// androidNotificationChannelName: 'PluriWave Radio',
|
||||||
/// androidNotificationOngoing: true,
|
/// androidNotificationOngoing: true,
|
||||||
/// androidStopForegroundOnPause: true,
|
/// androidStopForegroundOnPause: true,
|
||||||
|
/// androidNotificationIcon: 'drawable/ic_stat_radio',
|
||||||
/// ),
|
/// ),
|
||||||
/// );
|
/// );
|
||||||
|
/// registrarHandler(handler);
|
||||||
/// ```
|
/// ```
|
||||||
class PluriWaveAudioHandler extends BaseAudioHandler with SeekHandler {
|
class PluriWaveAudioHandler extends BaseAudioHandler with SeekHandler {
|
||||||
final AudioPlayer _player = AudioPlayer();
|
final AudioPlayer _player = AudioPlayer();
|
||||||
|
Emisora? emisoraActual;
|
||||||
|
double _volumen = 1.0;
|
||||||
|
double get volumen => _volumen;
|
||||||
|
|
||||||
PluriWaveAudioHandler() {
|
PluriWaveAudioHandler() {
|
||||||
|
_setupStreams();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _setupStreams() {
|
||||||
|
// Propagar estado del player → playbackState (lo que ve la notificación)
|
||||||
_player.playerStateStream.listen((state) {
|
_player.playerStateStream.listen((state) {
|
||||||
final playing = state.playing;
|
final playing = state.playing;
|
||||||
final proc = state.processingState;
|
final proc = state.processingState;
|
||||||
|
|
||||||
playbackState.add(playbackState.value.copyWith(
|
playbackState.add(playbackState.value.copyWith(
|
||||||
controls: [
|
controls: [
|
||||||
if (playing) MediaControl.pause else MediaControl.play,
|
if (playing) MediaControl.pause else MediaControl.play,
|
||||||
MediaControl.stop,
|
MediaControl.stop,
|
||||||
],
|
],
|
||||||
systemActions: const {MediaAction.seek},
|
systemActions: const {MediaAction.seek, MediaAction.stop},
|
||||||
androidCompactActionIndices: const [0],
|
androidCompactActionIndices: const [0],
|
||||||
processingState: {
|
processingState: _mapProcState(proc),
|
||||||
ProcessingState.idle: AudioProcessingState.idle,
|
|
||||||
ProcessingState.loading: AudioProcessingState.loading,
|
|
||||||
ProcessingState.buffering: AudioProcessingState.buffering,
|
|
||||||
ProcessingState.ready: AudioProcessingState.ready,
|
|
||||||
ProcessingState.completed: AudioProcessingState.completed,
|
|
||||||
}[proc]!,
|
|
||||||
playing: playing,
|
playing: playing,
|
||||||
|
bufferedPosition: _player.bufferedPosition,
|
||||||
|
speed: _player.speed,
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Actualizar bufferedPosition
|
||||||
|
_player.bufferedPositionStream.listen((pos) {
|
||||||
|
playbackState.add(playbackState.value.copyWith(bufferedPosition: pos));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
AudioProcessingState _mapProcState(ProcessingState state) {
|
||||||
|
return switch (state) {
|
||||||
|
ProcessingState.idle => AudioProcessingState.idle,
|
||||||
|
ProcessingState.loading => AudioProcessingState.loading,
|
||||||
|
ProcessingState.buffering => AudioProcessingState.buffering,
|
||||||
|
ProcessingState.ready => AudioProcessingState.ready,
|
||||||
|
ProcessingState.completed => AudioProcessingState.completed,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> playMediaItem(MediaItem item) async {
|
Future<void> playMediaItem(MediaItem item) async {
|
||||||
mediaItem.add(item);
|
mediaItem.add(item);
|
||||||
|
try {
|
||||||
|
await _player.stop();
|
||||||
await _player.setUrl(item.id);
|
await _player.setUrl(item.id);
|
||||||
await _player.play();
|
await _player.play();
|
||||||
|
} on PlayerException catch (e) {
|
||||||
|
playbackState.add(playbackState.value.copyWith(
|
||||||
|
processingState: AudioProcessingState.error,
|
||||||
|
errorMessage: e.message ?? 'Error de reproducción',
|
||||||
|
errorCode: e.code,
|
||||||
|
));
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -167,9 +173,22 @@ class PluriWaveAudioHandler extends BaseAudioHandler with SeekHandler {
|
|||||||
@override
|
@override
|
||||||
Future<void> stop() async {
|
Future<void> stop() async {
|
||||||
await _player.stop();
|
await _player.stop();
|
||||||
|
emisoraActual = null;
|
||||||
|
mediaItem.add(null);
|
||||||
await super.stop();
|
await super.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> seek(Duration position) => _player.seek(position);
|
Future<void> seek(Duration position) => _player.seek(position);
|
||||||
|
|
||||||
|
Future<void> setVolume(double vol) async {
|
||||||
|
_volumen = vol.clamp(0.0, 1.0);
|
||||||
|
await _player.setVolume(_volumen);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onTaskRemoved() async {
|
||||||
|
await stop();
|
||||||
|
await _player.dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,11 @@ class ServicioRadio {
|
|||||||
|
|
||||||
Future<List<Emisora>> _get(String path, Map<String, String> params) async {
|
Future<List<Emisora>> _get(String path, Map<String, String> params) async {
|
||||||
final servidor = await _servidor();
|
final servidor = await _servidor();
|
||||||
final uri = _uri(servidor, path, params);
|
// lastcheckok=1 filtra emisoras que la API verificó como funcionales
|
||||||
|
final uri = _uri(servidor, path, {
|
||||||
|
'lastcheckok': '1',
|
||||||
|
...params,
|
||||||
|
});
|
||||||
try {
|
try {
|
||||||
final resp = await http.get(uri, headers: {
|
final resp = await http.get(uri, headers: {
|
||||||
'User-Agent': 'PluriWave/0.1.0 (es.freetimelab.pluriwave)',
|
'User-Agent': 'PluriWave/0.1.0 (es.freetimelab.pluriwave)',
|
||||||
|
|||||||