Sonnat Project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.1 KiB

  1. import 'package:chewie_audio/chewie_audio.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:sonnat/core/theme/app_colors.dart';
  4. import 'package:sonnat/core/widgets/global_loading.dart';
  5. import 'package:video_player/video_player.dart';
  6. class AudioPlayer extends StatefulWidget {
  7. final String url;
  8. const AudioPlayer({super.key, required this.url});
  9. @override
  10. State<AudioPlayer> createState() => _AudioPlayerState();
  11. }
  12. class _AudioPlayerState extends State<AudioPlayer> {
  13. ChewieAudioController? chewieController;
  14. late VideoPlayerController videoPlayerController;
  15. @override
  16. void initState() {
  17. super.initState();
  18. videoPlayerController = VideoPlayerController.network(widget.url);
  19. }
  20. Future<bool> initAudio() async {
  21. await videoPlayerController.initialize();
  22. chewieController = ChewieAudioController(
  23. videoPlayerController: videoPlayerController,
  24. autoPlay: false,
  25. looping: false,
  26. );
  27. return Future.value(true);
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. return FutureBuilder(
  32. future: initAudio(),
  33. builder: (context, snapshot) {
  34. if (snapshot.hasData) {
  35. return Container(
  36. decoration: BoxDecoration(
  37. boxShadow: const [
  38. BoxShadow(
  39. color: AppColors.gray2,
  40. blurRadius: 1,
  41. offset: Offset.zero,
  42. spreadRadius: 1,
  43. ),
  44. ],
  45. borderRadius: BorderRadius.circular(16),
  46. ),
  47. child: ClipRRect(
  48. borderRadius: BorderRadius.circular(16),
  49. child: ChewieAudio(
  50. controller: chewieController!,
  51. ),
  52. ),
  53. );
  54. } else if (snapshot.hasError) {
  55. return const Icon(Icons.error_outline);
  56. }
  57. return const GlobalLoading(isSmallSize: true);
  58. });
  59. }
  60. @override
  61. void dispose() {
  62. videoPlayerController.dispose();
  63. chewieController?.dispose();
  64. super.dispose();
  65. }
  66. }