当前位置:网站首页>flutter video_ Player monitors and automatically plays the next song

flutter video_ Player monitors and automatically plays the next song

2022-06-22 14:50:00 Dense information

Reference resources
Reference resources

My approach is to monitor the current position and total duration of the song , Pay attention to the listening position , Listen after the song starts playing after initialization , Finally, remember to destroy

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

    if (_videoPlayerController != null) {
    
      // If the video controller is present , Clean up and recreate 
      _videoPlayerController.removeListener(_videoListener);
      _videoPlayerController.dispose();
    }

    videoplay();

  }


  videoplay() {
    
    _videoPlayerController = VideoPlayerController.network(
        widget.videoList[curIndex])
      // _videoPlayerController = VideoPlayerController.file(File("/storage/emulated/0/DCIM/pipixia/eecd00a7e13145ff84568fe4e10ea982.mp4"))
      ..initialize().then((_) {
    
        //  Make sure to display the first frame after initializing the video , Until the play button is pressed .
        setState(() {
    
          _videoPlayerController.play();
        });
      });
    // Start listening when you can play 
    _videoPlayerController.addListener(_videoListener);
  }
  
  // How to monitor 
  void _videoListener() {
    
    setState(() {
    
      var curPosition = _videoPlayerController.value.position;
      var totalPosition = _videoPlayerController.value.duration;
      curPos = curPosition.toString().substring(2, 7);
      // curPos=curPos.subString(0,2);
      totalPos = totalPosition.toString().substring(2, 7);
      print(" The current position ${curPos}, All ${totalPos}");
      // If the current position is the last position, jump to the next song 
      if (curPosition.toString() != "0:00:00.000000" &&
          curPosition == totalPosition) {
    
        setState(() {
    
          curIndex++;
          if (curIndex >= widget.videoList.length) {
    
            // Loop back to first 
            curIndex = 0;
          }
          curId = widget.idList[curIndex];
          getCommentLists();
          videoplay();
        });
      }
    });
  }

  @override
  void dispose() {
    
    _videoPlayerController.dispose();
    _videoPlayerController.removeListener(_videoListener);
    _commentController.dispose();
    _focusNodeComment.dispose();
    super.dispose();
  }
原网站

版权声明
本文为[Dense information]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221326106921.html