作为 flutter 中的第一个应用程序,我想构建一个节拍器应用程序。UI 已经构建完成,但实际节拍器功能我仍然遇到以下问题:
\n有时,节拍器会稍微滞后一点,只是足够,所以你会注意到它。flutter中有没有办法实现节拍器100%的精度?
\n演奏时不改变细分(您必须停止并启动节拍器)。如果值“节奏”和“细分”发生变化,如何自动应用于节拍器订阅?我知道 Flutter 提供了 Listenable、Stream、InheritedWidget 等工具,但我还没有找到在现有代码中实现这些工具的方法。
\n用户界面截图:
\n\n这是代码(它不完全是我写的 -> 学分):
\nimport \'dart:io\' show File;\nimport \'dart:async\';\nimport \'package:flutter/cupertino.dart\';\nimport \'package:quiver/async.dart\';\nimport \'package:audioplayers/audioplayers.dart\' show AudioPlayer;\nimport \'package:flutter/services.dart\' show ByteData, rootBundle;\nimport \'package:path_provider/path_provider.dart\' show getTemporaryDirectory;\n\n//credits: "Andi Qu", /sf/ask/3573388171/\n\nValueNotifier<int> tempo = ValueNotifier(100);\nint subdivision = 1;\nbool isPlaying = false;\n\nint soundIndex = 1;\nFile _soundFile;\n\nStreamSubscription<DateTime> _subscription;\n\nFuture<ByteData> _loadSound() async {\n return await rootBundle.load(\'assets/sounds/sound_$soundIndex.wav\');\n}\n\nvoid _writeSound() async {\n _soundFile = File(\n \'${(await getTemporaryDirectory()).path}/sounds/sound_$soundIndex.wav\');\n await _soundFile.writeAsBytes((await _loadSound()).buffer.asUint8List());\n print("_writeSound executed");\n}\n\nvoid _playLocal() async {\n final AudioPlayer _audioPlayer = AudioPlayer();\n AudioPlayer.logEnabled = false;\n await _audioPlayer.play(_soundFile.path, isLocal: true);\n}\n\n/// The actual method that plays the metronome\n\nvoid playpause() {\n print("playpause triggered");\n if (_soundFile == null) {\n print("_soundFile = null ---> Soundfile written");\n _writeSound();\n }\n\n if (isPlaying) {\n _subscription.cancel();\n isPlaying = false;\n print("metronome stopped");\n } else {\n _subscription = Metronome.periodic(new Duration(\n milliseconds: (60000 / (tempo.value * subdivision)).floor()))\n .listen((d) => _playLocal());\n isPlaying = true;\n print("metronome started");\n }\n}\n\nvoid increasetempo(int tempochange) {\n tempo.value = tempo.value + tempochange;\n\n if (isPlaying) {\n _subscription.cancel();\n print("_subscription canceled");\n _subscription = Metronome.periodic(new Duration(\n milliseconds: (60000 / (tempo.value * subdivision)).floor()))\n .listen((d) => _playLocal());\n }\n print("tempo changed to ${tempo.value}");\n}\n\nvoid decreasetempo(int tempochange) {\n tempo.value = tempo.value - tempochange;\n\n if (isPlaying) {\n _subscription.cancel();\n print("_subscription canceled");\n _subscription = Metronome.periodic(new Duration(\n milliseconds: (60000 / (tempo.value * subdivision)).floor()))\n .listen((d) => _playLocal());\n }\n print("tempo changed to ${tempo.value}");\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
977 次 |
| 最近记录: |