From 1415f91fa5f32b53f69332c7f1ef1129a3d9f454 Mon Sep 17 00:00:00 2001 From: Orangerot Date: Mon, 23 Dec 2024 03:18:39 +0100 Subject: feat: play audio on entering level and pause button --- lib/level.dart | 65 ++++++++++++++++++++++++++++++++++++++++++------ lib/level_selection.dart | 16 +++++++----- 2 files changed, 68 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/level.dart b/lib/level.dart index c92658b..e35325a 100644 --- a/lib/level.dart +++ b/lib/level.dart @@ -1,12 +1,49 @@ +import 'dart:io'; + import 'package:flutter/material.dart'; +import 'package:audioplayers/audioplayers.dart'; + +class Level extends StatefulWidget { + const Level({super.key, required this.stepmaniaFolderPath}); + final String stepmaniaFolderPath; + + @override + State createState() => _LevelState(); +} + +class _LevelState extends State { + final _player = AudioPlayer(); + bool _isPlaying = true; -class Level extends StatelessWidget { - const Level({super.key}); @override Widget build(BuildContext context) { + String audioPath = Directory(widget.stepmaniaFolderPath) + .listSync() + .firstWhere((entity) => entity.path.endsWith('.ogg'), + orElse: () => File('')) + .path; + _player.play(DeviceFileSource(audioPath)); return Scaffold( appBar: AppBar( - leading: Icon(Icons.pause), + leading: IconButton( + icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow), + onPressed: () => { + if (_isPlaying) + { + _player.pause(), + setState(() { + _isPlaying = false; + }) + } + else + { + _player.resume(), + setState(() { + _isPlaying = true; + }) + }, + }, + ), title: Text('Level 1'), actions: [ IconButton( @@ -15,10 +52,18 @@ class Level extends StatelessWidget { ], ), body: Stack(children: [ - Arrow( position: -100.0,), - Arrow( position: 00.0,), - Arrow( position: 100.0,), - Arrow( position: 200.0,), + Arrow( + position: -100.0, + ), + Arrow( + position: 00.0, + ), + Arrow( + position: 100.0, + ), + Arrow( + position: 200.0, + ), Positioned( top: 50, width: MediaQuery.of(context).size.width, @@ -43,6 +88,12 @@ class Level extends StatelessWidget { ), ])); } + + @override + void dispose() { + _player.dispose(); + super.dispose(); + } } class Arrow extends StatelessWidget { diff --git a/lib/level_selection.dart b/lib/level_selection.dart index cc79911..e2cdcbe 100644 --- a/lib/level_selection.dart +++ b/lib/level_selection.dart @@ -81,12 +81,13 @@ class _LevelSelectionState extends State { separatorBuilder: (BuildContext context, int index) => const Divider(), itemBuilder: (context, index) { - String thumbnailPath = - Directory(stepmaniaCoursesFolders[index].path) - .listSync() - .firstWhere((file) => file.path.toLowerCase().endsWith('banner.png'), + String thumbnailPath = Directory( + stepmaniaCoursesFolders[index].path) + .listSync() + .firstWhere( + (file) => file.path.toLowerCase().endsWith('banner.png'), orElse: () => File('')) - .path; + .path; return ListTile( leading: Image.file(File(thumbnailPath)), trailing: Icon(Icons.play_arrow), @@ -96,7 +97,10 @@ class _LevelSelectionState extends State { onTap: () => Navigator.push( context, MaterialPageRoute( - builder: (BuildContext context) => Level())), + builder: (BuildContext context) => Level( + stepmaniaFolderPath: + stepmaniaCoursesFolders[index].path, + ))), ); }, ); -- cgit v1.2.3