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 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 7 deletions(-) (limited to 'lib/level.dart') 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 { -- cgit v1.2.3