Skip to content

Commit af2eaa3

Browse files
committed
feat: integrate volume_controller for system volume synchronization
1 parent 08847a9 commit af2eaa3

8 files changed

Lines changed: 59 additions & 2 deletions

File tree

lib/providers/settings/video_player_settings_provider.dart

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import 'dart:io';
2+
13
import 'package:flutter/material.dart';
24
import 'package:flutter/services.dart';
3-
45
import 'package:collection/collection.dart';
6+
import 'package:flutter/foundation.dart';
57
import 'package:flutter_riverpod/flutter_riverpod.dart';
68
import 'package:screen_brightness/screen_brightness.dart';
9+
import 'package:volume_controller/volume_controller.dart';
710

811
import 'package:fladder/models/settings/key_combinations.dart';
912
import 'package:fladder/models/settings/video_player_settings.dart';
@@ -18,10 +21,38 @@ final videoPlayerSettingsProvider =
1821
final playbackRateProvider = StateProvider<double>((ref) => 1.0);
1922

2023
class VideoPlayerSettingsProviderNotifier extends StateNotifier<VideoPlayerSettingsModel> {
21-
VideoPlayerSettingsProviderNotifier(this.ref) : super(VideoPlayerSettingsModel());
24+
VideoPlayerSettingsProviderNotifier(this.ref) : super(VideoPlayerSettingsModel()) {
25+
_initVolumeSync();
26+
}
2227

2328
final Ref ref;
2429

30+
void _initVolumeSync() async {
31+
// Initialize volume from system volume on mobile/supported platforms
32+
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
33+
VolumeController.instance.showSystemUI = false;
34+
final initialVolume = await VolumeController.instance.getVolume();
35+
state = state.copyWith(internalVolume: initialVolume * 100);
36+
37+
VolumeController.instance.addListener((volume) {
38+
// Update both the model and the player when system volume changes (hardware buttons)
39+
final newVolume = volume * 100;
40+
if ((state.internalVolume - newVolume).abs() > 0.1) {
41+
state = state.copyWith(internalVolume: newVolume);
42+
ref.read(videoPlayerProvider).setVolume(newVolume);
43+
}
44+
});
45+
}
46+
}
47+
48+
@override
49+
void dispose() {
50+
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
51+
VolumeController.instance.removeListener();
52+
}
53+
super.dispose();
54+
}
55+
2556
@override
2657
set state(VideoPlayerSettingsModel value) {
2758
final oldState = super.state;
@@ -63,12 +94,18 @@ class VideoPlayerSettingsProviderNotifier extends StateNotifier<VideoPlayerSetti
6394
void setVolume(double value) {
6495
state = state.copyWith(internalVolume: value);
6596
ref.read(videoPlayerProvider).setVolume(value);
97+
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
98+
VolumeController.instance.setVolume(value / 100);
99+
}
66100
}
67101

68102
void steppedVolume(int i) {
69103
final value = (state.volume + i).clamp(0, 100).toDouble();
70104
state = state.copyWith(internalVolume: value);
71105
ref.read(videoPlayerProvider).setVolume(value);
106+
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
107+
VolumeController.instance.setVolume(value / 100);
108+
}
72109
}
73110

74111
void steppedSpeed(double i) {

linux/flutter/generated_plugin_registrant.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <screen_retriever_linux/screen_retriever_linux_plugin.h>
1616
#include <sqlite3_flutter_libs/sqlite3_flutter_libs_plugin.h>
1717
#include <url_launcher_linux/url_launcher_plugin.h>
18+
#include <volume_controller/volume_controller_plugin.h>
1819
#include <window_manager/window_manager_plugin.h>
1920

2021
void fl_register_plugins(FlPluginRegistry* registry) {
@@ -45,6 +46,9 @@ void fl_register_plugins(FlPluginRegistry* registry) {
4546
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
4647
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
4748
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
49+
g_autoptr(FlPluginRegistrar) volume_controller_registrar =
50+
fl_plugin_registry_get_registrar_for_plugin(registry, "VolumeControllerPlugin");
51+
volume_controller_plugin_register_with_registrar(volume_controller_registrar);
4852
g_autoptr(FlPluginRegistrar) window_manager_registrar =
4953
fl_plugin_registry_get_registrar_for_plugin(registry, "WindowManagerPlugin");
5054
window_manager_plugin_register_with_registrar(window_manager_registrar);

linux/flutter/generated_plugins.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
1212
screen_retriever_linux
1313
sqlite3_flutter_libs
1414
url_launcher_linux
15+
volume_controller
1516
window_manager
1617
)
1718

macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import sqflite_darwin
2929
import sqlite3_flutter_libs
3030
import url_launcher_macos
3131
import video_player_avfoundation
32+
import volume_controller
3233
import wakelock_plus
3334
import webview_flutter_wkwebview
3435
import window_manager
@@ -58,6 +59,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
5859
Sqlite3FlutterLibsPlugin.register(with: registry.registrar(forPlugin: "Sqlite3FlutterLibsPlugin"))
5960
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
6061
FVPVideoPlayerPlugin.register(with: registry.registrar(forPlugin: "FVPVideoPlayerPlugin"))
62+
VolumeControllerPlugin.register(with: registry.registrar(forPlugin: "VolumeControllerPlugin"))
6163
WakelockPlusMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockPlusMacosPlugin"))
6264
WebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "WebViewFlutterPlugin"))
6365
WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))

pubspec.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,14 @@ packages:
23862386
url: "https://pub.dev"
23872387
source: hosted
23882388
version: "15.0.2"
2389+
volume_controller:
2390+
dependency: "direct main"
2391+
description:
2392+
name: volume_controller
2393+
sha256: ce25d4e02dd6f0b6d5d02dc1daa8de11b2d42d0f5afab8b1eda6aa0ebab3937a
2394+
url: "https://pub.dev"
2395+
source: hosted
2396+
version: "3.4.4"
23892397
wakelock_plus:
23902398
dependency: "direct main"
23912399
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ dependencies:
113113
package_info_plus: ^9.0.0
114114
wakelock_plus: ^1.3.2
115115
screen_brightness: ^2.1.7
116+
volume_controller: ^3.4.4
116117
window_manager: ^0.5.1
117118
smtc_windows: ^1.1.0
118119
background_downloader: ^9.2.3

windows/flutter/generated_plugin_registrant.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <share_plus/share_plus_windows_plugin_c_api.h>
2121
#include <sqlite3_flutter_libs/sqlite3_flutter_libs_plugin.h>
2222
#include <url_launcher_windows/url_launcher_windows.h>
23+
#include <volume_controller/volume_controller_plugin_c_api.h>
2324
#include <window_manager/window_manager_plugin.h>
2425

2526
void RegisterPlugins(flutter::PluginRegistry* registry) {
@@ -51,6 +52,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
5152
registry->GetRegistrarForPlugin("Sqlite3FlutterLibsPlugin"));
5253
UrlLauncherWindowsRegisterWithRegistrar(
5354
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
55+
VolumeControllerPluginCApiRegisterWithRegistrar(
56+
registry->GetRegistrarForPlugin("VolumeControllerPluginCApi"));
5457
WindowManagerPluginRegisterWithRegistrar(
5558
registry->GetRegistrarForPlugin("WindowManagerPlugin"));
5659
}

windows/flutter/generated_plugins.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
1717
share_plus
1818
sqlite3_flutter_libs
1919
url_launcher_windows
20+
volume_controller
2021
window_manager
2122
)
2223

0 commit comments

Comments
 (0)