Optimize sound latency with preloading, add startup welcome chime, and improve Android vibration haptics
This commit is contained in:
parent
bf87873181
commit
8096cad270
|
|
@ -2,37 +2,88 @@ import 'package:flutter/services.dart';
|
||||||
import 'package:audioplayers/audioplayers.dart';
|
import 'package:audioplayers/audioplayers.dart';
|
||||||
import 'package:vibration/vibration.dart';
|
import 'package:vibration/vibration.dart';
|
||||||
import 'package:flutter/foundation.dart' show kIsWeb, debugPrint;
|
import 'package:flutter/foundation.dart' show kIsWeb, debugPrint;
|
||||||
|
import 'dart:io' show Platform;
|
||||||
|
|
||||||
class AppFeedback {
|
class AppFeedback {
|
||||||
static final AudioPlayer _player = AudioPlayer();
|
static final Map<String, AudioPlayer> _players = {
|
||||||
|
'click.mp3': AudioPlayer(),
|
||||||
|
'select.mp3': AudioPlayer(),
|
||||||
|
'success.mp3': AudioPlayer(),
|
||||||
|
'error.mp3': AudioPlayer(),
|
||||||
|
};
|
||||||
|
|
||||||
/// Play a sound from assets/sounds/
|
static bool _initialized = false;
|
||||||
|
|
||||||
|
/// Preload sounds to achieve zero latency
|
||||||
|
static void init() {
|
||||||
|
if (_initialized) return;
|
||||||
|
try {
|
||||||
|
_players.forEach((key, player) {
|
||||||
|
player.setReleaseMode(ReleaseMode.stop);
|
||||||
|
player.setSource(AssetSource('sounds/$key'));
|
||||||
|
});
|
||||||
|
_initialized = true;
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint('Error initializing AppFeedback: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Play a sound from assets/sounds/ with zero delay
|
||||||
static Future<void> playSound(String fileName) async {
|
static Future<void> playSound(String fileName) async {
|
||||||
try {
|
try {
|
||||||
// AudioPlayer play from assets/sounds/fileName
|
if (!_initialized) {
|
||||||
await _player.play(AssetSource('sounds/$fileName'));
|
init();
|
||||||
|
}
|
||||||
|
final player = _players[fileName];
|
||||||
|
if (player != null) {
|
||||||
|
// Stop/seek and resume for instant audio
|
||||||
|
await player.seek(Duration.zero);
|
||||||
|
await player.resume();
|
||||||
|
} else {
|
||||||
|
// Fallback for dynamically added files
|
||||||
|
final tempPlayer = AudioPlayer();
|
||||||
|
await tempPlayer.play(AssetSource('sounds/$fileName'));
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Web might throw security exception if player triggers before user interaction
|
|
||||||
// or other runtime issues. Catching prevents app crashes.
|
|
||||||
debugPrint('Audio playback error for $fileName: $e');
|
debugPrint('Audio playback error for $fileName: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helper to check Android platform safely
|
||||||
|
static bool get _isAndroid => !kIsWeb && Platform.isAndroid;
|
||||||
|
|
||||||
|
/// Trigger native vibration for Android with customized duration (in milliseconds)
|
||||||
|
static void _triggerVibration(int duration) async {
|
||||||
|
if (kIsWeb) return;
|
||||||
|
try {
|
||||||
|
if (_isAndroid) {
|
||||||
|
if (await Vibration.hasVibrator() == true) {
|
||||||
|
Vibration.vibrate(duration: duration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint('Vibration error: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Light haptic feedback + subtle click sound
|
/// Light haptic feedback + subtle click sound
|
||||||
static void click() {
|
static void click() {
|
||||||
HapticFeedback.lightImpact();
|
HapticFeedback.lightImpact();
|
||||||
|
_triggerVibration(30);
|
||||||
playSound('click.mp3');
|
playSound('click.mp3');
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Medium haptic + selection sound
|
/// Medium haptic + selection sound
|
||||||
static void select() {
|
static void select() {
|
||||||
HapticFeedback.mediumImpact();
|
HapticFeedback.mediumImpact();
|
||||||
|
_triggerVibration(60);
|
||||||
playSound('select.mp3');
|
playSound('select.mp3');
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Celebration sound + strong haptic vibration
|
/// Celebration sound + strong haptic vibration
|
||||||
static void success() {
|
static void success() {
|
||||||
HapticFeedback.heavyImpact();
|
HapticFeedback.heavyImpact();
|
||||||
|
_doubleVibrate();
|
||||||
playSound('success.mp3');
|
playSound('success.mp3');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -45,21 +96,25 @@ class AppFeedback {
|
||||||
/// Light haptic feedback (e.g. chip selections)
|
/// Light haptic feedback (e.g. chip selections)
|
||||||
static void lightHaptic() {
|
static void lightHaptic() {
|
||||||
HapticFeedback.lightImpact();
|
HapticFeedback.lightImpact();
|
||||||
|
_triggerVibration(30);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Medium haptic feedback
|
/// Medium haptic feedback
|
||||||
static void mediumHaptic() {
|
static void mediumHaptic() {
|
||||||
HapticFeedback.mediumImpact();
|
HapticFeedback.mediumImpact();
|
||||||
|
_triggerVibration(60);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Heavy haptic feedback
|
/// Heavy haptic feedback
|
||||||
static void heavyHaptic() {
|
static void heavyHaptic() {
|
||||||
HapticFeedback.heavyImpact();
|
HapticFeedback.heavyImpact();
|
||||||
|
_triggerVibration(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Subtle haptic feedback (e.g. screen navigation)
|
/// Subtle haptic feedback (e.g. screen navigation)
|
||||||
static void subtleHaptic() {
|
static void subtleHaptic() {
|
||||||
HapticFeedback.selectionClick();
|
HapticFeedback.selectionClick();
|
||||||
|
_triggerVibration(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Double vibrate pattern using vibration package
|
/// Double vibrate pattern using vibration package
|
||||||
|
|
@ -69,7 +124,6 @@ class AppFeedback {
|
||||||
// Double vibrate pattern: wait 0, vibrate 100ms, wait 100ms, vibrate 100ms
|
// Double vibrate pattern: wait 0, vibrate 100ms, wait 100ms, vibrate 100ms
|
||||||
Vibration.vibrate(pattern: [0, 100, 100, 100]);
|
Vibration.vibrate(pattern: [0, 100, 100, 100]);
|
||||||
} else {
|
} else {
|
||||||
// Fallback to native HapticFeedback
|
|
||||||
HapticFeedback.mediumImpact();
|
HapticFeedback.mediumImpact();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'theme.dart';
|
import 'theme.dart';
|
||||||
import 'screens/welcome_screen.dart';
|
import 'screens/welcome_screen.dart';
|
||||||
|
import 'feedback_helper.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
AppFeedback.init();
|
||||||
runApp(const PozoApp());
|
runApp(const PozoApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,11 @@ class _WelcomeScreenState extends State<WelcomeScreen> with SingleTickerProvider
|
||||||
);
|
);
|
||||||
|
|
||||||
_controller.forward();
|
_controller.forward();
|
||||||
|
Future.delayed(const Duration(milliseconds: 300), () {
|
||||||
|
if (mounted) {
|
||||||
|
AppFeedback.success();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _initMobileNumberDetection() async {
|
Future<void> _initMobileNumberDetection() async {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue