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: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<AudioPlayerWidget> {
@override
void dispose() {
if (audioPlayer.state == PlayerState.PLAYING) {
if (audioPlayer.playerState.playing) {
audioPlayer.stop();
}
onAudioPositionChanged?.cancel();
@ -88,24 +88,22 @@ class _AudioPlayerState extends State<AudioPlayerWidget> {
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:
if (audioPlayer.playerState.playing) {
await audioPlayer.pause();
break;
case PlayerState.PAUSED:
await audioPlayer.resume();
break;
case PlayerState.STOPPED:
default:
onAudioPositionChanged ??=
audioPlayer.onAudioPositionChanged.listen((state) {
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')}';
@ -114,22 +112,20 @@ class _AudioPlayerState extends State<AudioPlayerWidget> {
.round();
});
});
onDurationChanged ??= audioPlayer.onDurationChanged.listen((max) =>
setState(() => maxPosition = max.inMilliseconds.toDouble()));
onDurationChanged ??= audioPlayer.durationStream.listen((max) => max == null
? null
: setState(() => maxPosition = max.inMilliseconds.toDouble()));
onPlayerStateChanged ??=
audioPlayer.onPlayerStateChanged.listen((_) => setState(() {}));
onPlayerError ??= audioPlayer.onPlayerError.listen((e) {
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, StackTrace.current);
SentryController.captureException(e, s);
});
await audioPlayer.play(audioFile!.path);
break;
}
}
static const double buttonSize = 36;
@ -191,7 +187,7 @@ class _AudioPlayerState extends State<AudioPlayerWidget> {
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,

View File

@ -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,

View File

@ -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<Calling> {
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!');
}

View File

@ -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<void> 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;
}

View File

@ -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"))

View File

@ -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:

View File

@ -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