refactor: Switch to just audio for playing sounds
This commit is contained in:
parent
3cd9d3a2b2
commit
5eae299910
7 changed files with 74 additions and 72 deletions
|
|
@ -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,48 +88,44 @@ 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:
|
||||
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<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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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!');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue