added settings to toggle the audio
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../models/kanji_item.dart';
|
||||
import '../services/deck_repository.dart';
|
||||
import '../services/distractor_generator.dart';
|
||||
@@ -29,13 +31,24 @@ class _VocabScreenState extends State<VocabScreen> {
|
||||
List<String> _correctAnswers = [];
|
||||
int _score = 0;
|
||||
int _asked = 0;
|
||||
bool _playAudio = true;
|
||||
bool _playCorrectSound = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadSettings();
|
||||
_loadDeck();
|
||||
}
|
||||
|
||||
Future<void> _loadSettings() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
setState(() {
|
||||
_playAudio = prefs.getBool('playAudio') ?? true;
|
||||
_playCorrectSound = prefs.getBool('playCorrectSound') ?? true;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _loadDeck() async {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
@@ -184,17 +197,28 @@ class _VocabScreenState extends State<VocabScreen> {
|
||||
}
|
||||
|
||||
if (isCorrect) {
|
||||
await _audioPlayer.play(AssetSource('sfx/confirm.mp3'));
|
||||
final maleAudios =
|
||||
current.pronunciationAudios.where((a) => a.gender == 'male');
|
||||
if (maleAudios.isNotEmpty) {
|
||||
try {
|
||||
await _audioPlayer.play(UrlSource(maleAudios.first.url));
|
||||
} catch (e) {
|
||||
// Ignore player errors
|
||||
if (_playCorrectSound) {
|
||||
await _audioPlayer.play(AssetSource('sfx/confirm.mp3'));
|
||||
}
|
||||
if (_playAudio) {
|
||||
final maleAudios =
|
||||
current.pronunciationAudios.where((a) => a.gender == 'male');
|
||||
if (maleAudios.isNotEmpty) {
|
||||
final completer = Completer<void>();
|
||||
final sub = _audioPlayer.onPlayerComplete.listen((event) {
|
||||
if (!completer.isCompleted) completer.complete();
|
||||
});
|
||||
|
||||
try {
|
||||
await _audioPlayer.play(UrlSource(maleAudios.first.url));
|
||||
await completer.future.timeout(const Duration(seconds: 5));
|
||||
} catch (e) {
|
||||
// Ignore player errors
|
||||
} finally {
|
||||
await sub.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
await Future.delayed(const Duration(milliseconds: 400));
|
||||
} else {
|
||||
await Future.delayed(const Duration(milliseconds: 900));
|
||||
}
|
||||
@@ -225,10 +249,11 @@ class _VocabScreenState extends State<VocabScreen> {
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.settings),
|
||||
onPressed: () {
|
||||
Navigator.of(context).push(
|
||||
onPressed: () async {
|
||||
await Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => const SettingsScreen()),
|
||||
);
|
||||
_loadSettings();
|
||||
},
|
||||
)
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user