Newer
Older
MiniTias / lib / services / sound_service.dart
import 'package:shared_preferences/shared_preferences.dart';

import 'package:mini_tias/services/raw_capture_service.dart';

/// サウンドフィードバックの ON/OFF を管理し,再生を担うサービス.
///
/// 設定は SharedPreferences に永続化される(キー: `sound_enabled`).
class SoundService {
  static const _keySound = 'sound_enabled';

  final RawCaptureService _rawCaptureService;
  bool _soundEnabled = true;

  SoundService({RawCaptureService? rawCaptureService})
    : _rawCaptureService = rawCaptureService ?? RawCaptureService();

  /// サウンドが有効かどうか.
  bool get isSoundEnabled => _soundEnabled;

  /// SharedPreferences から設定を読み込む.
  Future<void> init() async {
    final prefs = await SharedPreferences.getInstance();
    _soundEnabled = prefs.getBool(_keySound) ?? true;
  }

  /// サウンドの ON/OFF を切り替え,設定を永続化する.
  Future<void> toggleSound() async {
    _soundEnabled = !_soundEnabled;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_keySound, _soundEnabled);
  }

  /// シャッター音を再生する(サウンドが OFF の場合は何もしない).
  Future<void> playShutterSound() async {
    if (!_soundEnabled) return;
    try {
      await _rawCaptureService.playShutterSound();
    } catch (e) {
      // サウンドエラーで撮影フローを止めない
    }
  }

  /// 保存完了音を再生する(サウンドが OFF の場合は何もしない).
  Future<void> playSaveCompleteSound() async {
    if (!_soundEnabled) return;
    try {
      await _rawCaptureService.playSaveCompleteSound();
    } catch (e) {
      // サウンドエラーで撮影フローを止めない
    }
  }
}