From 53c9f33c65ab8922bc52c1d1e78b5262ca8a0d1b Mon Sep 17 00:00:00 2001 From: Lucas Delanier Date: Sat, 29 Jul 2023 21:19:38 +0200 Subject: [PATCH] manage multiple song playing --- .../lib/components/music_list_component.dart | 17 ++++++-- .../lib/components/play_button_component.dart | 39 ++++++++++--------- Sources/justMUSIC/lib/main.dart | 2 + Sources/justMUSIC/lib/model/Music.dart | 13 +++++++ Sources/justMUSIC/pubspec.lock | 34 ++++++++-------- 5 files changed, 66 insertions(+), 39 deletions(-) diff --git a/Sources/justMUSIC/lib/components/music_list_component.dart b/Sources/justMUSIC/lib/components/music_list_component.dart index c92bba1..82cea64 100644 --- a/Sources/justMUSIC/lib/components/music_list_component.dart +++ b/Sources/justMUSIC/lib/components/music_list_component.dart @@ -6,9 +6,15 @@ import '../model/Music.dart'; class MusicListComponent extends StatelessWidget { final Music music; + final bool playing; + final int index; + final Function(int) selectMusic; const MusicListComponent({ Key? key, required this.music, + required this.playing, + required this.selectMusic, + required this.index, }) : super(key: key); @override @@ -90,9 +96,14 @@ class MusicListComponent extends StatelessWidget { ), ), Spacer(), - PlayButtonComponent( - urlPreview: music.previewUrl, - ) + music.previewUrl != null + ? PlayButtonComponent( + music: music, + callback: selectMusic, + playing: playing, + index: index, + ) + : Container() ], ), ); diff --git a/Sources/justMUSIC/lib/components/play_button_component.dart b/Sources/justMUSIC/lib/components/play_button_component.dart index 5c3c2e2..3b1f77a 100644 --- a/Sources/justMUSIC/lib/components/play_button_component.dart +++ b/Sources/justMUSIC/lib/components/play_button_component.dart @@ -3,9 +3,19 @@ import 'package:flutter/Material.dart'; import 'package:flutter_animated_play_button/flutter_animated_play_button.dart'; import 'package:ionicons/ionicons.dart'; +import '../model/Music.dart'; + class PlayButtonComponent extends StatefulWidget { - final String? urlPreview; - const PlayButtonComponent({Key? key, required this.urlPreview}) + final Music music; + final Function callback; + final int index; + bool playing; + PlayButtonComponent( + {Key? key, + required this.music, + required this.callback, + required this.playing, + required this.index}) : super(key: key); @override @@ -13,13 +23,12 @@ class PlayButtonComponent extends StatefulWidget { } class _PlayButtonComponentState extends State { - bool isPlaying = true; final player = AudioPlayer(); void switchStatePlaying() { setState(() { - isPlaying = !isPlaying; + widget.playing = !widget.playing; }); - stopSong(); + widget.music.stopSong(); } @override @@ -32,12 +41,14 @@ class _PlayButtonComponentState extends State { @override Widget build(BuildContext context) { - if (!isPlaying) { - playSong(); + if (widget.playing) { + widget.music.playSong(); } else {} - return isPlaying + return !widget.playing ? GestureDetector( - onTap: switchStatePlaying, + onTap: () { + widget.callback(widget.index); + }, child: Container( width: 30, height: 30, @@ -59,14 +70,4 @@ class _PlayButtonComponentState extends State { ), )); } - - Future playSong() async { - if (widget.urlPreview != null) { - await player.play(UrlSource(widget.urlPreview ?? "")); - } - } - - Future stopSong() async { - await player.stop(); - } } diff --git a/Sources/justMUSIC/lib/main.dart b/Sources/justMUSIC/lib/main.dart index 1203f32..e7d6cea 100644 --- a/Sources/justMUSIC/lib/main.dart +++ b/Sources/justMUSIC/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:audioplayers/audioplayers.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; @@ -18,6 +19,7 @@ void main() { class MyApp extends StatelessWidget { static UserViewModel userViewModel = UserViewModel(); static MusicViewModel musicViewModel = MusicViewModel(); + static AudioPlayer audioPlayer = AudioPlayer(); const MyApp({super.key}); diff --git a/Sources/justMUSIC/lib/model/Music.dart b/Sources/justMUSIC/lib/model/Music.dart index 367d6a6..711b10a 100644 --- a/Sources/justMUSIC/lib/model/Music.dart +++ b/Sources/justMUSIC/lib/model/Music.dart @@ -1,4 +1,7 @@ +import 'package:audioplayers/audioplayers.dart'; + import 'Artist.dart'; +import '../main.dart'; class Music { final String _id; @@ -58,4 +61,14 @@ class Music { set artists(List value) { _artists = value; } + + Future playSong() async { + if (previewUrl != null) { + await MyApp.audioPlayer.play(UrlSource(previewUrl!)); + } + } + + Future stopSong() async { + await MyApp.audioPlayer.stop(); + } } diff --git a/Sources/justMUSIC/pubspec.lock b/Sources/justMUSIC/pubspec.lock index 685a4a9..706cf83 100644 --- a/Sources/justMUSIC/pubspec.lock +++ b/Sources/justMUSIC/pubspec.lock @@ -5,10 +5,10 @@ packages: dependency: transitive description: name: async - sha256: bfe67ef28df125b7dddcea62755991f807aa39a2492a23e1550161692950bbe0 + sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c" url: "https://pub.dev" source: hosted - version: "2.10.0" + version: "2.11.0" audioplayers: dependency: "direct main" description: @@ -85,10 +85,10 @@ packages: dependency: transitive description: name: characters - sha256: e6a326c8af69605aec75ed6c187d06b349707a27fbff8222ca9cc2cff167975c + sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605" url: "https://pub.dev" source: hosted - version: "1.2.1" + version: "1.3.0" circular_reveal_animation: dependency: "direct main" description: @@ -109,10 +109,10 @@ packages: dependency: transitive description: name: collection - sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0 + sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c" url: "https://pub.dev" source: hosted - version: "1.17.0" + version: "1.17.1" crypto: dependency: transitive description: @@ -268,10 +268,10 @@ packages: dependency: transitive description: name: js - sha256: "5528c2f391ededb7775ec1daa69e65a2d61276f7552de2b5f7b8d34ee9fd4ab7" + sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3 url: "https://pub.dev" source: hosted - version: "0.6.5" + version: "0.6.7" lints: dependency: transitive description: @@ -284,10 +284,10 @@ packages: dependency: transitive description: name: matcher - sha256: "16db949ceee371e9b99d22f88fa3a73c4e59fd0afed0bd25fc336eb76c198b72" + sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb" url: "https://pub.dev" source: hosted - version: "0.12.13" + version: "0.12.15" material_color_utilities: dependency: transitive description: @@ -300,10 +300,10 @@ packages: dependency: transitive description: name: meta - sha256: "6c268b42ed578a53088d834796959e4a1814b5e9e164f147f580a386e5decf42" + sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" url: "https://pub.dev" source: hosted - version: "1.8.0" + version: "1.9.1" modal_bottom_sheet: dependency: "direct main" description: @@ -316,10 +316,10 @@ packages: dependency: transitive description: name: path - sha256: db9d4f58c908a4ba5953fcee2ae317c94889433e5024c27ce74a37f94267945b + sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" url: "https://pub.dev" source: hosted - version: "1.8.2" + version: "1.8.3" path_provider: dependency: transitive description: @@ -449,10 +449,10 @@ packages: dependency: transitive description: name: test_api - sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206 + sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb url: "https://pub.dev" source: hosted - version: "0.4.16" + version: "0.5.1" text_scroll: dependency: "direct main" description: @@ -518,5 +518,5 @@ packages: source: hosted version: "1.1.0" sdks: - dart: ">=2.18.2 <3.0.0" + dart: ">=3.0.0-0 <4.0.0" flutter: ">=3.3.0" -- 2.36.3