balvinderz / video_player_web_hls

Supports hls on chrome and other browsers.
BSD 3-Clause "New" or "Revised" License
52 stars 60 forks source link

[video_player_web_hls] Unable to play hls vidoes in web #19

Closed ale-russ-kabbee closed 2 years ago

ale-russ-kabbee commented 2 years ago

.I'm trying to play hls videos using video_player_web in my flutter web app. But when I try to load the videos it keeps giving "TypeError: Cannot read properties of undefined (reading 'isSupported') at video_player_web_hls._VideoPlayer.new.initialize (http://localhost:46773/packages/video_player_web_hls/video_player_web_ hls.dart.li b.js:260:37) at video_player_web_hls.VideoPlayerPluginHls.new.create (http://localhost:46773/packages/video_player_web_hls/video_player_web_ hls.dart.li b.js:181:16) at create.next () at runBody (http://localhost:46773/dart_sdk.js:39250:34) at Object._async [as async] (http://localhost:46773/dart_sdk.js:39281:7) at video_player_web_hls.VideoPlayerPluginHls.new.create (http://localhost:46773/packages/video_player_web_hls/video_player_web_ hls.dart.li b.js:155:20) at video_player.VideoPlayerController.network.initialize (http://localhost:46773/packages/video_player/video_player.dart.lib.js: 949:69) at initialize.next () at runBody (http://localhost:46773/dart_sdk.js:39250:34) at Object._async [as async] (http://localhost:46773/dart_sdk.js:39281:7) at video_player.VideoPlayerController.network.initialize (http://localhost:46773/packages/video_player/video_player.dart.lib.js: 923:20) at http://localhost:46773/packages/demotest/main.dart.lib.js:287:14 at main._VideoAppState.new.initState (http://localhost:46773/packages/demotest/main.dart.lib.js:292:11) at framework.StatefulElement.new.[_firstBuild] (http://localhost:46773/packages/flutter/src/widgets/widget_span.dart.l ib.js:53342 :54) at framework.StatefulElement.new.mount (http://localhost:46773/packages/flutter/src/widgets/widget_span.dart.l ib.js:30229 :28) at RenderObjectToWidgetElement.new.inflateWidget (http://localhost:46773/packages/flutter/src/widgets/widget_span.dart.l ib.js:28991 :16) at RenderObjectToWidgetElement.new.updateChild (http://localhost:46773/packages/flutter/src/widgets/widget_span.dart.l ib.js:28845 :25) at RenderObjectToWidgetElement.new.[_rebuild] (http://localhost:46773/packages/flutter/src/widgets/widget_span.dart.l ib.js:55830 :37) at RenderObjectToWidgetElement.new.mount (http://localhost:46773/packages/flutter/src/widgets/widget_span.dart.l ib.js:55809 :29) at http://localhost:46773/packages/flutter/src/widgets/widget_span.dart.li b.js:55742: 37 at framework.BuildOwner.new.buildScope (http://localhost:46773/packages/flutter/src/widgets/widget_span.dart.l ib.js:52787 :13) at RenderObjectToWidgetAdapter.new.attachToRenderTree (http://localhost:46773/packages/flutter/src/widgets/widget_span.dart.l ib.js:55741 :17) at binding$5.WidgetsFlutterBinding.new.attachRootWidget (http://localhost:46773/packages/flutter/src/widgets/widget_span.dart.l ib.js:55608 :238) at http://localhost:46773/packages/flutter/src/widgets/widget_span.dart.li b.js:55601: 14 at internalCallback (http://localhost:46773/dart_sdk.js:25311:11)"

Below is the code I'm using

import 'package:video_player/video_player.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(VideoApp());
}

class VideoApp extends StatefulWidget {
  @override
  _VideoAppState createState() => _VideoAppState();
}

class _VideoAppState extends State<VideoApp> {
  VideoPlayerController? _controller;

