File size: 2,211 Bytes
2e813e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// lib/widgets/video_player/lifecycle_manager.dart

import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';

/// A mixin for managing video player lifecycle and visibility changes
/// Must be used on a State that implements WidgetsBindingObserver
mixin VideoPlayerLifecycleMixin<T extends StatefulWidget> on State<T>, WidgetsBindingObserver {
  /// Whether video was playing before going to background
  bool _wasPlayingBeforeBackground = false;
  
  /// Whether the player is currently playing
  bool get isPlaying;
  
  /// Set the playing state
  set isPlaying(bool value);

  /// Sets up visibility listeners
  @override
  void initState() {
    super.initState();
    
    // Register as an observer to detect app lifecycle changes
    WidgetsBinding.instance.addObserver(this);
    
    // Add web-specific visibility change listener
    if (kIsWeb) {
      setupWebVisibilityListeners();
    }
  }
  
  /// Set up web-specific visibility listeners
  void setupWebVisibilityListeners();
  
  /// Handles visibility state changes
  void handleVisibilityChange();
  
  /// Toggles playback state
  void togglePlayback();
  
  /// Pauses video playback when app goes to background
  void pauseVideo() {
    if (isPlaying) {
      _wasPlayingBeforeBackground = true;
      togglePlayback();
    }
  }
  
  /// Resumes video playback when app comes to foreground
  void resumeVideo() {
    if (!isPlaying && _wasPlayingBeforeBackground) {
      _wasPlayingBeforeBackground = false;
      togglePlayback();
    }
  }
  
  /// Handles app lifecycle state changes
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // Handle app lifecycle changes for native platforms
    if (!kIsWeb) {
      if (state == AppLifecycleState.paused || 
          state == AppLifecycleState.inactive || 
          state == AppLifecycleState.detached) {
        pauseVideo();
      } else if (state == AppLifecycleState.resumed && _wasPlayingBeforeBackground) {
        resumeVideo();
      }
    }
  }
  
  /// Clean up resources
  @override
  void dispose() {
    // Unregister the observer
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}