使用video_player的文件构造函数拒绝权限

时间:2018-04-26 15:47:19

标签: dart flutter

好的,我设置了一个相对简单的颤动程序来测试video_player插件(https://pub.dartlang.org/documentation/video_player/0.5.1/。)

当我使用"网络"播放器正常工作或者"资产"控制器的构造函数,但是当我尝试使用"文件时#34;构造函数我遇到权限被拒绝错误。我指向的文件是一个mp4文件,它位于应用程序文档文件夹中......

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';

void main() => runApp(new MyApp());

//WILL NOT CHANGE
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

//WILL CHANGE
class _MyAppState extends State<MyApp> {
  String _title = 'App Bar Demo';
  String _myState = 'NO STATE';

  VideoPlayerController _controller;
  bool _isPlaying = false;


  void setControllerDir() async{

  }

  Future<String> appDir() async{
  final directory = await getApplicationDocumentsDirectory();

  return directory.path;
  }


  void _pressed(String message) async {
    //CORRECT
    setState(() {
        _myState = message;
      }
    );
    var testPath = await appDir();
    print(testPath);
    //WRONG
    //_myState = message;
    print(_myState);
  }

  @override
  void initState() {
    super.initState();
    //_controller = new VideoPlayerController.network('http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4',)

    //_controller = new VideoPlayerController.asset('assets/test.mp4')
    var file = new File('/data/user/0/com.example.videotest/app_flutter/test.mp4');
    //_controller = new VideoPlayerController.asset('assets/test.mp4')
    //final directory = await getApplicationDocumentsDirectory();

    _controller = new VideoPlayerController.file(file)

      ..addListener(() {
        final bool isPlaying = _controller.value.isPlaying;
        if (isPlaying != _isPlaying) {
          setState(() {
            _isPlaying = isPlaying;
          });
        }
      })
      ..initialize();
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: _title,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(_title),
          actions: <Widget>[
            new Text('text'),
            new IconButton(icon: new Icon(Icons.add_alert), onPressed: () {_pressed('Alert Pressed');}),
            new IconButton(icon: new Icon(Icons.print), onPressed: () {_pressed('Print Pressed');}),
            new IconButton(icon: new Icon(Icons.people), onPressed: () {_pressed('People Pressed');}),
            new RaisedButton(child: new Text('Button'),onPressed: () {_pressed('Button Pressed');}),

          ],
        ),
        body: new Container(
          padding: const EdgeInsets.all(10.0),
          child: new AspectRatio(
            aspectRatio: 1280 / 720,
            child: new VideoPlayer(_controller),
          ),
        ),
        floatingActionButton: new FloatingActionButton(
          onPressed:
              _controller.value.isPlaying ? _controller.pause : _controller.play,
          child: new Icon(
            _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
          ),
        ),       
      ),
    );
  }
}

我是Android和Flutter开发的新手。我尝试向我的清单添加外部存储权限,但这没有帮助。无论如何,我正在运行扑灭beta 2和颤动医生检查罚款。我在Android Studio的Device File Explorer中使用上传器添加了视频文件,我可以在那里看到该文件。任何线索将不胜感激。谢谢:-D

1 个答案:

答案 0 :(得分:0)

好的,让我澄清一下这里发生了什么,以防任何人在这个问题上遇到困难并需要一些澄清。所以我有一个简单的video_player应用程序示例,当我尝试访问我使用Android中的设备文件资源管理器上传到我的模拟器中的/data/user/0/com.example.videotest/app_flutter目录的视频文件时无效工作室。我得到了许可被拒绝的错误。事实证明,我需要打开一个终端并使用&#34; adb&#34;工具到&#34;推&#34;视频文件到该目录,以获取文件的权限,并获取VideoController对象的文件构造函数以使用该文件。但首先我必须将adb deamon设置为以root身份运行才能获得推送文件的权限。所以它归结为......

adb root
adb push test.mp4 /data/user/0/com.example.videotest/app_flutter/