flutter_macos_video_player icon indicating copy to clipboard operation
flutter_macos_video_player copied to clipboard

Failed to load video: The operation could not be completed

Open chenliang0571 opened this issue 3 years ago • 10 comments

Unhandled Exception: PlatformException(VideoError, Failed to load video: The operation could not be completed, null, null)

for both network and file:

  • VideoPlayerController.network('https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4'...
  • VideoPlayerController.file(File('/tmp/Butterfly-209.mp4'))
dependencies:
  flutter:
    sdk: flutter
  video_player: ^2.4.5
  video_player_macos: 1.0.6

sample: https://github.com/chenliang0571/video_player/tree/master/video_player/example

chenliang0571 avatar Jul 18 '22 12:07 chenliang0571

@chenliang0571

Hey, here is an example of our MacOS video view.

Please let me know if this helps, I need to update the docs.

import 'package:bap/components/label_text.dart';
import 'package:bap/utils/langauge.dart';
import 'package:chewie/chewie.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:video_player/video_player.dart';

class VideoViewMacOS extends StatefulWidget {
  final String url;

  const VideoViewMacOS({
    Key? key,
    required this.url,
  }) : super(key: key);

  @override
  State<VideoViewMacOS> createState() => _VideoViewMacOSState();
}

class _VideoViewMacOSState extends State<VideoViewMacOS> {
  late VideoPlayerController? _videoPlayerController;
  ChewieController? _chewieController;
  bool _wasError = false;

  @override
  void initState() {
    initAsync();
    super.initState();
  }

  initAsync() async {
    try {
      _videoPlayerController = VideoPlayerController.network(widget.url);
      await _videoPlayerController!.initialize();

      _chewieController = ChewieController(
        videoPlayerController: _videoPlayerController!,
        autoPlay: false,
        looping: false,
      );
      setState(() {});
    } catch (e) {
      setState(() {
        _wasError = true;
      });
      if (kDebugMode) {
        print("There was an issue loading the video player.");
        print(e);
      }
    }
  }

  @override
  void dispose() {
    _videoPlayerController?.dispose();
    _chewieController?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints(
        maxHeight: 400,
      ),
      child: _loadingOrPlayer(),
    );
  }

  _loadingOrPlayer() {
    if (_chewieController == null || _wasError) {
      return _loading();
    }

    return _player();
  }

  _player() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(8),
      child: AspectRatio(
        aspectRatio: 16 / 9,
        child: Chewie(
          controller: _chewieController!,
        ),
      ),
    );
  }

  _loading() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        SpinKitCircle(
          color: Theme.of(context).highlightColor,
        ),
        SizedBox(height: 8),
        LabelText(lang.loadingVideo)
      ],
    );
  }
}

ollyde avatar Jul 18 '22 12:07 ollyde

@ollydixon Thank you for your quick response. It's still not working.

it hangs/stuck here: await _videoPlayerController!.initialize();

sample code: https://github.com/chenliang0571/video_player/tree/master/video_player/example/lib

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.0.0, on macOS 12.4 21F79 darwin-x64, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google
    Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[!] Android Studio (not installed)
[✓] VS Code (version 1.69.1)
[✓] Connected device (1 available)
[✓] HTTP Host Availability

chenliang0571 avatar Jul 18 '22 17:07 chenliang0571

@chenliang0571 it has to be an issue with how the assets are delivered. Can you try hosting the asset somewhere else?

The example I've given seems to work for us :-/

ollyde avatar Jul 19 '22 08:07 ollyde

@chenliang0571 it has to be an issue with how the assets are delivered. Can you try hosting the asset somewhere else?

The example I've given seems to work for us :-/

it works for network now, turns out I didn't add network access in the relevant .entitlements files.

I understand that VideoPlayerController.asset is not working for now. How about VideoPlayerController.file? it seems not working neither.

chenliang0571 avatar Jul 22 '22 08:07 chenliang0571

my bad, VideoPlayerController.file works fine.

chenliang0571 avatar Jul 22 '22 13:07 chenliang0571

my bad, VideoPlayerController.file works fine.

Could you advise what location you are opening videos from and what entitlements have you added to the macos app?

My files are under the getApplicationDocumentsDirectory() from the path_provider plugin, e.g. in a folder like:

/Users/{user name}/Library/Containers/{app id}/Data/Documents/{app name}/

The macos video player controller code never returns from the .initialize() method and I don't see any exception caught in the surrounding try/catch:

  _controller = VideoPlayerController.file(file);
  await _controller!.initialize();

Oddly if I move file to /Users/{user name}/Downloads/ the .initialize() method works. Any ideas how to make it work with the application's own document folder?

@ollydixon it would be great if you'd include a complete macos example project with your plugin implementation

ekuleshov avatar Jul 27 '22 02:07 ekuleshov

@ekuleshov hey I've been away for a few weeks. I'm mainly using this for HTTPs links.

ollyde avatar Aug 01 '22 10:08 ollyde

@ekuleshov hey I've been away for a few weeks. I'm mainly using this for HTTPs links.

@ollydixon right. It works for http and some shared dirs like "Downloads" once the app has corresponding permissions.

But it does not work (with no error or anything) for the app's own internal data folder like /Users/{user name}/Library/Containers/{app id}/Data/Documents/{app name}/ the starting part of that path is returned by the official macos's path_provider plugin. I presumed there is no special permissions required for that location, yet there we are.

I'd appreciate any ideas how to debug this.

ekuleshov avatar Aug 04 '22 19:08 ekuleshov

@ekuleshov I think on MacOS you need to request access to other directories which is out of the scope of this library.

ollyde avatar Aug 05 '22 09:08 ollyde

@ollydixon I'd agree with that if the folder wasn't the app-owned data folder. Besides, the app can successfully write and read files from that folder. Just the video player doesn't work when file is given from that same location.

ekuleshov-idexx avatar Aug 05 '22 16:08 ekuleshov-idexx