Fix audio playing in chat-only mode (#272)

* Rely on widget lifecycle for video

* Remove old comments

* Remove unnecessary webview reload

* Fix js exceptions
This commit is contained in:
Tommy 2023-06-19 22:02:18 -04:00 • committed by GitHub
parent f12355ad7a
commit 698fe8d05d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 33 deletions

View file

@ -22,6 +22,10 @@ class _VideoState extends State<Video> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
if (widget.videoStore.settingsStore.showVideo) {
widget.videoStore.videoWebViewController
.loadRequest(Uri.parse(widget.videoStore.videoUrl));
}
WidgetsBinding.instance.addObserver(this);
}
@ -45,6 +49,9 @@ class _VideoState extends State<Video> with WidgetsBindingObserver {
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
widget.videoStore.videoWebViewController
.loadRequest(Uri.parse('about:blank'));
super.dispose();
}
}

View file

@ -55,11 +55,7 @@ abstract class VideoStoreBase with Store {
NavigationDelegate(
onPageFinished: (_) => initVideo(),
),
)
..loadRequest(Uri.parse(videoUrl));
// allowsInlineMediaPlayback: true,
// initialMediaPlaybackPolicy: AutoMediaPlaybackPolicy.always_allow,
);
/// The timer that handles hiding the overlay automatically
late Timer _overlayTimer;
@ -97,6 +93,22 @@ abstract class VideoStoreBase with Store {
required this.authStore,
required this.settingsStore,
}) {
// Initialize the video webview params for iOS to enable video autoplay.
if (WebViewPlatform.instance is WebKitWebViewPlatform) {
_videoWebViewParams = WebKitWebViewControllerCreationParams(
allowsInlineMediaPlayback: true,
mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
);
} else {
_videoWebViewParams = const PlatformWebViewControllerCreationParams();
}
// Initialize the video webview params for Android to enable video autoplay.
if (videoWebViewController.platform is AndroidWebViewController) {
(videoWebViewController.platform as AndroidWebViewController)
.setMediaPlaybackRequiresUserGesture(false);
}
// On Android, enable auto PiP mode (setAutoEnterEnabled) if the device supports it.
if (Platform.isAndroid) {
SimplePip.isAutoPipAvailable.then((isAutoPipAvailable) {
@ -114,36 +126,22 @@ abstract class VideoStoreBase with Store {
(_) => videoWebViewController.loadRequest(Uri.parse(videoUrl)),
);
// Initialize the video webview params for iOS to enable video autoplay.
if (WebViewPlatform.instance is WebKitWebViewPlatform) {
_videoWebViewParams = WebKitWebViewControllerCreationParams(
allowsInlineMediaPlayback: true,
mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
);
} else {
_videoWebViewParams = const PlatformWebViewControllerCreationParams();
}
// Initialize the video webview params for Android to enable video autoplay.
if (videoWebViewController.platform is AndroidWebViewController) {
(videoWebViewController.platform as AndroidWebViewController)
.setMediaPlaybackRequiresUserGesture(false);
}
updateStreamInfo();
}
/// Initializes the video webview.
@action
Future<void> initVideo() async {
// Add event listeners to notify the JavaScript channels when the video plays and pauses.
try {
videoWebViewController.runJavaScript(
'document.getElementsByTagName("video")[0].addEventListener("pause", () => VideoPause.postMessage("video paused"));');
videoWebViewController.runJavaScript(
'document.getElementsByTagName("video")[0].addEventListener("playing", () => VideoPlaying.postMessage("video playing"));');
} catch (e) {
debugPrint(e.toString());
if (await videoWebViewController.currentUrl() == videoUrl) {
// Add event listeners to notify the JavaScript channels when the video plays and pauses.
try {
videoWebViewController.runJavaScript(
'document.getElementsByTagName("video")[0].addEventListener("pause", () => VideoPause.postMessage("video paused"));');
videoWebViewController.runJavaScript(
'document.getElementsByTagName("video")[0].addEventListener("playing", () => VideoPlaying.postMessage("video playing"));');
} catch (e) {
debugPrint(e.toString());
}
}
// Determine whether the device is an iPad or not.
@ -262,10 +260,6 @@ abstract class VideoStoreBase with Store {
});
}
// Not ideal, but seems like the only way of disposing of the video properly.
// Will both prevent the video from continuing to play when dismissed and closes PiP on iOS.
if (Platform.isIOS) videoWebViewController.reload();
_disposeOverlayReaction();
}
}