flutter - 尝试从flutter中的pushNamed的参数访问视频网址时出错
问题描述
我正在尝试将 URL 传递给video controller
通过pushNamed
颤振。
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 参数中获得的butterfly.mp4
URL替换 URL。bee.mp4
在屏幕上我可以看到butterfly.mp4
视频播放器中的帧,但它永远不会播放或更新bee.mp4
另外,我尝试调用ModalRoute.of(context).settings.arguments;
ininitState
但最终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),
);
}
}
推荐阅读
- web-services - SOAP 和 HTTP 响应代码
- java - Spring:无法找到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/util]
- python - 更改 QPushbutton 或 QToolbutton 的图标
- mysql - mysql - 由于高索引基数导致查询缓慢
- laravel - 通过数据库记录进行 Laravel 验证
- android - 如何检查 PeriodicWorkRequest 是否已入队?
- c# - TFS 构建服务器 2015 - 转换 web.config 不正确
- html - 淡出 HTML 元素/CSS 遮罩
- string - Julia:用分隔符连接字符串(相当于 R 的粘贴)
- sql - ssis visual studio 2010和excel时间戳作为日期