Skip to content

Commit

Permalink
Merge pull request #607 from lamarios/feautre/volume-and-brightness-c…
Browse files Browse the repository at this point in the history
…ontrol-in-video-player

add sliders to adjust device brightness and volume when the video is …
  • Loading branch information
lamarios authored Sep 28, 2024
2 parents b6da5cf + fcd01d8 commit 925273e
Show file tree
Hide file tree
Showing 12 changed files with 406 additions and 28 deletions.
102 changes: 85 additions & 17 deletions lib/player/states/player_controls.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import 'dart:ui';

import 'package:bloc/bloc.dart';
import 'package:easy_debounce/easy_debounce.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:clipious/player/models/media_event.dart';
import 'package:clipious/player/states/interfaces/media_player.dart';
import 'package:easy_debounce/easy_debounce.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_volume_controller/flutter_volume_controller.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:logging/logging.dart';
import 'package:screen_brightness/screen_brightness.dart';

import '../../globals.dart';
import '../../main.dart';
Expand Down Expand Up @@ -188,23 +193,86 @@ class PlayerControlsCubit extends Cubit<PlayerControlsState> {
emit(state.copyWith(justDoubleTappedSkip: false));
});
}

Future<void> startBrightnessAdjustments() async {
final currentBrightness = await ScreenBrightness().current;
emit(state.copyWith(
systemBrightness: currentBrightness, showBrightnessSlider: true));
}

Future<void> updateBrightness(
DragUpdateDetails details, BoxConstraints constraints) async {
// Set upper and lower bounds (75% of the screen for full brightness, bottom 25% for zero brightness)
double upperBound = 0.25; // top 75% is full brightness
double lowerBound = 0.75; // bottom is 0 brightness

// Clamping the brightness based on the drag position
double normalizedPosition =
(details.localPosition.dy / constraints.maxHeight)
.clamp(upperBound, lowerBound);

// Linearly interpolate between 1 (full brightness) and 0 (no brightness)
final screenBrightness = lerpDouble(
1, 0, (normalizedPosition - upperBound) / (lowerBound - upperBound))!;

await ScreenBrightness().setScreenBrightness(screenBrightness);
emit(state.copyWith(systemBrightness: screenBrightness));
}

void stopBrightnessAdjustments() {
emit(state.copyWith(showBrightnessSlider: false));
}

Future<void> startVolumeAdjustments() async {
await FlutterVolumeController.updateShowSystemUI(false);
final currentVolume = await FlutterVolumeController.getVolume();
emit(state.copyWith(
systemVolume: currentVolume ?? 0, showVolumeSlider: true));
}

Future<void> updateVolume(
DragUpdateDetails details, BoxConstraints constraints) async {
// Set upper and lower bounds (75% of the screen for full volume, bottom 25% for zero volume)
double upperBound = 0.25; // top 75% is full volume
double lowerBound = 0.75; // bottom is 0 volume

// Clamping the volume based on the drag position
double normalizedPosition =
(details.localPosition.dy / constraints.maxHeight)
.clamp(upperBound, lowerBound);

// Linearly interpolate between 1 (full volume) and 0 (no volume)
final volume = lerpDouble(
1, 0, (normalizedPosition - upperBound) / (lowerBound - upperBound))!;

await FlutterVolumeController.setVolume(volume);
emit(state.copyWith(systemVolume: volume));
}

void stopVolumeAdjustments() {
emit(state.copyWith(showVolumeSlider: false));
}
}

