sarbagyastha / youtube_player_flutter

A Flutter plugin for inline playback or streaming of YouTube videos using the official iFrame Player API.
https://youtube.sarbagyastha.com.np
BSD 3-Clause "New" or "Revised" License
692 stars 784 forks source link

[BUG] Sound keeps playing on IOS device even when controller is disposed #148

Open yousifAlneamy opened 4 years ago

yousifAlneamy commented 4 years ago

When using iOS device, if you go fullscreen then exit fullscreen. The sound of the video keeps running even when the widget and controller are disposed.

To Reproduce You simply run the video then pause it, then run it again in fullscreen mode then exit fullscreen and even when you dispose() the controller and the parent widget, the sound keeps running. I thought this problem only with the simulator, but even when testing on a real device, the same problem apprears.

On Android Works fine

flutter doctor -v
[✓] Flutter (Channel stable, v1.12.13+hotfix.7, on Mac OS X 10.15.3 19D76, locale en-IQ)
    • Flutter version 1.12.13+hotfix.7 at /Users/yousif/development/flutter
    • Framework revision 9f5ff2306b (12 days ago), 2020-01-26 22:38:26 -0800
    • Engine revision a67792536c
    • Dart version 2.7.0

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/yousif/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.3.1, Build version 11C504
    • CocoaPods version 1.8.4

[!] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.41.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.8.0

The widget that Im using:

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

class CustomYoutubePlayer extends StatefulWidget {
  final String videoId;
  final Widget placeholder;

  CustomYoutubePlayer({
    @required this.videoId,
    this.placeholder,
  }) : super(key: ValueKey(videoId));

  @override
  _CustomYoutubePlayerState createState() => _CustomYoutubePlayerState();
}

class _CustomYoutubePlayerState extends State<CustomYoutubePlayer> {
  YoutubePlayerController _youtubePlayerController;
  bool _isVideoReady;
  @override
  void initState() {
    super.initState();
    _isVideoReady = false;

    _youtubePlayerController = YoutubePlayerController(
      initialVideoId: widget.videoId,
      flags: YoutubePlayerFlags(
        controlsVisibleAtStart: true,
        mute: false,
        autoPlay: false,
        forceHideAnnotation: false,
        hideThumbnail: false,
        captionLanguage: 'ar',
      ),
    );
  }

  @override
  void dispose() {
    _youtubePlayerController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Container(
          child: Center(
            child: YoutubePlayer(
              controller: _youtubePlayerController,
              showVideoProgressIndicator: true,
              progressIndicatorColor: Colors.red,
              onReady: () {
                setState(() {
                  _isVideoReady = true;
                });
              },
              onEnded: (meta) {
                _youtubePlayerController
                  ..seekTo(Duration.zero)
                  ..pause();
              },
            ),
          ),
        ),
        if (!_isVideoReady && widget.placeholder is Widget)
          Container(
            color: Colors.white,
            child: Center(
              child: widget.placeholder,
            ),
          ),
      ],
    );
    ;
  }
}
sarbagyastha commented 4 years ago

Version 6.1.0+1 should fix the issue.