如何在Flutter VideoPlayer上捕获服务器端错误?

问题描述

当尝试使用Vimeo网址,video_player和chewie播放器获取视频时,出现错误

Exception has occurred.
PlatformException (PlatformException(VideoError,Video player had error com.google.android.exoplayer2.ExoPlaybackException: com.google.android.exoplayer2.upstream.HttpDataSource$InvalidResponseCodeException: Response code: 403,null))

如何打印网站上图片中显示的图像而不是此处的错误?

  VideoPlayerController _videoPlayerController;
  ChewieController _chewieController;
  double _aspectRatio = 16 / 9;
  TabController _tabController;

  void _handleTabSelection() {
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(vsync: this,length: 2);
    _tabController.addListener(_handleTabSelection);
    _videoPlayerController =
        VideoPlayerController.network('myvideourl');
    _chewieController = ChewieController(
        allowedScreenSleep: false,allowFullScreen: true,deviceOrientationsAfterFullScreen: [
          DeviceOrientation.landscapeRight,DeviceOrientation.landscapeLeft,DeviceOrientation.portraitUp,DeviceOrientation.portraitDown,],videoPlayerController: _videoPlayerController,aspectRatio: _aspectRatio,autoInitialize: true,autoPlay: true,showControls: true,errorBuilder: (context,errorMessage) {
          return Center(
            child: Text(
              errorMessage,style: TextStyle(color: Colors.white),),);
        });
    _chewieController.addListener(() {
      if (_chewieController.isFullScreen) {
        SystemChrome.setPreferredOrientations([
          DeviceOrientation.landscapeRight,]);
      } else {
        SystemChrome.setPreferredOrientations([
          DeviceOrientation.portraitUp,]);
      }
    });
  }

  @override
  void dispose() {
    _videoPlayerController.dispose();
    _chewieController.dispose();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,]);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color(0xff4fc1e9),appBar: AppBar(
          backgroundColor: Color(0xff3bafda),shadowColor: Color(0xff4a89dc),leading: IconButton(
              icon: Icon(Icons.menu),iconSize: 30.0,color: Colors.white,onPressed: () {} //=> _scaffoldKey.currentState.openDrawer(),title: Image.asset(
            "assets/images/firm-logo.png",fit: BoxFit.contain,height: 32,centerTitle: true,elevation: 5,body: Column(
          children: [
            Stack(
              children: <Widget>[
                Container(
                  height: MediaQuery.of(context).size.width -
                      MediaQuery.of(context).size.width / 3.5,decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(30.0),boxShadow: [
                      BoxShadow(
                          color: Colors.black26,offset: Offset(0.0,2.0),blurRadius: 6.0),child: ClipRRect(
                    child: Container(
                      color: Colors.black,child: Center(
                        child: Chewie(
                          controller: _chewieController,

enter image description here

如何在Flutter应用程序中捕获此错误

解决方法

您可以听到以下错误:

_controller.addListener(() {
 if (_controller.value.hasError) {
  print(_controller.value.errorDescription);
 }
});

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...