@freezed
class PlayerControlsState with _$PlayerControlsState {
const factory PlayerControlsState({
@Default(false) bool errored,
@Default(Duration.zero) Duration position,
@Default(Duration(seconds: 1)) Duration duration,
@Default(Duration.zero) Duration buffer,
@Default(FullScreenState.notFullScreen) FullScreenState fullScreenState,
@Default(false) bool displayControls,
@Default(false) bool muted,
@Default(false) bool buffering,
@Default(false) bool draggingPositionSlider,
@Default(0) double doubleTapFastForwardedOpacity,
@Default(0) double doubleTapRewindedOpacity,
@Default(false) bool justDoubleTappedSkip,
@Default(false) bool showSponsorBlocked,
}) = _PlayercontrolsState;
const factory PlayerControlsState(
{@Default(false) bool errored,
@Default(Duration.zero) Duration position,
@Default(Duration(seconds: 1)) Duration duration,
@Default(Duration.zero) Duration buffer,
@Default(FullScreenState.notFullScreen) FullScreenState fullScreenState,
@Default(false) bool displayControls,
@Default(false) bool muted,
@Default(false) bool buffering,
@Default(false) bool draggingPositionSlider,
@Default(0) double doubleTapFastForwardedOpacity,
@Default(0) double doubleTapRewindedOpacity,
@Default(false) bool justDoubleTappedSkip,
@Default(false) bool showSponsorBlocked,
// system setting adjustments
@Default(false) bool showBrightnessSlider,
@Default(0) double systemBrightness,
@Default(false) bool showVolumeSlider,
@Default(0) double systemVolume}) = _PlayercontrolsState;
}
106 changes: 99 additions & 7 deletions lib/player/states/player_controls.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ mixin _$PlayerControlsState {
double get doubleTapRewindedOpacity => throw _privateConstructorUsedError;
bool get justDoubleTappedSkip => throw _privateConstructorUsedError;
bool get showSponsorBlocked => throw _privateConstructorUsedError;
bool get showBrightnessSlider => throw _privateConstructorUsedError;
double get systemBrightness => throw _privateConstructorUsedError;
bool get showVolumeSlider => throw _privateConstructorUsedError;
double get systemVolume => throw _privateConstructorUsedError;

/// Create a copy of PlayerControlsState
/// with the given fields replaced by the non-null parameter values.
Expand Down Expand Up @@ -57,7 +61,11 @@ abstract class $PlayerControlsStateCopyWith<$Res> {
double doubleTapFastForwardedOpacity,
double doubleTapRewindedOpacity,
bool justDoubleTappedSkip,
bool showSponsorBlocked});
bool showSponsorBlocked,
bool showBrightnessSlider,
double systemBrightness,
bool showVolumeSlider,
double systemVolume});
}

/// @nodoc
Expand Down Expand Up @@ -88,6 +96,10 @@ class _$PlayerControlsStateCopyWithImpl<$Res, $Val extends PlayerControlsState>
Object? doubleTapRewindedOpacity = null,
Object? justDoubleTappedSkip = null,
Object? showSponsorBlocked = null,
Object? showBrightnessSlider = null,
Object? systemBrightness = null,
Object? showVolumeSlider = null,
Object? systemVolume = null,
}) {
return _then(_value.copyWith(
errored: null == errored
Expand Down Expand Up @@ -142,6 +154,22 @@ class _$PlayerControlsStateCopyWithImpl<$Res, $Val extends PlayerControlsState>
? _value.showSponsorBlocked
: showSponsorBlocked // ignore: cast_nullable_to_non_nullable
as bool,
showBrightnessSlider: null == showBrightnessSlider
? _value.showBrightnessSlider
: showBrightnessSlider // ignore: cast_nullable_to_non_nullable
as bool,
systemBrightness: null == systemBrightness
? _value.systemBrightness
: systemBrightness // ignore: cast_nullable_to_non_nullable
as double,
showVolumeSlider: null == showVolumeSlider
? _value.showVolumeSlider
: showVolumeSlider // ignore: cast_nullable_to_non_nullable
as bool,
systemVolume: null == systemVolume
? _value.systemVolume
: systemVolume // ignore: cast_nullable_to_non_nullable
as double,
) as $Val);
}
}
Expand All @@ -167,7 +195,11 @@ abstract class _$$PlayercontrolsStateImplCopyWith<$Res>
double doubleTapFastForwardedOpacity,
double doubleTapRewindedOpacity,
bool justDoubleTappedSkip,
bool showSponsorBlocked});
bool showSponsorBlocked,
bool showBrightnessSlider,
double systemBrightness,
bool showVolumeSlider,
double systemVolume});
}

/// @nodoc
Expand Down Expand Up @@ -196,6 +228,10 @@ class __$$PlayercontrolsStateImplCopyWithImpl<$Res>
Object? doubleTapRewindedOpacity = null,
Object? justDoubleTappedSkip = null,
Object? showSponsorBlocked = null,
Object? showBrightnessSlider = null,
Object? systemBrightness = null,
Object? showVolumeSlider = null,
Object? systemVolume = null,
}) {
return _then(_$PlayercontrolsStateImpl(
errored: null == errored
Expand Down Expand Up @@ -250,6 +286,22 @@ class __$$PlayercontrolsStateImplCopyWithImpl<$Res>
? _value.showSponsorBlocked
: showSponsorBlocked // ignore: cast_nullable_to_non_nullable
as bool,
showBrightnessSlider: null == showBrightnessSlider
? _value.showBrightnessSlider
: showBrightnessSlider // ignore: cast_nullable_to_non_nullable
as bool,
systemBrightness: null == systemBrightness
? _value.systemBrightness
: systemBrightness // ignore: cast_nullable_to_non_nullable
as double,
showVolumeSlider: null == showVolumeSlider
? _value.showVolumeSlider
: showVolumeSlider // ignore: cast_nullable_to_non_nullable
as bool,
systemVolume: null == systemVolume
? _value.systemVolume
: systemVolume // ignore: cast_nullable_to_non_nullable
as double,
));
}
}
Expand All @@ -270,7 +322,11 @@ class _$PlayercontrolsStateImpl implements _PlayercontrolsState {
this.doubleTapFastForwardedOpacity = 0,
this.doubleTapRewindedOpacity = 0,
this.justDoubleTappedSkip = false,
this.showSponsorBlocked = false});
this.showSponsorBlocked = false,
this.showBrightnessSlider = false,
this.systemBrightness = 0,
this.showVolumeSlider = false,
this.systemVolume = 0});

