mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2025-02-17 14:30:40 +01:00
refactor: Upgrade to latest flutter_sound_lite
This commit is contained in:
parent
b6dca5b7a7
commit
2f7dece4c7
@ -6,8 +6,7 @@ import 'package:fluffychat/components/message_download_content.dart';
|
|||||||
import 'package:flushbar/flushbar_helper.dart';
|
import 'package:flushbar/flushbar_helper.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/flutter_sound.dart';
|
import 'package:flutter_sound_lite/flutter_sound.dart';
|
||||||
import 'package:intl/intl.dart';
|
|
||||||
import 'package:universal_html/prefer_universal/html.dart' as html;
|
import 'package:universal_html/prefer_universal/html.dart' as html;
|
||||||
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';
|
||||||
@ -31,7 +30,7 @@ enum AudioPlayerStatus { NOT_DOWNLOADED, DOWNLOADING, DOWNLOADED }
|
|||||||
class _AudioPlayerState extends State<AudioPlayer> {
|
class _AudioPlayerState extends State<AudioPlayer> {
|
||||||
AudioPlayerStatus status = AudioPlayerStatus.NOT_DOWNLOADED;
|
AudioPlayerStatus status = AudioPlayerStatus.NOT_DOWNLOADED;
|
||||||
|
|
||||||
FlutterSound flutterSound = FlutterSound();
|
final FlutterSoundPlayer flutterSound = FlutterSoundPlayer();
|
||||||
|
|
||||||
StreamSubscription soundSubscription;
|
StreamSubscription soundSubscription;
|
||||||
Uint8List audioFile;
|
Uint8List audioFile;
|
||||||
@ -58,9 +57,12 @@ class _AudioPlayerState extends State<AudioPlayer> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
if (flutterSound.audioState == t_AUDIO_STATE.IS_PLAYING) {
|
if (flutterSound.isPlaying) {
|
||||||
flutterSound.stopPlayer();
|
flutterSound.stopPlayer();
|
||||||
}
|
}
|
||||||
|
if (flutterSound.isOpen()) {
|
||||||
|
flutterSound.closeAudioSession();
|
||||||
|
}
|
||||||
soundSubscription?.cancel();
|
soundSubscription?.cancel();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
@ -86,28 +88,31 @@ class _AudioPlayerState extends State<AudioPlayer> {
|
|||||||
void _playAction() async {
|
void _playAction() async {
|
||||||
if (AudioPlayer.currentId != widget.event.eventId) {
|
if (AudioPlayer.currentId != widget.event.eventId) {
|
||||||
if (AudioPlayer.currentId != null) {
|
if (AudioPlayer.currentId != null) {
|
||||||
if (flutterSound.audioState != t_AUDIO_STATE.IS_STOPPED) {
|
if (!flutterSound.isStopped) {
|
||||||
await flutterSound.stopPlayer();
|
await flutterSound.stopPlayer();
|
||||||
setState(() => null);
|
setState(() => null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AudioPlayer.currentId = widget.event.eventId;
|
AudioPlayer.currentId = widget.event.eventId;
|
||||||
}
|
}
|
||||||
switch (flutterSound.audioState) {
|
switch (flutterSound.playerState) {
|
||||||
case t_AUDIO_STATE.IS_PLAYING:
|
case PlayerState.isPlaying:
|
||||||
await flutterSound.pausePlayer();
|
await flutterSound.pausePlayer();
|
||||||
break;
|
break;
|
||||||
case t_AUDIO_STATE.IS_PAUSED:
|
case PlayerState.isPaused:
|
||||||
await flutterSound.resumePlayer();
|
await flutterSound.resumePlayer();
|
||||||
break;
|
break;
|
||||||
case t_AUDIO_STATE.IS_RECORDING:
|
case PlayerState.isStopped:
|
||||||
break;
|
default:
|
||||||
case t_AUDIO_STATE.IS_STOPPED:
|
if (!flutterSound.isOpen()) {
|
||||||
await flutterSound.startPlayerFromBuffer(
|
await flutterSound.openAudioSession(
|
||||||
audioFile,
|
focus: AudioFocus.requestFocusAndStopOthers,
|
||||||
codec: t_CODEC.CODEC_AAC,
|
category: SessionCategory.playback);
|
||||||
);
|
}
|
||||||
soundSubscription ??= flutterSound.onPlayerStateChanged.listen((e) {
|
|
||||||
|
await flutterSound.setSubscriptionDuration(Duration(milliseconds: 100));
|
||||||
|
await flutterSound.startPlayer(fromDataBuffer: audioFile);
|
||||||
|
soundSubscription ??= flutterSound.onProgress.listen((e) {
|
||||||
if (AudioPlayer.currentId != widget.event.eventId) {
|
if (AudioPlayer.currentId != widget.event.eventId) {
|
||||||
soundSubscription?.cancel()?.then((f) => soundSubscription = null);
|
soundSubscription?.cancel()?.then((f) => soundSubscription = null);
|
||||||
setState(() {
|
setState(() {
|
||||||
@ -116,19 +121,13 @@ class _AudioPlayerState extends State<AudioPlayer> {
|
|||||||
});
|
});
|
||||||
AudioPlayer.currentId = null;
|
AudioPlayer.currentId = null;
|
||||||
} else if (e != null) {
|
} else if (e != null) {
|
||||||
var date =
|
var txt =
|
||||||
DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
|
'${e.position.inMinutes.toString().padLeft(2, '0')}:${(e.position.inSeconds % 60).toString().padLeft(2, '0')}';
|
||||||
var txt = DateFormat('mm:ss', 'en_US').format(date);
|
|
||||||
setState(() {
|
setState(() {
|
||||||
maxPosition = e.duration;
|
maxPosition = e.duration.inMilliseconds.toDouble();
|
||||||
currentPosition = e.currentPosition;
|
currentPosition = e.position.inMilliseconds.toDouble();
|
||||||
statusText = txt;
|
statusText = txt;
|
||||||
});
|
});
|
||||||
if (e.duration == e.currentPosition) {
|
|
||||||
soundSubscription
|
|
||||||
?.cancel()
|
|
||||||
?.then((f) => soundSubscription = null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
@ -158,9 +157,7 @@ class _AudioPlayerState extends State<AudioPlayer> {
|
|||||||
? CircularProgressIndicator(strokeWidth: 2)
|
? CircularProgressIndicator(strokeWidth: 2)
|
||||||
: IconButton(
|
: IconButton(
|
||||||
icon: Icon(
|
icon: Icon(
|
||||||
flutterSound.audioState == t_AUDIO_STATE.IS_PLAYING
|
flutterSound.isPlaying ? Icons.pause : Icons.play_arrow,
|
||||||
? Icons.pause
|
|
||||||
: Icons.play_arrow,
|
|
||||||
color: widget.color,
|
color: widget.color,
|
||||||
),
|
),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
@ -175,8 +172,8 @@ class _AudioPlayerState extends State<AudioPlayer> {
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: Slider(
|
child: Slider(
|
||||||
value: currentPosition,
|
value: currentPosition,
|
||||||
onChanged: (double position) =>
|
onChanged: (double position) => flutterSound
|
||||||
flutterSound.seekToPlayer(position.toInt()),
|
.seekToPlayer(Duration(milliseconds: position.toInt())),
|
||||||
max: status == AudioPlayerStatus.DOWNLOADED ? maxPosition : 0,
|
max: status == AudioPlayerStatus.DOWNLOADED ? maxPosition : 0,
|
||||||
min: 0,
|
min: 0,
|
||||||
),
|
),
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
import 'package:flutter_sound/flutter_sound.dart';
|
import 'package:flutter_sound_lite/flutter_sound.dart';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
class RecordingDialog extends StatefulWidget {
|
class RecordingDialog extends StatefulWidget {
|
||||||
final Function onFinished;
|
|
||||||
final L10n l10n;
|
final L10n l10n;
|
||||||
|
|
||||||
const RecordingDialog({
|
const RecordingDialog({
|
||||||
this.onFinished,
|
|
||||||
@required this.l10n,
|
@required this.l10n,
|
||||||
Key key,
|
Key key,
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
@ -20,22 +20,38 @@ class RecordingDialog extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _RecordingDialogState extends State<RecordingDialog> {
|
class _RecordingDialogState extends State<RecordingDialog> {
|
||||||
FlutterSound flutterSound = FlutterSound();
|
final FlutterSoundRecorder flutterSound = FlutterSoundRecorder();
|
||||||
String time = '00:00:00';
|
String time = '00:00:00';
|
||||||
|
|
||||||
StreamSubscription _recorderSubscription;
|
StreamSubscription _recorderSubscription;
|
||||||
|
|
||||||
bool error = false;
|
bool error = false;
|
||||||
|
String _recordedPath;
|
||||||
|
double _decibels = 0;
|
||||||
|
|
||||||
void startRecording() async {
|
void startRecording() async {
|
||||||
try {
|
try {
|
||||||
await flutterSound.startRecorder(
|
await flutterSound.openAudioSession();
|
||||||
codec: t_CODEC.CODEC_AAC,
|
await flutterSound.setSubscriptionDuration(Duration(milliseconds: 100));
|
||||||
);
|
|
||||||
_recorderSubscription = flutterSound.onRecorderStateChanged.listen((e) {
|
final codec = Codec.aacADTS;
|
||||||
var date =
|
final tempDir = await getTemporaryDirectory();
|
||||||
DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
|
_recordedPath = '${tempDir.path}/recording${ext[codec.index]}';
|
||||||
setState(() => time = DateFormat('mm:ss:SS', 'en_US').format(date));
|
|
||||||
|
// delete any existing file
|
||||||
|
var outputFile = File(_recordedPath);
|
||||||
|
if (outputFile.existsSync()) {
|
||||||
|
await outputFile.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
await flutterSound.startRecorder(codec: codec, toFile: _recordedPath);
|
||||||
|
|
||||||
|
_recorderSubscription = flutterSound.onProgress.listen((e) {
|
||||||
|
setState(() {
|
||||||
|
_decibels = e.decibels;
|
||||||
|
time =
|
||||||
|
'${e.duration.inMinutes.toString().padLeft(2, '0')}:${(e.duration.inSeconds % 60).toString().padLeft(2, '0')}';
|
||||||
|
});
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = true;
|
error = true;
|
||||||
@ -52,6 +68,7 @@ class _RecordingDialogState extends State<RecordingDialog> {
|
|||||||
void dispose() {
|
void dispose() {
|
||||||
if (flutterSound.isRecording) flutterSound.stopRecorder();
|
if (flutterSound.isRecording) flutterSound.stopRecorder();
|
||||||
_recorderSubscription?.cancel();
|
_recorderSubscription?.cancel();
|
||||||
|
flutterSound.closeAudioSession();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,12 +79,24 @@ class _RecordingDialogState extends State<RecordingDialog> {
|
|||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
const maxDecibalWidth = 64.0;
|
||||||
|
final decibalWidth = min(_decibels / 2, maxDecibalWidth).toDouble();
|
||||||
return AlertDialog(
|
return AlertDialog(
|
||||||
content: Row(
|
content: Row(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
CircleAvatar(
|
Container(
|
||||||
backgroundColor: Colors.red,
|
width: maxDecibalWidth,
|
||||||
radius: 8,
|
height: maxDecibalWidth,
|
||||||
|
alignment: Alignment.center,
|
||||||
|
child: AnimatedContainer(
|
||||||
|
duration: Duration(milliseconds: 50),
|
||||||
|
width: decibalWidth,
|
||||||
|
height: decibalWidth,
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.red,
|
||||||
|
borderRadius: BorderRadius.circular(decibalWidth),
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
SizedBox(width: 8),
|
SizedBox(width: 8),
|
||||||
Expanded(
|
Expanded(
|
||||||
@ -100,11 +129,8 @@ class _RecordingDialogState extends State<RecordingDialog> {
|
|||||||
),
|
),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
await _recorderSubscription?.cancel();
|
await _recorderSubscription?.cancel();
|
||||||
final result = await flutterSound.stopRecorder();
|
await flutterSound.stopRecorder();
|
||||||
if (widget.onFinished != null) {
|
Navigator.of(context).pop<String>(_recordedPath);
|
||||||
widget.onFinished(result);
|
|
||||||
}
|
|
||||||
Navigator.of(context).pop();
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
@ -31,6 +31,7 @@ import 'package:flutter/services.dart';
|
|||||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
import 'package:image_picker/image_picker.dart';
|
import 'package:image_picker/image_picker.dart';
|
||||||
import 'package:pedantic/pedantic.dart';
|
import 'package:pedantic/pedantic.dart';
|
||||||
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
import 'package:scroll_to_index/scroll_to_index.dart';
|
import 'package:scroll_to_index/scroll_to_index.dart';
|
||||||
import 'package:swipe_to_action/swipe_to_action.dart';
|
import 'package:swipe_to_action/swipe_to_action.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
@ -260,13 +261,16 @@ class _ChatState extends State<Chat> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void voiceMessageAction(BuildContext context) async {
|
void voiceMessageAction(BuildContext context) async {
|
||||||
String result;
|
if (await Permission.microphone.isGranted != true) {
|
||||||
await showDialog(
|
final status = await Permission.microphone.request();
|
||||||
context: context,
|
if (status != PermissionStatus.granted) return;
|
||||||
builder: (c) => RecordingDialog(
|
}
|
||||||
onFinished: (r) => result = r,
|
final result = await showDialog<String>(
|
||||||
l10n: L10n.of(context),
|
context: context,
|
||||||
));
|
builder: (c) => RecordingDialog(
|
||||||
|
l10n: L10n.of(context),
|
||||||
|
),
|
||||||
|
);
|
||||||
if (result == null) return;
|
if (result == null) return;
|
||||||
final audioFile = File(result);
|
final audioFile = File(result);
|
||||||
// as we already explicitly say send in the recording dialog,
|
// as we already explicitly say send in the recording dialog,
|
||||||
|
34
pubspec.lock
34
pubspec.lock
@ -426,13 +426,34 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.5.7"
|
version: "0.5.7"
|
||||||
flutter_sound:
|
flutter_sound_lite:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: flutter_sound
|
name: flutter_sound_lite
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.1"
|
version: "7.5.3+1"
|
||||||
|
flutter_sound_platform_interface:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: flutter_sound_platform_interface
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "7.5.3+1"
|
||||||
|
flutter_sound_web:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: flutter_sound_web
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "7.5.3+1"
|
||||||
|
flutter_spinkit:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: flutter_spinkit
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "4.1.2+1"
|
||||||
flutter_svg:
|
flutter_svg:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -879,6 +900,13 @@ 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: "3.0.1"
|
||||||
receive_sharing_intent:
|
receive_sharing_intent:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
@ -37,7 +37,7 @@ dependencies:
|
|||||||
universal_html: ^1.2.4
|
universal_html: ^1.2.4
|
||||||
receive_sharing_intent: ^1.4.2
|
receive_sharing_intent: ^1.4.2
|
||||||
flutter_slidable: ^0.5.7
|
flutter_slidable: ^0.5.7
|
||||||
flutter_sound: 2.1.1
|
flutter_sound_lite: ^7.5.3+1
|
||||||
open_file: ^3.0.3
|
open_file: ^3.0.3
|
||||||
mime_type: ^0.3.2
|
mime_type: ^0.3.2
|
||||||
flushbar: ^1.10.4
|
flushbar: ^1.10.4
|
||||||
|
Loading…
x
Reference in New Issue
Block a user