Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void setLooping(boolean value) {
void setVolume(long value) {
if (mediaPlayer == null) return;

long bracketedValue = Math.max(0, Math.min(100, value));
long bracketedValue = Math.max(0, Math.min(200, value));
mediaPlayer.setVolume((int) bracketedValue);
}

Expand Down
68 changes: 65 additions & 3 deletions flutter_vlc_player/lib/src/vlc_player_controller.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:developer';
import 'dart:io';

import 'package:flutter/services.dart';
Expand All @@ -20,7 +21,7 @@ import 'package:flutter_vlc_player_platform_interface/flutter_vlc_player_platfor
///
/// After [dispose] all further calls are ignored.
class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
static const _maxVolume = 100;
int _maxVolume = 100;

/// The URI to the video file. This will be in different formats depending on
/// the [DataSourceType] of the original video.
Expand All @@ -39,6 +40,9 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
/// Initialize vlc player when the platform is ready automatically
final bool autoInitialize;

/// Video start from [startAt]. When initialization is complete
final Duration? startAt;

/// Set keep playing video in background, when app goes in background.
/// The default value is false.
final bool allowBackgroundPlayback;
Expand Down Expand Up @@ -71,7 +75,6 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {

/// List of onRenderer listeners
final List<RendererCallback> _onRendererEventListeners = [];

bool _isDisposed = false;

VlcAppLifeCycleObserver? _lifeCycleObserver;
Expand All @@ -96,6 +99,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
this.dataSource, {
this.autoInitialize = true,
this.allowBackgroundPlayback = false,
this.startAt,
this.package,
this.hwAcc = HwAcc.auto,
this.autoPlay = true,
Expand All @@ -118,6 +122,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
this.dataSource, {
this.autoInitialize = true,
this.allowBackgroundPlayback = false,
this.startAt,
this.hwAcc = HwAcc.auto,
this.autoPlay = true,
this.options,
Expand All @@ -139,6 +144,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
File file, {
this.autoInitialize = true,
this.allowBackgroundPlayback = true,
this.startAt,
this.hwAcc = HwAcc.auto,
this.autoPlay = true,
this.options,
Expand Down Expand Up @@ -346,9 +352,24 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {

_notifyOnInitListeners();

/// Calling start at
unawaited(_setStartAt());

return initializingCompleter.future;
}

/// Waiting for video being loaded and then seek to start at
Future<void> _setStartAt() async {
if (startAt != null) {
do {
await Future.delayed(const Duration(milliseconds: 100));
} while (value.playingState != PlayingState.playing);

log("seeking: ${value.duration.inSeconds}");
await seekTo(startAt ?? Duration.zero);
}
}
Comment thread
This conversation was marked as resolved.

/// Dispose controller
@override
Future<void> dispose() async {
Expand Down Expand Up @@ -394,6 +415,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
String? package,
bool? autoPlay,
HwAcc? hwAcc,
Duration? startAt,
}) async {
_dataSourceType = DataSourceType.asset;
this.package = package;
Expand All @@ -403,6 +425,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
package: package,
autoPlay: autoPlay,
hwAcc: hwAcc,
startAt: startAt,
);
}

Expand All @@ -414,6 +437,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
String dataSource, {
bool? autoPlay,
HwAcc? hwAcc,
Duration? startAt,
}) async {
_dataSourceType = DataSourceType.network;
package = null;
Expand All @@ -422,6 +446,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
dataSourceType: DataSourceType.network,
autoPlay: autoPlay,
hwAcc: hwAcc,
startAt: startAt,
);
}

Expand All @@ -433,6 +458,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
File file, {
bool? autoPlay,
HwAcc? hwAcc,
Duration? startAt,
}) async {
_dataSourceType = DataSourceType.file;
package = null;
Expand All @@ -442,6 +468,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
dataSourceType: DataSourceType.file,
autoPlay: autoPlay,
hwAcc: hwAcc,
startAt: startAt,
);
}

Expand All @@ -456,6 +483,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
String? package,
bool? autoPlay,
HwAcc? hwAcc,
Duration? startAt,
}) async {
_throwIfNotInitialized('setStreamUrl');
await vlcPlayerPlatform.stop(_viewId);
Expand All @@ -468,6 +496,10 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
autoPlay: autoPlay ?? true,
);

if (startAt != null) {
await vlcPlayerPlatform.seekTo(_viewId, startAt);
}

return;
}

Expand Down Expand Up @@ -557,9 +589,39 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
return position;
}

/// Sets the max audio volume of
///
/// [maxVolume] indicates a value between 1 (lowest) and 200 (highest) on a
/// linear scale.
Future<void> setMaxVolume(int maxVolume) async {
_throwIfNotInitialized('setMaxVolume');
const volumeLimit = 200;
if (maxVolume > volumeLimit) {
throw ArgumentError.value(
maxVolume,
'Max volume should be lower than 200.',
Comment thread
itsSagarBro marked this conversation as resolved.
Outdated
);
} else if (maxVolume <= 0) {
throw ArgumentError.value(
maxVolume,
'Max volume should not be 0 or negative.',
);
}
_maxVolume = maxVolume;

return;
}

/// Returns current vlc max volume level.
Future<int> getMaxVolume() async {
_throwIfNotInitialized('getMaxVolume');

return _maxVolume;
}

/// Sets the audio volume of
///
/// [volume] indicates a value between 0 (silent) and 100 (full volume) on a
/// [volume] indicates a value between 0 (silent), 100 (full volume) and 200 (maximum) on a
/// linear scale.
Future<void> setVolume(int volume) async {
_throwIfNotInitialized('setVolume');
Expand Down