Merge branch 'krille/just-audio' into 'main'

refactor: Switch to just audio for playing sounds

See merge request famedly/fluffychat!772
This commit is contained in:
Krille Fear 2022-03-13 07:15:59 +00:00
commit 3dfbf29bd5
7 changed files with 74 additions and 72 deletions

View File

@ -3,8 +3,8 @@ import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:just_audio/just_audio.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
@ -46,7 +46,7 @@ class _AudioPlayerState extends State<AudioPlayerWidget> {
@override @override
void dispose() { void dispose() {
if (audioPlayer.state == PlayerState.PLAYING) { if (audioPlayer.playerState.playing) {
audioPlayer.stop(); audioPlayer.stop();
} }
onAudioPositionChanged?.cancel(); onAudioPositionChanged?.cancel();
@ -88,24 +88,22 @@ class _AudioPlayerState extends State<AudioPlayerWidget> {
void _playAction() async { void _playAction() async {
if (AudioPlayerWidget.currentId != widget.event.eventId) { if (AudioPlayerWidget.currentId != widget.event.eventId) {
if (AudioPlayerWidget.currentId != null) { if (AudioPlayerWidget.currentId != null) {
if (audioPlayer.state != PlayerState.STOPPED) { if (audioPlayer.playerState.playing) {
await audioPlayer.stop(); await audioPlayer.stop();
setState(() {}); setState(() {});
} }
} }
AudioPlayerWidget.currentId = widget.event.eventId; AudioPlayerWidget.currentId = widget.event.eventId;
} }
switch (audioPlayer.state) { if (audioPlayer.playerState.playing) {
case PlayerState.PLAYING:
await audioPlayer.pause(); await audioPlayer.pause();
break; return;
case PlayerState.PAUSED: } else if (audioPlayer.position != Duration.zero) {
await audioPlayer.resume(); await audioPlayer.play();
break; return;
case PlayerState.STOPPED: }
default:
onAudioPositionChanged ??= onAudioPositionChanged ??= audioPlayer.positionStream.listen((state) {
audioPlayer.onAudioPositionChanged.listen((state) {
setState(() { setState(() {
statusText = statusText =
'${state.inMinutes.toString().padLeft(2, '0')}:${(state.inSeconds % 60).toString().padLeft(2, '0')}'; '${state.inMinutes.toString().padLeft(2, '0')}:${(state.inSeconds % 60).toString().padLeft(2, '0')}';
@ -114,22 +112,20 @@ class _AudioPlayerState extends State<AudioPlayerWidget> {
.round(); .round();
}); });
}); });
onDurationChanged ??= audioPlayer.onDurationChanged.listen((max) => onDurationChanged ??= audioPlayer.durationStream.listen((max) => max == null
setState(() => maxPosition = max.inMilliseconds.toDouble())); ? null
: setState(() => maxPosition = max.inMilliseconds.toDouble()));
onPlayerStateChanged ??= onPlayerStateChanged ??=
audioPlayer.onPlayerStateChanged.listen((_) => setState(() {})); audioPlayer.playingStream.listen((_) => setState(() {}));
onPlayerError ??= audioPlayer.onPlayerError.listen((e) { audioPlayer.setFilePath(audioFile!.path);
audioPlayer.play().catchError((e, s) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text(L10n.of(context)!.oopsSomethingWentWrong), content: Text(L10n.of(context)!.oopsSomethingWentWrong),
), ),
); );
SentryController.captureException(e, StackTrace.current); SentryController.captureException(e, s);
}); });
await audioPlayer.play(audioFile!.path);
break;
}
} }
static const double buttonSize = 36; static const double buttonSize = 36;
@ -191,7 +187,7 @@ class _AudioPlayerState extends State<AudioPlayerWidget> {
color: widget.color.withAlpha(64), color: widget.color.withAlpha(64),
borderRadius: BorderRadius.circular(64), borderRadius: BorderRadius.circular(64),
child: Icon( child: Icon(
audioPlayer.state == PlayerState.PLAYING audioPlayer.playerState.playing
? Icons.pause_outlined ? Icons.pause_outlined
: Icons.play_arrow_outlined, : Icons.play_arrow_outlined,
color: widget.color, color: widget.color,

View File

@ -80,7 +80,7 @@ class MessageContent extends StatelessWidget {
case MessageTypes.Sticker: case MessageTypes.Sticker:
return Sticker(event); return Sticker(event);
case MessageTypes.Audio: case MessageTypes.Audio:
if (PlatformInfos.isMobile) { if (PlatformInfos.isMobile || PlatformInfos.isDesktop) {
return AudioPlayerWidget( return AudioPlayerWidget(
event, event,
color: textColor, color: textColor,

View File

@ -22,8 +22,8 @@ import 'dart:math';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart'; import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:just_audio/just_audio.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
import 'package:pedantic/pedantic.dart'; import 'package:pedantic/pedantic.dart';
import 'package:wakelock/wakelock.dart'; import 'package:wakelock/wakelock.dart';
@ -174,7 +174,9 @@ class _MyCallingPage extends State<Calling> {
void _playCallSound() async { void _playCallSound() async {
const path = 'assets/sounds/call.ogg'; const path = 'assets/sounds/call.ogg';
if (kIsWeb || PlatformInfos.isMobile || PlatformInfos.isMacOS) { if (kIsWeb || PlatformInfos.isMobile || PlatformInfos.isMacOS) {
await AssetsAudioPlayer.newPlayer().open(Audio(path)); final player = AudioPlayer();
await player.setAsset(path);
player.play();
} else { } else {
Logs().w('Playing sound not implemented for this platform!'); Logs().w('Playing sound not implemented for this platform!');
} }

View File

@ -1,7 +1,7 @@
import 'package:flutter/foundation.dart'; 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:flutter_ringtone_player/flutter_ringtone_player.dart';
import 'package:just_audio/just_audio.dart';
import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/platform_infos.dart';
@ -14,7 +14,7 @@ class UserMediaManager {
static final UserMediaManager _instance = UserMediaManager._internal(); static final UserMediaManager _instance = UserMediaManager._internal();
AssetsAudioPlayer? _assetsAudioPlayer; AudioPlayer? _assetsAudioPlayer;
Future<void> startRingingTone() async { Future<void> startRingingTone() async {
if (PlatformInfos.isMobile) { if (PlatformInfos.isMobile) {
@ -22,9 +22,9 @@ class UserMediaManager {
} else if ((kIsWeb || PlatformInfos.isMacOS) && } else if ((kIsWeb || PlatformInfos.isMacOS) &&
_assetsAudioPlayer != null) { _assetsAudioPlayer != null) {
const path = 'assets/sounds/phone.ogg'; const path = 'assets/sounds/phone.ogg';
final player = _assetsAudioPlayer = AssetsAudioPlayer.newPlayer(); final player = _assetsAudioPlayer = AudioPlayer();
await player.open(Audio(path), player.setAsset(path);
autoStart: true, loopMode: LoopMode.playlist); player.play();
} }
return; return;
} }

View File

@ -5,9 +5,7 @@
import FlutterMacOS import FlutterMacOS
import Foundation import Foundation
import assets_audio_player import audio_session
import assets_audio_player_web
import audioplayers
import connectivity_plus_macos import connectivity_plus_macos
import desktop_drop import desktop_drop
import desktop_lifecycle import desktop_lifecycle
@ -19,6 +17,7 @@ import flutter_secure_storage_macos
import flutter_web_auth import flutter_web_auth
import flutter_webrtc import flutter_webrtc
import geolocator_apple import geolocator_apple
import just_audio
import package_info import package_info
import package_info_plus_macos import package_info_plus_macos
import path_provider_macos import path_provider_macos
@ -29,9 +28,7 @@ import video_compress
import wakelock_macos import wakelock_macos
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AssetsAudioPlayerPlugin.register(with: registry.registrar(forPlugin: "AssetsAudioPlayerPlugin")) AudioSessionPlugin.register(with: registry.registrar(forPlugin: "AudioSessionPlugin"))
AssetsAudioPlayerWebPlugin.register(with: registry.registrar(forPlugin: "AssetsAudioPlayerWebPlugin"))
AudioplayersPlugin.register(with: registry.registrar(forPlugin: "AudioplayersPlugin"))
ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin")) ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin"))
DesktopDropPlugin.register(with: registry.registrar(forPlugin: "DesktopDropPlugin")) DesktopDropPlugin.register(with: registry.registrar(forPlugin: "DesktopDropPlugin"))
DesktopLifecyclePlugin.register(with: registry.registrar(forPlugin: "DesktopLifecyclePlugin")) DesktopLifecyclePlugin.register(with: registry.registrar(forPlugin: "DesktopLifecyclePlugin"))
@ -43,6 +40,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterWebAuthPlugin.register(with: registry.registrar(forPlugin: "FlutterWebAuthPlugin")) FlutterWebAuthPlugin.register(with: registry.registrar(forPlugin: "FlutterWebAuthPlugin"))
FlutterWebRTCPlugin.register(with: registry.registrar(forPlugin: "FlutterWebRTCPlugin")) FlutterWebRTCPlugin.register(with: registry.registrar(forPlugin: "FlutterWebRTCPlugin"))
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin")) GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))
JustAudioPlugin.register(with: registry.registrar(forPlugin: "JustAudioPlugin"))
FLTPackageInfoPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlugin")) FLTPackageInfoPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlugin"))
FLTPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlusPlugin")) FLTPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlusPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))

View File

@ -71,20 +71,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0" 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: async:
dependency: transitive dependency: transitive
description: description:
@ -92,13 +78,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.8.2" version: "2.8.2"
audioplayers: audio_session:
dependency: "direct main" dependency: transitive
description: description:
name: audioplayers name: audio_session
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.20.1" version: "0.1.6+1"
base58check: base58check:
dependency: transitive dependency: transitive
description: description:
@ -891,6 +877,27 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.6.3" 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: keyboard_shortcuts:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -10,8 +10,6 @@ dependencies:
adaptive_dialog: ^1.3.0 adaptive_dialog: ^1.3.0
adaptive_theme: ^2.3.0 adaptive_theme: ^2.3.0
animations: ^2.0.2 animations: ^2.0.2
assets_audio_player: ^3.0.4+1
audioplayers: ^0.20.1
blurhash_dart: ^1.1.0 blurhash_dart: ^1.1.0
cached_network_image: ^3.2.0 cached_network_image: ^3.2.0
callkeep: ^0.3.2 callkeep: ^0.3.2
@ -55,6 +53,7 @@ dependencies:
image: ^3.1.1 image: ^3.1.1
image_picker: ^0.8.4+8 image_picker: ^0.8.4+8
intl: any intl: any
just_audio: ^0.9.20
keyboard_shortcuts: ^0.1.4 keyboard_shortcuts: ^0.1.4
localstorage: ^4.0.0+1 localstorage: ^4.0.0+1
lottie: ^1.2.2 lottie: ^1.2.2