当前位置:网站首页>Essentials of fleet video playback and fleet videoplayer video playback components

Essentials of fleet video playback and fleet videoplayer video playback components

2022-06-23 02:21:00 Susceptible to cold

1 Add dependency

  #  Video playback 
  video_player: ^1.0.1

2 Preparation before playing video

2.1 Network access

stay ios In the catalog info.plist In the manifest file iOS Set up http Network access :

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

As shown in the figure below

stay android In the catalog AndroidManifest.xml Configure network request permissions and... In the manifest file http Access rights of

    <!--     Network request permission -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- External file storage permissions -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

networkSecurityConfig The configuration is http Access right

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

3 Video playback

The loading and playback of video resources are controlled through VideoPlayerController To operate the

3.1 Loading of video resources

VideoPlayerController  _playerController ;
  @override
  void initState() {
    super.initState();

    // Network link 
    //VideoPlayerController.network(url);
    //VideoPlayerController.file(File(url));
    // Local link 
    _videoPlayerController =
        VideoPlayerController.asset("asset Resource path ");

   // Call initialization method 
   _videoPlayerController.initialize()
     // Callback after asynchronous execution 
     ..whenComplete(() {
       // Refresh the page 
       setState(() {});
     });

  }

3.2 Video playback components

AspectRatio(
   // Set the size of the video   Aspect ratio . The aspect ratio is expressed as the aspect ratio . for example ,16:9 The value of the aspect ratio is 16.0/9.0
   aspectRatio: _videoPlayerController.value.aspectRatio,
   // Play video components 
   child: VideoPlayer(_videoPlayerController),
 ),
)

3.3 Video playback related control

    // Get the information about the current video playing 
    VideoPlayerValue videoPlayerValue = _videoPlayerController.value;

    // Initialization complete 
    bool initialized = videoPlayerValue.initialized;
    // Is it playing 
    bool isPlaying = videoPlayerValue.isPlaying;
    // The width to height ratio of the currently playing video 
    double aspectRatio = videoPlayerValue.aspectRatio;
    // Whether the current video is cached 
    bool isBuffer = videoPlayerValue.isBuffering;
    // Is the current video looping 
    bool isLoop = videoPlayerValue.isLooping;
    // The total length of video currently playing 
    Duration totalDuration = videoPlayerValue.duration;
    // Where the video is currently playing 
    Duration currentDuration = videoPlayerValue.position;
    if (initialized) {
      //  Video initialized 
      if (isPlaying) {
        //  Playing  ---  Pause 
        _videoPlayerController.pause();
      } else {
        // Pause  ---- Play 
        _videoPlayerController.play();
      }
      setState(() {});
    } else {
      // uninitialized 
      _videoPlayerController.initialize().then((_) {
        // videoPlayerController.play();
        // setState(() {});
      });
    }
原网站

版权声明
本文为[Susceptible to cold]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202081834200047.html