尝试从 pushNamed in 颤振中的参数访问视频 URL 时出错



我正在尝试通过颤振中的pushNamed将URL传递给video controller

我面临的error如下


I/ExoPlayerImpl(29662): Release 2d82dca [ExoPlayerLib/2.9.6] [ASUS_X00T_2, ASUS_X00TD, asus, 28] [goog.exo.core]
I/flutter (29662): https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4
E/BufferQueueProducer(29662): [SurfaceTexture-0-29662-5] cancelBuffer: BufferQueue has been abandoned
I/chatty  (29662): uid=10085(com.example.demo_app) JNISurfaceTextu identical 5 lines
E/BufferQueueProducer(29662): [SurfaceTexture-0-29662-5] cancelBuffer: BufferQueue has been abandoned
D/SurfaceUtils(29662): disconnecting from surface 0x7b16f15010, reason disconnectFromSurface
I/ExoPlayerImpl(29662): Init 3c27ac9 [ExoPlayerLib/2.9.6] [ASUS_X00T_2, ASUS_X00TD, asus, 28]
I/OMXClient(29662): IOmx service obtained
D/SurfaceUtils(29662): connecting to surface 0x7b17399010, reason connectToSurface
I/MediaCodec(29662): [OMX.qcom.video.decoder.avc] setting surface generation to 30373895
D/SurfaceUtils(29662): disconnecting from surface 0x7b17399010, reason connectToSurface(reconnect)
D/SurfaceUtils(29662): connecting to surface 0x7b17399010, reason connectToSurface(reconnect)
I/ExtendedACodec(29662): setupVideoDecoder()
I/ExtendedACodec(29662): Decoder will be in frame by frame mode
D/SurfaceUtils(29662): set up nativeWindow 0x7b17399010 for 1280x720, color 0x7fa30c06, rotation 0, usage 0x20002900
W/MapperHal(29662): buffer descriptor with invalid usage bits 0x2000
W/MapperHal(29662): buffer descriptor with invalid usage bits 0x2000
I/OMXClient(29662): IOmx service obtained
I/ACodec  (29662): codec does not support config priority (err -2147483648)
I/ACodec  (29662): codec does not support config priority (err -2147483648)
I/ACodec  (29662): codec does not support config operating rate (err -2147483648)
W/ExtendedACodec(29662): Failed to get extension for extradata parameter
W/MapperHal(29662): buffer descriptor with invalid usage bits 0x2000
D/SurfaceUtils(29662): set up nativeWindow 0x7b17399010 for 1280x720, color 0x7fa30c06, rotation 0, usage 0x20002900
W/MapperHal(29662): buffer descriptor with invalid usage bits 0x2000
I/chatty  (29662): uid=10085(com.example.demo_app) CodecLooper identical 1 line
W/MapperHal(29662): buffer descriptor with invalid usage bits 0x2000
W/MapperHal(29662): buffer descriptor with invalid usage bits 0x2000
W/MapperHal(29662): buffer descriptor with invalid usage bits 0x2000
I/flutter (29662): https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4
I/flutter (29662): https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4
I/flutter (29662): https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4
I/flutter (29662): https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4
I/flutter (29662): https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4
I/flutter (29662): https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4
I/flutter (29662): https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4
I/flutter (29662): https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4
I/flutter (29662): https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4
Lost connection to device.

DemoShowScreen的代码如下


