Initial commit
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class SystemVolumeEvent {
|
||||
final int current;
|
||||
final int max;
|
||||
const SystemVolumeEvent({required this.current, required this.max});
|
||||
|
||||
double get percent => max == 0 ? 0 : current / max;
|
||||
}
|
||||
|
||||
class SystemAudioController {
|
||||
static const _methodChannel = MethodChannel('smplayer/system_audio');
|
||||
static const _eventChannel = EventChannel('smplayer/system_audio_events');
|
||||
|
||||
SystemAudioController._();
|
||||
static final SystemAudioController instance = SystemAudioController._();
|
||||
|
||||
Stream<SystemVolumeEvent>? _volumeChanges;
|
||||
|
||||
Stream<SystemVolumeEvent> get volumeChanges {
|
||||
_volumeChanges ??= _eventChannel.receiveBroadcastStream().map((event) {
|
||||
final map = (event as Map).cast<Object?, Object?>();
|
||||
return SystemVolumeEvent(
|
||||
current: (map['current'] as num).toInt(),
|
||||
max: (map['max'] as num).toInt(),
|
||||
);
|
||||
});
|
||||
return _volumeChanges!;
|
||||
}
|
||||
|
||||
Future<SystemVolumeEvent> getVolume() async {
|
||||
final result = await _methodChannel.invokeMapMethod<String, dynamic>(
|
||||
'getVolume',
|
||||
);
|
||||
if (result == null) {
|
||||
return const SystemVolumeEvent(current: 0, max: 15);
|
||||
}
|
||||
return SystemVolumeEvent(
|
||||
current: (result['current'] as num).toInt(),
|
||||
max: (result['max'] as num).toInt(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<SystemVolumeEvent> adjustVolume(int delta) async {
|
||||
final result = await _methodChannel.invokeMapMethod<String, dynamic>(
|
||||
'adjustVolume',
|
||||
{'delta': delta},
|
||||
);
|
||||
if (result == null) {
|
||||
return const SystemVolumeEvent(current: 0, max: 15);
|
||||
}
|
||||
return SystemVolumeEvent(
|
||||
current: (result['current'] as num).toInt(),
|
||||
max: (result['max'] as num).toInt(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> setKeyIntercept(bool enabled) async {
|
||||
await _methodChannel.invokeMethod<void>('setKeyIntercept', {
|
||||
'enabled': enabled,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user