如何在 videoplayercontroller.asset() 中打印视频路径

How to print a video path in videoplayercontroller.asset() in flutter

所以我试图制作一个功能,当我们点击一​​个图标按钮时它会改变视频路径。它将使用字符串更改视频路径。我正在使用 print("$videoname") 来确保字符串发生变化,并且它确实发生了变化。但它仍然不起作用,使用 videoplayercontroller.assets(): 这是我试图制作的代码

    String videoname="Video/Intro.mp4";
              Container(
              child:Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                ChewieListItem(
    
                  videoPlayerController: VideoPlayerController.asset(videoname),
                  looping: false,
    
                       ),
                Container(
                  margin: EdgeInsets.symmetric(vertical: 20.0),
                  height: 100.0,
                  child: ListView(
                    scrollDirection: Axis.horizontal,
                    children: [
                      IconButton(
    
                        icon:Icon(Icons.people),
                      onPressed: (){
                              videoname="Video/Intro.mp4";
                              print("$videoname");
                      },
    
    
                      ),
    
                      IconButton(
    
                        icon:Icon(Icons.personal_video),
                        onPressed: (){
                          videoname="Video/Intro1.mp4";
                          print("$videoname");
                        },
    
    
                      ),
    
    
                    ],
                  ),
                ),
    )

setState 应该适合你

onPressed: () {
              setState(() {
                videoname="Video/Intro1.mp4";
                print("$videoname");
              });
            },

您可以复制粘贴 运行 下面的完整代码
第 1 步:在 ChewieListItem
中使用 key: UniqueKey() 第 2 步:在 onPressed
中使用 setState 代码片段

ChewieListItem(
          key: UniqueKey(),
          videoPlayerController: VideoPlayerController.asset(videoname),
          looping: false,
        ),
        
 IconButton(
    icon: Icon(Icons.people),
    onPressed: () {
      setState(() {
        videoname = "Video/Intro.mp4";
        print("$videoname");
      });
    },
  ),
 IconButton(
    icon: Icon(Icons.personal_video),
    onPressed: () {
      setState(() {
        videoname = "Video/Intro1.mp4";
        print("$videoname");
      });
    },
  ),        

工作演示

完整代码

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';

class ChewieListItem extends StatefulWidget {
  // This will contain the URL/asset path which we want to play
  final VideoPlayerController videoPlayerController;
  final bool looping;

  ChewieListItem({
    @required this.videoPlayerController,
    this.looping,
    Key key,
  }) : super(key: key);

  @override
  _ChewieListItemState createState() => _ChewieListItemState();
}

class _ChewieListItemState extends State<ChewieListItem> {
  ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    print("ChewieListItem initState");
    // Wrapper on top of the videoPlayerController
    _chewieController = ChewieController(
      videoPlayerController: widget.videoPlayerController,
      aspectRatio: 16 / 9,
      // Prepare the video to be played and display the first frame
      autoInitialize: true,
      looping: widget.looping,
      // Errors can occur for example when trying to play a video
      // from a non-existent URL
      errorBuilder: (context, errorMessage) {
        return Center(
          child: Text(
            errorMessage,
            style: TextStyle(color: Colors.white),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Chewie(
        controller: _chewieController,
      ),
    );
  }

  @override
  void dispose() {
    print("ChewieListItem dispose");
    super.dispose();
    // IMPORTANT to dispose of all the used resources
    widget.videoPlayerController.dispose();
    _chewieController.dispose();
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String videoname = "Video/Intro.mp4";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
          child:
              Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        ChewieListItem(
          key: UniqueKey(),
          videoPlayerController: VideoPlayerController.asset(videoname),
          looping: false,
        ),
        Container(
          margin: EdgeInsets.symmetric(vertical: 20.0),
          height: 100.0,
          child: ListView(
            scrollDirection: Axis.horizontal,
            children: [
              IconButton(
                icon: Icon(Icons.people),
                onPressed: () {
                  setState(() {
                    videoname = "Video/Intro.mp4";
                    print("$videoname");
                  });
                },
              ),
              IconButton(
                icon: Icon(Icons.personal_video),
                onPressed: () {
                  setState(() {
                    videoname = "Video/Intro1.mp4";
                    print("$videoname");
                  });
                },
              ),
            ],
          ),
        ),
      ])),
    );
  }
}