72 lines
1.7 KiB
Dart
72 lines
1.7 KiB
Dart
import 'package:audioplayers/audioplayers.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:styled_widget/styled_widget.dart';
|
|
|
|
class AudioPlayerWidgetController extends GetxController {
|
|
final String title;
|
|
final String url;
|
|
|
|
AudioPlayerWidgetController({
|
|
required this.title,
|
|
required this.url,
|
|
});
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
|
|
player = AudioPlayer();
|
|
player.play(UrlSource(url));
|
|
|
|
player.onPlayerStateChanged.listen((event) {
|
|
_playerState.value = event;
|
|
});
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
player.dispose();
|
|
super.onClose();
|
|
}
|
|
|
|
late AudioPlayer player;
|
|
|
|
final _playerState = PlayerState.stopped.obs;
|
|
get playerState => _playerState.value;
|
|
}
|
|
|
|
class AudioPlayerWidget extends GetView<AudioPlayerWidgetController> {
|
|
const AudioPlayerWidget({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListTile(
|
|
leading: Obx(
|
|
() => controller.playerState == PlayerState.playing
|
|
? IconButton(
|
|
onPressed: () => controller.player.pause(),
|
|
icon: const Icon(Icons.pause),
|
|
)
|
|
: IconButton(
|
|
onPressed: () => controller.player.resume(),
|
|
icon: const Icon(Icons.play_arrow),
|
|
),
|
|
),
|
|
title: const Text("Проигрывается"),
|
|
subtitle: Text(controller.title),
|
|
).card().paddingAll(16).center();
|
|
}
|
|
|
|
static AudioPlayerWidget create(
|
|
{required String url, required String title}) {
|
|
Get.lazyPut<AudioPlayerWidgetController>(
|
|
() => AudioPlayerWidgetController(
|
|
title: title,
|
|
url: url,
|
|
),
|
|
);
|
|
|
|
return const AudioPlayerWidget();
|
|
}
|
|
}
|