  @override
  void initState() {
    super.initState();
    try {
      _controller = VideoPlayerController.network(
          'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8')
        ..initialize().then((_) {
          // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
          setState(() {});
        });
      _controller!.setVolume(0.0);
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Video Demo',
      home: Scaffold(
        body: Center(
          child: _controller!.value.initialized
              ? AspectRatio(
                  aspectRatio: _controller!.value.aspectRatio,
                  child: VideoPlayer(_controller!),
                )
              : Container(),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            setState(() {
              _controller!.value.isPlaying
                  ? _controller!.pause()
                  : _controller!.play();
            });
          },
          child: Icon(
            _controller!.value.isPlaying ? Icons.pause : Icons.play_arrow,
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller!.dispose();
  }
}

Can anyone help me how to solve this issues

balvinderz commented 2 years ago

You have not included

In index.html

ale-russ-kabbee commented 2 years ago
<!DOCTYPE html>
<html>
  <head>
    <!--
    If you are serving your web app in a path other than the root, change the
    href value below to reflect the base path you are serving from.

    The path provided below has to start and end with a slash "/" in order for
    it to work correctly.

    For more details:
    * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
  -->
    <base href="/" />

    <meta charset="UTF-8" />
    <meta content="IE=Edge" http-equiv="X-UA-Compatible" />
    <meta name="description" content="A new Flutter project." />

    <!-- iOS meta tags & icons -->
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="apple-mobile-web-app-title" content="demotest" />
    <link rel="apple-touch-icon" href="icons/Icon-192.png" />

    <title>demotest</title>
    <link rel="manifest" href="manifest.json" />
  </head>
  <body>
    <!-- This script installs service_worker.js to provide PWA functionality to
       application. For more information, see:
       https://developers.google.com/web/fundamentals/primers/service-workers -->

    <script>
      var serviceWorkerVersion = null;
      var scriptLoaded = false;
      function loadMainDartJs() {
        if (scriptLoaded) {
          return;
        }
        scriptLoaded = true;
        var scriptTag = document.createElement("script");
        scriptTag.src = "main.dart.js";
        scriptTag.type = "application/javascript";
        document.body.append(scriptTag);
      }

      if ("serviceWorker" in navigator) {
        // Service workers are supported. Use them.
        window.addEventListener("load", function () {
          // Wait for registration to finish before dropping the <script> tag.
          // Otherwise, the browser will load the script multiple times,
          // potentially different versions.
          var serviceWorkerUrl =
            "flutter_service_worker.js?v=" + serviceWorkerVersion;
          navigator.serviceWorker.register(serviceWorkerUrl).then((reg) => {
            function waitForActivation(serviceWorker) {
              serviceWorker.addEventListener("statechange", () => {
                if (serviceWorker.state == "activated") {
                  console.log("Installed new service worker.");
                  loadMainDartJs();
                }
              });
            }
            if (!reg.active && (reg.installing || reg.waiting)) {
              // No active web worker and we have installed or are installing
              // one for the first time. Simply wait for it to activate.
              waitForActivation(reg.installing ?? reg.waiting);
            } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
              // When the app updates the serviceWorkerVersion changes, so we
              // need to ask the service worker to update.
              console.log("New service worker available.");
              reg.update();
              waitForActivation(reg.installing);
            } else {
              // Existing service worker is still good.
              console.log("Loading app from service worker.");
              loadMainDartJs();
            }
          });

          // If service worker doesn't succeed in a reasonable amount of time,
          // fallback to plaint <script> tag.
          setTimeout(() => {
            if (!scriptLoaded) {
              console.warn(
                "Failed to load app from service worker. Falling back to plain <script> tag."
              );
              loadMainDartJs();
            }
          }, 4000);
        });
      } else {
        // Service workers not supported. Just drop the <script> tag.
        loadMainDartJs();
      }
    </script>
    <script
      src="https://cdn.jsdelivr.net/npm/hls.js@latest"
      type="application/javascript"
    ></script>
  </body>
</html>
ale-russ-kabbee commented 2 years ago

I added but the problem is still there

balvinderz commented 2 years ago

Try adding this script tag before end of head. If that doesn't work i will check this on 20th .

ale-russ-kabbee commented 2 years ago

I tried still not working

ale-russ-kabbee commented 2 years ago

Fixed the issue. Apparently all it needed was flutter clean and now its working as expected