diff options
author | Orangerot <purple@orangerot.dev> | 2024-12-23 03:18:39 +0100 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-12-23 03:18:39 +0100 |
commit | 1415f91fa5f32b53f69332c7f1ef1129a3d9f454 (patch) | |
tree | b8988e9a40fe01c38806aea422945b33f1a0424f /lib | |
parent | d397b62b5b6f6db45a2cc0ae6ca4e03ff04c54b2 (diff) |
feat: play audio on entering level and pause button
Diffstat (limited to 'lib')
-rw-r--r-- | lib/level.dart | 65 | ||||
-rw-r--r-- | lib/level_selection.dart | 16 |
2 files changed, 68 insertions, 13 deletions
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<Level> createState() => _LevelState(); +} + +class _LevelState extends State<Level> { + 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<LevelSelection> { 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<LevelSelection> { onTap: () => Navigator.push( context, MaterialPageRoute( - builder: (BuildContext context) => Level())), + builder: (BuildContext context) => Level( + stepmaniaFolderPath: + stepmaniaCoursesFolders[index].path, + ))), ); }, ); |