import 'package:flutter/material.dart';
import '../../models/show.dart';
import 'package:video_player/video_player.dart';
class DemoShow extends StatefulWidget {
  static const id = "show_show_screen";
  @override
  _DemoShowState createState() => _DemoShowState();
}
class _DemoShowState extends State<DemoShow> {
  VideoPlayerController _controller;
  Future<void> _initializeVideoPlayerFuture;
  Show data;
  var media;
  @override
  void initState() {
    // Create and store the VideoPlayerController. The VideoPlayerController
    // offers several different constructors to play videos from assets, files,
    // or the internet.
      media = 'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4';
      _controller = VideoPlayerController.network(media);      
      // Initialize the controller and store the Future for later use.
      _initializeVideoPlayerFuture = _controller.initialize();
      // Use the controller to loop the video.
      _controller.setLooping(false);

    super.initState();
  }
  @override
  void dispose() {
    // Ensure disposing of the VideoPlayerController to free up resources.
    _controller.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    data = ModalRoute.of(context).settings.arguments;
    setState(() {
      media = data.mediaUrl;
      print(media);
    });
    return Scaffold(
      appBar: AppBar(
        title: Text('${data.title}', style: Theme.of(context).textTheme.title,),
      ),
      // Use a FutureBuilder to display a loading spinner while waiting for the
      // VideoPlayerController to finish initializing.
      body: SingleChildScrollView(
        child: Container(
          child: Column(
            children: <Widget>[
              FutureBuilder(
                future: _initializeVideoPlayerFuture,
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.done) {
                    // If the VideoPlayerController has finished initialization, use
                    // the data it provides to limit the aspect ratio of the video.
                    return AspectRatio(
                      aspectRatio: _controller.value.aspectRatio,
                      // Use the VideoPlayer widget to display the video.
                      child: VideoPlayer(_controller),
                    );
                  } else {
                    // If the VideoPlayerController is still initializing, show a
                    // loading spinner.
                    return Center(child: CircularProgressIndicator());
                  }
                },
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // Wrap the play or pause in a call to `setState`. This ensures the
          // correct icon is shown.
          setState(() {
            // If the video is playing, pause it.
            if (_controller.value.isPlaying) {
              _controller.pause();
            } else {
              // If the video is paused, play it.
              _controller.play();
              _controller.seekTo(Duration(seconds: 32));
            }
          });
        },
        // Display the correct icon depending on the state of the player.
        child: Icon(
          _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

我试图用我从 pushNamed 参数获得的 URL 替换bee.mp4 butterfly.mp4 URL。

在屏幕上,我可以在视频播放器中看到butterfly.mp4帧,但它永远不会播放或更新bee.mp4

此外,我尝试在 initState 中调用 ModalRoute.of(context).settings.arguments;,但最终得到null,以便删除代码。

### 更新代码后编辑 视频帧更改为当前视频,但我仍然面临播放视频的问题。

带有Stateless Widget的新代码:


import 'package:flutter/material.dart';
import '../../models/demo.dart';
import 'package:video_player/video_player.dart';
class DemoShowScreen extends StatelessWidget {
  static const id = "demo_show_screen";
  VideoPlayerController _controller;
  Future<void> _initializeVideoPlayerFuture;
  @override
  Widget build(BuildContext context) {
    Show data = ModalRoute.of(context).settings.arguments;
    _controller = VideoPlayerController.network(data.mediaUrl);
    _controller.setLooping(false);  
    return Scaffold(
      body: Column(
        children: <Widget>[
          Container(
            child: FutureBuilder(
              future: _controller.initialize(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  // If the VideoPlayerController has finished initialization, use
                  // the data it provides to limit the aspect ratio of the video.
                  return AspectRatio(
                    aspectRatio: _controller.value.aspectRatio,
                    // Use the VideoPlayer widget to display the video.
                    child: VideoPlayer(_controller),
                  );
                } else {
                  // If the VideoPlayerController is still initializing, show a
                  // loading spinner.
                  return Center(child: CircularProgressIndicator());
                }
              },
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: () {
            // Wrap the play or pause in a call to `setState`. This ensures the
            // correct icon is shown.
              // If the video is playing, pause it.
              if (_controller.value.isPlaying) {
                _controller.pause();
              } else {
                // If the video is paused, play it.
                _controller.play();
                _controller.seekTo(Duration(seconds: 32));
              }
          },
          // Display the correct icon depending on the state of the player.
          child: Icon(
            _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
          ),
      ),
    );
  }
}

错误代码 :

I/ExoPlayerImpl(22057): Release 877cbe7 [ExoPlayerLib/2.9.6] [ASUS_X00T_2, ASUS_X00TD, asus, 28] [goog.exo.core]
E/BufferQueueProducer(22057): [SurfaceTexture-0-22057-1] cancelBuffer: BufferQueue has been abandoned
I/chatty  (22057): uid=10085(com.example.demo_app) JNISurfaceTextu identical 5 lines
E/BufferQueueProducer(22057): [SurfaceTexture-0-22057-1] cancelBuffer: BufferQueue has been abandoned
D/SurfaceUtils(22057): disconnecting from surface 0x7b17aa2010, reason disconnectFromSurface
I/ExoPlayerImpl(22057): Init cf82360 [ExoPlayerLib/2.9.6] [ASUS_X00T_2, ASUS_X00TD, asus, 28]
I/OMXClient(22057): IOmx service obtained
D/SurfaceUtils(22057): connecting to surface 0x7b337e0010, reason connectToSurface
I/MediaCodec(22057): [OMX.qcom.video.decoder.avc] setting surface generation to 22586371
D/SurfaceUtils(22057): disconnecting from surface 0x7b337e0010, reason connectToSurface(reconnect)
D/SurfaceUtils(22057): connecting to surface 0x7b337e0010, reason connectToSurface(reconnect)
I/ExtendedACodec(22057): setupVideoDecoder()
I/ExtendedACodec(22057): Decoder will be in frame by frame mode
D/SurfaceUtils(22057): set up nativeWindow 0x7b337e0010 for 1280x720, color 0x7fa30c06, rotation 0, usage 0x20002900
W/MapperHal(22057): buffer descriptor with invalid usage bits 0x2000
W/MapperHal(22057): buffer descriptor with invalid usage bits 0x2000
I/OMXClient(22057): IOmx service obtained
I/ACodec  (22057): codec does not support config priority (err -2147483648)
I/ACodec  (22057): codec does not support config priority (err -2147483648)
I/ACodec  (22057): codec does not support config operating rate (err -2147483648)
W/ExtendedACodec(22057): Failed to get extension for extradata parameter
W/MapperHal(22057): buffer descriptor with invalid usage bits 0x2000
W/MapperHal(22057): buffer descriptor with invalid usage bits 0x2000
D/SurfaceUtils(22057): set up nativeWindow 0x7b337e0010 for 1280x720, color 0x7fa30c06, rotation 0, usage 0x20002900
W/MapperHal(22057): buffer descriptor with invalid usage bits 0x2000
W/MapperHal(22057): buffer descriptor with invalid usage bits 0x2000
W/MapperHal(22057): buffer descriptor with invalid usage bits 0x2000
W/MapperHal(22057): buffer descriptor with invalid usage bits 0x2000 

尝试将整个类包装在 StatelessWidget 中,因为在构建方法之前调用了 initState,

还要避免在构建函数中调用 setState。

class WebViewPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ArgumentData data = ModalRoute.of(context).settings.arguments;
    return Scaffold(
body:DemoShow(data.url),
    );
  }
}

最新更新