@override
@JsonKey()
Expand Down Expand Up @@ -311,10 +367,22 @@ class _$PlayercontrolsStateImpl implements _PlayercontrolsState {
@override
@JsonKey()
final bool showSponsorBlocked;
@override
@JsonKey()
final bool showBrightnessSlider;
@override
@JsonKey()
final double systemBrightness;
@override
@JsonKey()
final bool showVolumeSlider;
@override
@JsonKey()
final double systemVolume;

@override
String toString() {
return 'PlayerControlsState(errored: $errored, position: $position, duration: $duration, buffer: $buffer, fullScreenState: $fullScreenState, displayControls: $displayControls, muted: $muted, buffering: $buffering, draggingPositionSlider: $draggingPositionSlider, doubleTapFastForwardedOpacity: $doubleTapFastForwardedOpacity, doubleTapRewindedOpacity: $doubleTapRewindedOpacity, justDoubleTappedSkip: $justDoubleTappedSkip, showSponsorBlocked: $showSponsorBlocked)';
return 'PlayerControlsState(errored: $errored, position: $position, duration: $duration, buffer: $buffer, fullScreenState: $fullScreenState, displayControls: $displayControls, muted: $muted, buffering: $buffering, draggingPositionSlider: $draggingPositionSlider, doubleTapFastForwardedOpacity: $doubleTapFastForwardedOpacity, doubleTapRewindedOpacity: $doubleTapRewindedOpacity, justDoubleTappedSkip: $justDoubleTappedSkip, showSponsorBlocked: $showSponsorBlocked, showBrightnessSlider: $showBrightnessSlider, systemBrightness: $systemBrightness, showVolumeSlider: $showVolumeSlider, systemVolume: $systemVolume)';
}

@override
Expand Down Expand Up @@ -347,7 +415,15 @@ class _$PlayercontrolsStateImpl implements _PlayercontrolsState {
(identical(other.justDoubleTappedSkip, justDoubleTappedSkip) ||
other.justDoubleTappedSkip == justDoubleTappedSkip) &&
(identical(other.showSponsorBlocked, showSponsorBlocked) ||
other.showSponsorBlocked == showSponsorBlocked));
other.showSponsorBlocked == showSponsorBlocked) &&
(identical(other.showBrightnessSlider, showBrightnessSlider) ||
other.showBrightnessSlider == showBrightnessSlider) &&
(identical(other.systemBrightness, systemBrightness) ||
other.systemBrightness == systemBrightness) &&
(identical(other.showVolumeSlider, showVolumeSlider) ||
other.showVolumeSlider == showVolumeSlider) &&
(identical(other.systemVolume, systemVolume) ||
other.systemVolume == systemVolume));
}

@override
Expand All @@ -365,7 +441,11 @@ class _$PlayercontrolsStateImpl implements _PlayercontrolsState {
doubleTapFastForwardedOpacity,
doubleTapRewindedOpacity,
justDoubleTappedSkip,
showSponsorBlocked);
showSponsorBlocked,
showBrightnessSlider,
systemBrightness,
showVolumeSlider,
systemVolume);

/// Create a copy of PlayerControlsState
/// with the given fields replaced by the non-null parameter values.
Expand All @@ -391,7 +471,11 @@ abstract class _PlayercontrolsState implements PlayerControlsState {
final double doubleTapFastForwardedOpacity,
final double doubleTapRewindedOpacity,
final bool justDoubleTappedSkip,
final bool showSponsorBlocked}) = _$PlayercontrolsStateImpl;
final bool showSponsorBlocked,
final bool showBrightnessSlider,
final double systemBrightness,
final bool showVolumeSlider,
final double systemVolume}) = _$PlayercontrolsStateImpl;

@override
bool get errored;
Expand Down Expand Up @@ -419,6 +503,14 @@ abstract class _PlayercontrolsState implements PlayerControlsState {
bool get justDoubleTappedSkip;
@override
bool get showSponsorBlocked;
@override
bool get showBrightnessSlider;
@override
double get systemBrightness;
@override
bool get showVolumeSlider;
@override
double get systemVolume;

/// Create a copy of PlayerControlsState
/// with the given fields replaced by the non-null parameter values.
Expand Down
Loading

0 comments on commit 925273e

Please sign in to comment.