mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2024-11-23 20:49:26 +01:00
refactor: Remove flutter_sound
This commit is contained in:
parent
2cf4f47950
commit
334d4c04df
@ -1,26 +1,28 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:typed_data';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
|
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
|
||||||
|
import 'package:audioplayers/audioplayers.dart';
|
||||||
import 'package:famedlysdk/famedlysdk.dart';
|
import 'package:famedlysdk/famedlysdk.dart';
|
||||||
|
import 'package:fluffychat/utils/sentry_controller.dart';
|
||||||
import 'package:fluffychat/views/widgets/message_download_content.dart';
|
import 'package:fluffychat/views/widgets/message_download_content.dart';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_sound_lite/flutter_sound.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:universal_html/html.dart' as html;
|
import 'package:universal_html/html.dart' as html;
|
||||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
import '../../utils/ui_fake.dart' if (dart.library.html) 'dart:ui' as ui;
|
import '../../utils/ui_fake.dart' if (dart.library.html) 'dart:ui' as ui;
|
||||||
import 'matrix.dart';
|
import 'matrix.dart';
|
||||||
import '../../utils/event_extension.dart';
|
import '../../utils/event_extension.dart';
|
||||||
|
|
||||||
class AudioPlayer extends StatefulWidget {
|
class AudioPlayerWidget extends StatefulWidget {
|
||||||
final Color color;
|
final Color color;
|
||||||
final Event event;
|
final Event event;
|
||||||
|
|
||||||
static String currentId;
|
static String currentId;
|
||||||
|
|
||||||
const AudioPlayer(this.event, {this.color = Colors.black, Key key})
|
const AudioPlayerWidget(this.event, {this.color = Colors.black, Key key})
|
||||||
: super(key: key);
|
: super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -29,18 +31,21 @@ class AudioPlayer extends StatefulWidget {
|
|||||||
|
|
||||||
enum AudioPlayerStatus { notDownloaded, downloading, downloaded }
|
enum AudioPlayerStatus { notDownloaded, downloading, downloaded }
|
||||||
|
|
||||||
class _AudioPlayerState extends State<AudioPlayer> {
|
class _AudioPlayerState extends State<AudioPlayerWidget> {
|
||||||
AudioPlayerStatus status = AudioPlayerStatus.notDownloaded;
|
AudioPlayerStatus status = AudioPlayerStatus.notDownloaded;
|
||||||
|
final AudioPlayer audioPlayer = AudioPlayer();
|
||||||
|
|
||||||
final FlutterSoundPlayer flutterSound = FlutterSoundPlayer();
|
StreamSubscription onAudioPositionChanged;
|
||||||
|
StreamSubscription onDurationChanged;
|
||||||
StreamSubscription soundSubscription;
|
StreamSubscription onPlayerStateChanged;
|
||||||
Uint8List audioFile;
|
StreamSubscription onPlayerError;
|
||||||
|
|
||||||
String statusText = '00:00';
|
String statusText = '00:00';
|
||||||
double currentPosition = 0;
|
double currentPosition = 0;
|
||||||
double maxPosition = 0;
|
double maxPosition = 0;
|
||||||
|
|
||||||
|
File audioFile;
|
||||||
|
|
||||||
String webSrcUrl;
|
String webSrcUrl;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -59,13 +64,14 @@ class _AudioPlayerState extends State<AudioPlayer> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
if (flutterSound.isPlaying) {
|
if (audioPlayer.state == AudioPlayerState.PLAYING) {
|
||||||
flutterSound.stopPlayer();
|
audioPlayer.stop();
|
||||||
}
|
}
|
||||||
if (flutterSound.isOpen()) {
|
onAudioPositionChanged?.cancel();
|
||||||
flutterSound.closeAudioSession();
|
onDurationChanged?.cancel();
|
||||||
}
|
onPlayerStateChanged?.cancel();
|
||||||
soundSubscription?.cancel();
|
onPlayerError?.cancel();
|
||||||
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,8 +81,15 @@ class _AudioPlayerState extends State<AudioPlayer> {
|
|||||||
try {
|
try {
|
||||||
final matrixFile =
|
final matrixFile =
|
||||||
await widget.event.downloadAndDecryptAttachmentCached();
|
await widget.event.downloadAndDecryptAttachmentCached();
|
||||||
|
final tempDir = await getTemporaryDirectory();
|
||||||
|
final fileName = matrixFile.name.contains('.')
|
||||||
|
? matrixFile.name
|
||||||
|
: '${matrixFile.name}.mp3';
|
||||||
|
final file = File('${tempDir.path}/$fileName');
|
||||||
|
await file.writeAsBytes(matrixFile.bytes);
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
audioFile = matrixFile.bytes;
|
audioFile = file;
|
||||||
status = AudioPlayerStatus.downloaded;
|
status = AudioPlayerStatus.downloaded;
|
||||||
});
|
});
|
||||||
_playAction();
|
_playAction();
|
||||||
@ -91,50 +104,42 @@ class _AudioPlayerState extends State<AudioPlayer> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _playAction() async {
|
void _playAction() async {
|
||||||
if (AudioPlayer.currentId != widget.event.eventId) {
|
if (AudioPlayerWidget.currentId != widget.event.eventId) {
|
||||||
if (AudioPlayer.currentId != null) {
|
if (AudioPlayerWidget.currentId != null) {
|
||||||
if (!flutterSound.isStopped) {
|
if (audioPlayer.state != AudioPlayerState.STOPPED) {
|
||||||
await flutterSound.stopPlayer();
|
await audioPlayer.stop();
|
||||||
setState(() => null);
|
setState(() => null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AudioPlayer.currentId = widget.event.eventId;
|
AudioPlayerWidget.currentId = widget.event.eventId;
|
||||||
}
|
}
|
||||||
switch (flutterSound.playerState) {
|
switch (audioPlayer.state) {
|
||||||
case PlayerState.isPlaying:
|
case AudioPlayerState.PLAYING:
|
||||||
await flutterSound.pausePlayer();
|
await audioPlayer.pause();
|
||||||
break;
|
break;
|
||||||
case PlayerState.isPaused:
|
case AudioPlayerState.PAUSED:
|
||||||
await flutterSound.resumePlayer();
|
await audioPlayer.resume();
|
||||||
break;
|
break;
|
||||||
case PlayerState.isStopped:
|
case AudioPlayerState.STOPPED:
|
||||||
default:
|
default:
|
||||||
if (!flutterSound.isOpen()) {
|
onAudioPositionChanged ??=
|
||||||
await flutterSound.openAudioSession(
|
audioPlayer.onAudioPositionChanged.listen((state) {
|
||||||
focus: AudioFocus.requestFocusAndStopOthers,
|
setState(() => currentPosition = state.inMilliseconds.toDouble());
|
||||||
category: SessionCategory.playback);
|
|
||||||
}
|
|
||||||
|
|
||||||
await flutterSound.setSubscriptionDuration(Duration(milliseconds: 100));
|
|
||||||
await flutterSound.startPlayer(fromDataBuffer: audioFile);
|
|
||||||
soundSubscription ??= flutterSound.onProgress.listen((e) {
|
|
||||||
if (AudioPlayer.currentId != widget.event.eventId) {
|
|
||||||
soundSubscription?.cancel()?.then((f) => soundSubscription = null);
|
|
||||||
setState(() {
|
|
||||||
currentPosition = 0;
|
|
||||||
statusText = '00:00';
|
|
||||||
});
|
|
||||||
AudioPlayer.currentId = null;
|
|
||||||
} else if (e != null) {
|
|
||||||
final txt =
|
|
||||||
'${e.position.inMinutes.toString().padLeft(2, '0')}:${(e.position.inSeconds % 60).toString().padLeft(2, '0')}';
|
|
||||||
setState(() {
|
|
||||||
maxPosition = e.duration.inMilliseconds.toDouble();
|
|
||||||
currentPosition = e.position.inMilliseconds.toDouble();
|
|
||||||
statusText = txt;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
onDurationChanged ??= audioPlayer.onDurationChanged.listen((max) =>
|
||||||
|
setState(() => maxPosition = max.inMilliseconds.toDouble()));
|
||||||
|
onPlayerStateChanged ??= audioPlayer.onPlayerStateChanged
|
||||||
|
.listen((_) => setState(() => null));
|
||||||
|
onPlayerError ??= audioPlayer.onPlayerError.listen((e) {
|
||||||
|
AdaptivePageLayout.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(L10n.of(context).oopsSomethingWentWrong),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
SentryController.captureException(e, StackTrace.current);
|
||||||
|
});
|
||||||
|
|
||||||
|
await audioPlayer.play(audioFile.path);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -163,12 +168,12 @@ class _AudioPlayerState extends State<AudioPlayer> {
|
|||||||
? CircularProgressIndicator(strokeWidth: 2)
|
? CircularProgressIndicator(strokeWidth: 2)
|
||||||
: IconButton(
|
: IconButton(
|
||||||
icon: Icon(
|
icon: Icon(
|
||||||
flutterSound.isPlaying
|
audioPlayer.state == AudioPlayerState.PLAYING
|
||||||
? Icons.pause_outlined
|
? Icons.pause_outlined
|
||||||
: Icons.play_arrow_outlined,
|
: Icons.play_arrow_outlined,
|
||||||
color: widget.color,
|
color: widget.color,
|
||||||
),
|
),
|
||||||
tooltip: flutterSound.isPlaying
|
tooltip: audioPlayer.state == AudioPlayerState.PLAYING
|
||||||
? L10n.of(context).audioPlayerPause
|
? L10n.of(context).audioPlayerPause
|
||||||
: L10n.of(context).audioPlayerPlay,
|
: L10n.of(context).audioPlayerPlay,
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
@ -183,8 +188,8 @@ class _AudioPlayerState extends State<AudioPlayer> {
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: Slider(
|
child: Slider(
|
||||||
value: currentPosition,
|
value: currentPosition,
|
||||||
onChanged: (double position) => flutterSound
|
onChanged: (double position) =>
|
||||||
.seekToPlayer(Duration(milliseconds: position.toInt())),
|
audioPlayer.seek(Duration(milliseconds: position.toInt())),
|
||||||
max: status == AudioPlayerStatus.downloaded ? maxPosition : 0,
|
max: status == AudioPlayerStatus.downloaded ? maxPosition : 0,
|
||||||
min: 0,
|
min: 0,
|
||||||
),
|
),
|
||||||
|
@ -83,7 +83,7 @@ class MessageContent extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
return MessageDownloadContent(event, textColor);
|
return MessageDownloadContent(event, textColor);
|
||||||
case MessageTypes.Audio:
|
case MessageTypes.Audio:
|
||||||
return AudioPlayer(
|
return AudioPlayerWidget(
|
||||||
event,
|
event,
|
||||||
color: textColor,
|
color: textColor,
|
||||||
);
|
);
|
||||||
|
42
pubspec.lock
42
pubspec.lock
@ -71,6 +71,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.5.0"
|
version: "2.5.0"
|
||||||
|
audioplayers:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: audioplayers
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.18.3"
|
||||||
base58check:
|
base58check:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -407,34 +414,6 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.6.0"
|
version: "0.6.0"
|
||||||
flutter_sound_lite:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: flutter_sound_lite
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "8.1.1"
|
|
||||||
flutter_sound_platform_interface:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: flutter_sound_platform_interface
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "8.1.1"
|
|
||||||
flutter_sound_web:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: flutter_sound_web
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "8.1.1"
|
|
||||||
flutter_spinkit:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: flutter_spinkit
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "5.0.0"
|
|
||||||
flutter_svg:
|
flutter_svg:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -867,13 +846,6 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.0"
|
version: "2.1.0"
|
||||||
recase:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: recase
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "4.0.0"
|
|
||||||
receive_sharing_intent:
|
receive_sharing_intent:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
@ -11,6 +11,7 @@ dependencies:
|
|||||||
adaptive_page_layout: ^0.2.3
|
adaptive_page_layout: ^0.2.3
|
||||||
adaptive_theme: ^2.2.0
|
adaptive_theme: ^2.2.0
|
||||||
android_path_provider: ^0.2.1
|
android_path_provider: ^0.2.1
|
||||||
|
audioplayers: ^0.18.3
|
||||||
cached_network_image: ^3.0.0
|
cached_network_image: ^3.0.0
|
||||||
cupertino_icons: any
|
cupertino_icons: any
|
||||||
# desktop_notifications: ^0.0.0-dev.4 // Currently blocked by: https://github.com/canonical/desktop_notifications.dart/issues/5
|
# desktop_notifications: ^0.0.0-dev.4 // Currently blocked by: https://github.com/canonical/desktop_notifications.dart/issues/5
|
||||||
@ -39,7 +40,6 @@ dependencies:
|
|||||||
flutter_screen_lock: ^4.0.3
|
flutter_screen_lock: ^4.0.3
|
||||||
flutter_secure_storage: ^4.1.0
|
flutter_secure_storage: ^4.1.0
|
||||||
flutter_slidable: ^0.6.0
|
flutter_slidable: ^0.6.0
|
||||||
flutter_sound_lite: ^8.1.1
|
|
||||||
flutter_svg: ^0.21.0+1
|
flutter_svg: ^0.21.0+1
|
||||||
flutter_typeahead: ^3.1.1
|
flutter_typeahead: ^3.1.1
|
||||||
future_loading_dialog: ^0.1.2
|
future_loading_dialog: ^0.1.2
|
||||||
|
Loading…
Reference in New Issue
Block a user