Skip to content

Commit

Permalink
Added a universal sync
Browse files Browse the repository at this point in the history
  • Loading branch information
hmziqrs committed Nov 12, 2024
1 parent 89318a2 commit 5c4a3da
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
25 changes: 20 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@
"type": "dart"
},
{
"name": "Linux (desktop)",
"deviceId": "linux",
"name": "macOS (desktop)",
"deviceId": "macos",
"type": "dart",
"request": "launch"
"request": "launch",
"args": [
"-t",
"lib/main.dart"
]
},
{
"name": "Mac Designed for iPad (desktop)",
"deviceId": "mac-designed-for-ipad",
"type": "dart",
"request": "launch",
"args": [
"-t",
"lib/main.dart"
]
},
{
"name": "Chrome (web)",
Expand All @@ -19,15 +33,16 @@
"request": "launch",
"args": [
"-t",
"lib/main.dart"
"lib/main.web.dart"
]
}
],
"compounds": [
{
"name": "current",
"configurations": [
"Linux (desktop)",
"macOS (desktop)",
"Mac Designed for iPad (desktop)",
"Chrome (web)"
]
}
Expand Down
57 changes: 38 additions & 19 deletions lib/screens/Home/Home.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
Expand All @@ -13,32 +15,50 @@ class HomeScreen extends StatefulWidget {

class _HomeScreenState extends State<HomeScreen> {
late VideoPlayerController controller;
// late AdmobInterstitial interstitialAd;
bool canClose = false;

// Video duration in microseconds
late int videoDurationMicros;

Future<void> syncVideo() async {
// Get current UTC timestamp in microseconds
int currentTimeMicros = DateTime.now().toUtc().microsecondsSinceEpoch;
print("Suncing video $currentTimeMicros ");

// Calculate the position where video should be playing
int position = currentTimeMicros % videoDurationMicros;

// Convert to Duration
Duration seekPosition = Duration(microseconds: position);
print("seek: $seekPosition ${seekPosition.inSeconds}");
print("Position: $position current: ${controller.value.position}");

// Seek to calculated position and play
await controller.seekTo(seekPosition);
await controller.play();
}

@override
void initState() {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
this.controller = VideoPlayerController.asset("assets/video.mp4");
this.controller.setLooping(true);

this.controller.initialize().then((value) async {
controller = VideoPlayerController.asset("assets/video.mp4");
controller.setLooping(true);

controller.initialize().then((value) async {
videoDurationMicros = controller.value.duration.inMicroseconds;
if (!kIsWeb) {
await this.controller.play();
syncVideo();
}
});
if (App.showAds) {
// this.interstitialAd = AdmobInterstitial(
// adUnitId: Ads.fullScreen(),
// );
}

super.initState();
}

@override
void dispose() {
this.controller.pause();
this.controller.dispose();
controller.pause();
controller.dispose();
super.dispose();
}

Expand All @@ -64,17 +84,16 @@ class _HomeScreenState extends State<HomeScreen> {

await sheet.closed;

this.controller.play();
syncVideo();
}

@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
this.controller.pause();
this.loadAdd();

return this.canClose;
controller.pause();
loadAdd();
return canClose;
},
child: Scaffold(
resizeToAvoidBottomInset: false,
Expand All @@ -84,13 +103,13 @@ class _HomeScreenState extends State<HomeScreen> {
fit: StackFit.expand,
children: [
Positioned.fill(
child: VideoPlayer(this.controller),
child: VideoPlayer(controller),
),
Builder(
builder: (context) {
return Positioned.fill(
child: GestureDetector(
onTap: () => this.onTap(context),
onTap: () => onTap(context),
child: Container(
color: Colors.transparent,
),
Expand Down
6 changes: 0 additions & 6 deletions macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ PODS:
- GoogleUtilities/UserDefaults (8.0.2):
- GoogleUtilities/Logger
- GoogleUtilities/Privacy
- Flutter
- FlutterMacOS
- media_kit_libs_macos_video (1.0.4):
- FlutterMacOS
- media_kit_native_event_loop (1.0.0):
Expand Down Expand Up @@ -181,7 +179,6 @@ DEPENDENCIES:
- firebase_crashlytics (from `Flutter/ephemeral/.symlinks/plugins/firebase_crashlytics/macos`)
- firebase_messaging (from `Flutter/ephemeral/.symlinks/plugins/firebase_messaging/macos`)
- FlutterMacOS (from `Flutter/ephemeral`)
- in_app_purchase_storekit (from `Flutter/ephemeral/.symlinks/plugins/in_app_purchase_storekit/darwin`)
- media_kit_libs_macos_video (from `Flutter/ephemeral/.symlinks/plugins/media_kit_libs_macos_video/macos`)
- media_kit_native_event_loop (from `Flutter/ephemeral/.symlinks/plugins/media_kit_native_event_loop/macos`)
- media_kit_video (from `Flutter/ephemeral/.symlinks/plugins/media_kit_video/macos`)
Expand Down Expand Up @@ -224,8 +221,6 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/firebase_messaging/macos
FlutterMacOS:
:path: Flutter/ephemeral
in_app_purchase_storekit:
:path: Flutter/ephemeral/.symlinks/plugins/in_app_purchase_storekit/darwin
media_kit_libs_macos_video:
:path: Flutter/ephemeral/.symlinks/plugins/media_kit_libs_macos_video/macos
media_kit_native_event_loop:
Expand Down Expand Up @@ -268,7 +263,6 @@ SPEC CHECKSUMS:
GoogleAppMeasurement: 6e49ffac7d3f2c3ded9cc663f912a13b67bbd0de
GoogleDataTransport: aae35b7ea0c09004c3797d53c8c41f66f219d6a7
GoogleUtilities: 26a3abef001b6533cf678d3eb38fd3f614b7872d
in_app_purchase_storekit: 8c3b0b3eb1b0f04efbff401c3de6266d4258d433
media_kit_libs_macos_video: b3e2bbec2eef97c285f2b1baa7963c67c753fb82
media_kit_native_event_loop: 81fd5b45192b72f8b5b69eaf5b540f45777eb8d5
media_kit_video: c75b07f14d59706c775778e4dd47dd027de8d1e5
Expand Down

0 comments on commit 5c4a3da

Please sign in to comment.