From 85c1710ae3097860fcc099573bf8c0837e093733 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sun, 13 Mar 2022 07:54:44 +0100 Subject: [PATCH] refactor: Switch to just audio for playing sounds --- lib/pages/chat/events/audio_player.dart | 72 +++++++++---------- lib/pages/chat/events/message_content.dart | 2 +- lib/pages/dialer/dialer.dart | 6 +- lib/utils/voip/user_media_manager.dart | 10 +-- macos/Flutter/GeneratedPluginRegistrant.swift | 10 ++- pubspec.lock | 43 ++++++----- pubspec.yaml | 3 +- 7 files changed, 74 insertions(+), 72 deletions(-) diff --git a/lib/pages/chat/events/audio_player.dart b/lib/pages/chat/events/audio_player.dart index aa3cd536..5a6c8a62 100644 --- a/lib/pages/chat/events/audio_player.dart +++ b/lib/pages/chat/events/audio_player.dart @@ -3,8 +3,8 @@ import 'dart:io'; import 'package:flutter/material.dart'; -import 'package:audioplayers/audioplayers.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; +import 'package:just_audio/just_audio.dart'; import 'package:matrix/matrix.dart'; import 'package:path_provider/path_provider.dart'; @@ -46,7 +46,7 @@ class _AudioPlayerState extends State { @override void dispose() { - if (audioPlayer.state == PlayerState.PLAYING) { + if (audioPlayer.playerState.playing) { audioPlayer.stop(); } onAudioPositionChanged?.cancel(); @@ -88,48 +88,44 @@ class _AudioPlayerState extends State { void _playAction() async { if (AudioPlayerWidget.currentId != widget.event.eventId) { if (AudioPlayerWidget.currentId != null) { - if (audioPlayer.state != PlayerState.STOPPED) { + if (audioPlayer.playerState.playing) { await audioPlayer.stop(); setState(() {}); } } AudioPlayerWidget.currentId = widget.event.eventId; } - switch (audioPlayer.state) { - case PlayerState.PLAYING: - await audioPlayer.pause(); - break; - case PlayerState.PAUSED: - await audioPlayer.resume(); - break; - case PlayerState.STOPPED: - default: - onAudioPositionChanged ??= - audioPlayer.onAudioPositionChanged.listen((state) { - setState(() { - statusText = - '${state.inMinutes.toString().padLeft(2, '0')}:${(state.inSeconds % 60).toString().padLeft(2, '0')}'; - currentPosition = ((state.inMilliseconds.toDouble() / maxPosition) * - AudioPlayerWidget.wavesCount) - .round(); - }); - }); - onDurationChanged ??= audioPlayer.onDurationChanged.listen((max) => - setState(() => maxPosition = max.inMilliseconds.toDouble())); - onPlayerStateChanged ??= - audioPlayer.onPlayerStateChanged.listen((_) => setState(() {})); - onPlayerError ??= audioPlayer.onPlayerError.listen((e) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text(L10n.of(context)!.oopsSomethingWentWrong), - ), - ); - SentryController.captureException(e, StackTrace.current); - }); - - await audioPlayer.play(audioFile!.path); - break; + if (audioPlayer.playerState.playing) { + await audioPlayer.pause(); + return; + } else if (audioPlayer.position != Duration.zero) { + await audioPlayer.play(); + return; } + + onAudioPositionChanged ??= audioPlayer.positionStream.listen((state) { + setState(() { + statusText = + '${state.inMinutes.toString().padLeft(2, '0')}:${(state.inSeconds % 60).toString().padLeft(2, '0')}'; + currentPosition = ((state.inMilliseconds.toDouble() / maxPosition) * + AudioPlayerWidget.wavesCount) + .round(); + }); + }); + onDurationChanged ??= audioPlayer.durationStream.listen((max) => max == null + ? null + : setState(() => maxPosition = max.inMilliseconds.toDouble())); + onPlayerStateChanged ??= + audioPlayer.playingStream.listen((_) => setState(() {})); + audioPlayer.setFilePath(audioFile!.path); + audioPlayer.play().catchError((e, s) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(L10n.of(context)!.oopsSomethingWentWrong), + ), + ); + SentryController.captureException(e, s); + }); } static const double buttonSize = 36; @@ -191,7 +187,7 @@ class _AudioPlayerState extends State { color: widget.color.withAlpha(64), borderRadius: BorderRadius.circular(64), child: Icon( - audioPlayer.state == PlayerState.PLAYING + audioPlayer.playerState.playing ? Icons.pause_outlined : Icons.play_arrow_outlined, color: widget.color, diff --git a/lib/pages/chat/events/message_content.dart b/lib/pages/chat/events/message_content.dart index 61b0bdc4..7e451786 100644 --- a/lib/pages/chat/events/message_content.dart +++ b/lib/pages/chat/events/message_content.dart @@ -80,7 +80,7 @@ class MessageContent extends StatelessWidget { case MessageTypes.Sticker: return Sticker(event); case MessageTypes.Audio: - if (PlatformInfos.isMobile) { + if (PlatformInfos.isMobile || PlatformInfos.isDesktop) { return AudioPlayerWidget( event, color: textColor, diff --git a/lib/pages/dialer/dialer.dart b/lib/pages/dialer/dialer.dart index a868f515..5bac3bea 100644 --- a/lib/pages/dialer/dialer.dart +++ b/lib/pages/dialer/dialer.dart @@ -22,8 +22,8 @@ import 'dart:math'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:assets_audio_player/assets_audio_player.dart'; import 'package:flutter_webrtc/flutter_webrtc.dart'; +import 'package:just_audio/just_audio.dart'; import 'package:matrix/matrix.dart'; import 'package:pedantic/pedantic.dart'; import 'package:wakelock/wakelock.dart'; @@ -174,7 +174,9 @@ class _MyCallingPage extends State { void _playCallSound() async { const path = 'assets/sounds/call.ogg'; if (kIsWeb || PlatformInfos.isMobile || PlatformInfos.isMacOS) { - await AssetsAudioPlayer.newPlayer().open(Audio(path)); + final player = AudioPlayer(); + await player.setAsset(path); + player.play(); } else { Logs().w('Playing sound not implemented for this platform!'); } diff --git a/lib/utils/voip/user_media_manager.dart b/lib/utils/voip/user_media_manager.dart index 8d06ffbf..874da93e 100644 --- a/lib/utils/voip/user_media_manager.dart +++ b/lib/utils/voip/user_media_manager.dart @@ -1,7 +1,7 @@ import 'package:flutter/foundation.dart'; -import 'package:assets_audio_player/assets_audio_player.dart'; import 'package:flutter_ringtone_player/flutter_ringtone_player.dart'; +import 'package:just_audio/just_audio.dart'; import 'package:fluffychat/utils/platform_infos.dart'; @@ -14,7 +14,7 @@ class UserMediaManager { static final UserMediaManager _instance = UserMediaManager._internal(); - AssetsAudioPlayer? _assetsAudioPlayer; + AudioPlayer? _assetsAudioPlayer; Future startRingingTone() async { if (PlatformInfos.isMobile) { @@ -22,9 +22,9 @@ class UserMediaManager { } else if ((kIsWeb || PlatformInfos.isMacOS) && _assetsAudioPlayer != null) { const path = 'assets/sounds/phone.ogg'; - final player = _assetsAudioPlayer = AssetsAudioPlayer.newPlayer(); - await player.open(Audio(path), - autoStart: true, loopMode: LoopMode.playlist); + final player = _assetsAudioPlayer = AudioPlayer(); + player.setAsset(path); + player.play(); } return; } diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 8b78e3a6..62cfaffc 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,9 +5,7 @@ import FlutterMacOS import Foundation -import assets_audio_player -import assets_audio_player_web -import audioplayers +import audio_session import connectivity_plus_macos import desktop_drop import desktop_lifecycle @@ -19,6 +17,7 @@ import flutter_secure_storage_macos import flutter_web_auth import flutter_webrtc import geolocator_apple +import just_audio import package_info import package_info_plus_macos import path_provider_macos @@ -29,9 +28,7 @@ import video_compress import wakelock_macos func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { - AssetsAudioPlayerPlugin.register(with: registry.registrar(forPlugin: "AssetsAudioPlayerPlugin")) - AssetsAudioPlayerWebPlugin.register(with: registry.registrar(forPlugin: "AssetsAudioPlayerWebPlugin")) - AudioplayersPlugin.register(with: registry.registrar(forPlugin: "AudioplayersPlugin")) + AudioSessionPlugin.register(with: registry.registrar(forPlugin: "AudioSessionPlugin")) ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin")) DesktopDropPlugin.register(with: registry.registrar(forPlugin: "DesktopDropPlugin")) DesktopLifecyclePlugin.register(with: registry.registrar(forPlugin: "DesktopLifecyclePlugin")) @@ -43,6 +40,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { FlutterWebAuthPlugin.register(with: registry.registrar(forPlugin: "FlutterWebAuthPlugin")) FlutterWebRTCPlugin.register(with: registry.registrar(forPlugin: "FlutterWebRTCPlugin")) GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin")) + JustAudioPlugin.register(with: registry.registrar(forPlugin: "JustAudioPlugin")) FLTPackageInfoPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlugin")) FLTPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlusPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) diff --git a/pubspec.lock b/pubspec.lock index 88b83da2..d1c4bd3a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -71,20 +71,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.0" - assets_audio_player: - dependency: "direct main" - description: - name: assets_audio_player - url: "https://pub.dartlang.org" - source: hosted - version: "3.0.4+1" - assets_audio_player_web: - dependency: transitive - description: - name: assets_audio_player_web - url: "https://pub.dartlang.org" - source: hosted - version: "3.0.4+1" async: dependency: transitive description: @@ -92,13 +78,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.8.2" - audioplayers: - dependency: "direct main" + audio_session: + dependency: transitive description: - name: audioplayers + name: audio_session url: "https://pub.dartlang.org" source: hosted - version: "0.20.1" + version: "0.1.6+1" base58check: dependency: transitive description: @@ -891,6 +877,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.6.3" + just_audio: + dependency: "direct main" + description: + name: just_audio + url: "https://pub.dartlang.org" + source: hosted + version: "0.9.20" + just_audio_platform_interface: + dependency: transitive + description: + name: just_audio_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "4.1.0" + just_audio_web: + dependency: transitive + description: + name: just_audio_web + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.7" keyboard_shortcuts: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 4aedef5c..9739d00e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,8 +10,6 @@ dependencies: adaptive_dialog: ^1.3.0 adaptive_theme: ^2.3.0 animations: ^2.0.2 - assets_audio_player: ^3.0.4+1 - audioplayers: ^0.20.1 blurhash_dart: ^1.1.0 cached_network_image: ^3.2.0 callkeep: ^0.3.2 @@ -55,6 +53,7 @@ dependencies: image: ^3.1.1 image_picker: ^0.8.4+8 intl: any + just_audio: ^0.9.20 keyboard_shortcuts: ^0.1.4 localstorage: ^4.0.0+1 lottie: ^1.2.2