|
@@ -24,13 +24,13 @@ class RecordController extends BaseController {
|
|
|
FrameAnimationController(autoPlay: false);
|
|
FrameAnimationController(autoPlay: false);
|
|
|
final Rx<RecordStatus> currentStatus = RecordStatus.pending.obs;
|
|
final Rx<RecordStatus> currentStatus = RecordStatus.pending.obs;
|
|
|
final RxDouble currentDuration = 0.0.obs;
|
|
final RxDouble currentDuration = 0.0.obs;
|
|
|
- final AudioRecorder record = AudioRecorder();
|
|
|
|
|
- final RecordConfig recordConfig = const RecordConfig(
|
|
|
|
|
|
|
+ final AudioRecorder _record = AudioRecorder();
|
|
|
|
|
+ final RecordConfig _recordConfig = const RecordConfig(
|
|
|
encoder: AudioEncoder.pcm16bits,
|
|
encoder: AudioEncoder.pcm16bits,
|
|
|
bitRate: 128000,
|
|
bitRate: 128000,
|
|
|
sampleRate: 44100,
|
|
sampleRate: 44100,
|
|
|
numChannels: 2);
|
|
numChannels: 2);
|
|
|
- late final String lastRecordId;
|
|
|
|
|
|
|
+ late final String _lastRecordId;
|
|
|
|
|
|
|
|
@override
|
|
@override
|
|
|
void onInit() {
|
|
void onInit() {
|
|
@@ -39,13 +39,19 @@ class RecordController extends BaseController {
|
|
|
_initLastRecordStatus();
|
|
_initLastRecordStatus();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @override
|
|
|
|
|
+ void onClose() {
|
|
|
|
|
+ super.onClose();
|
|
|
|
|
+ _record.dispose();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
void _initLastRecordId() {
|
|
void _initLastRecordId() {
|
|
|
String? lastRecordId = KVUtil.getString(keyLastRecordId, null);
|
|
String? lastRecordId = KVUtil.getString(keyLastRecordId, null);
|
|
|
if (lastRecordId == null || lastRecordId.isEmpty) {
|
|
if (lastRecordId == null || lastRecordId.isEmpty) {
|
|
|
- this.lastRecordId = const Uuid().v4();
|
|
|
|
|
- KVUtil.putString(keyLastRecordId, this.lastRecordId);
|
|
|
|
|
|
|
+ _lastRecordId = const Uuid().v4();
|
|
|
|
|
+ KVUtil.putString(keyLastRecordId, _lastRecordId);
|
|
|
} else {
|
|
} else {
|
|
|
- this.lastRecordId = lastRecordId;
|
|
|
|
|
|
|
+ _lastRecordId = lastRecordId;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -55,7 +61,7 @@ class RecordController extends BaseController {
|
|
|
if (currentRecordFile.existsSync() && fileLength > 0) {
|
|
if (currentRecordFile.existsSync() && fileLength > 0) {
|
|
|
_changeRecordStatus(RecordStatus.paused);
|
|
_changeRecordStatus(RecordStatus.paused);
|
|
|
currentDuration.value = _getPcmDuration(
|
|
currentDuration.value = _getPcmDuration(
|
|
|
- fileLength, recordConfig.sampleRate, 16, recordConfig.numChannels);
|
|
|
|
|
|
|
+ fileLength, _recordConfig.sampleRate, 16, _recordConfig.numChannels);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -117,19 +123,19 @@ class RecordController extends BaseController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Future<void> _startOrContinueRecord() async {
|
|
Future<void> _startOrContinueRecord() async {
|
|
|
- bool hasPermission = await record.hasPermission();
|
|
|
|
|
|
|
+ bool hasPermission = await _record.hasPermission();
|
|
|
if (!hasPermission) {
|
|
if (!hasPermission) {
|
|
|
_onRecordPermissionDenied();
|
|
_onRecordPermissionDenied();
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
File targetFile = await _getCurrentRecordFile();
|
|
File targetFile = await _getCurrentRecordFile();
|
|
|
- Stream<Uint8List> recordStream = await record.startStream(recordConfig);
|
|
|
|
|
|
|
+ Stream<Uint8List> recordStream = await _record.startStream(_recordConfig);
|
|
|
_changeRecordStatus(RecordStatus.recording);
|
|
_changeRecordStatus(RecordStatus.recording);
|
|
|
recordStream.listen((data) async {
|
|
recordStream.listen((data) async {
|
|
|
targetFile.writeAsBytesSync(data, mode: FileMode.append);
|
|
targetFile.writeAsBytesSync(data, mode: FileMode.append);
|
|
|
currentDuration.value = currentDuration.value +
|
|
currentDuration.value = currentDuration.value +
|
|
|
- _getPcmDuration(data.length, recordConfig.sampleRate, 16,
|
|
|
|
|
- recordConfig.numChannels);
|
|
|
|
|
|
|
+ _getPcmDuration(data.length, _recordConfig.sampleRate, 16,
|
|
|
|
|
+ _recordConfig.numChannels);
|
|
|
}, onDone: () {
|
|
}, onDone: () {
|
|
|
_changeRecordStatus(RecordStatus.paused);
|
|
_changeRecordStatus(RecordStatus.paused);
|
|
|
}, onError: (error) {
|
|
}, onError: (error) {
|
|
@@ -140,12 +146,12 @@ class RecordController extends BaseController {
|
|
|
_onRecordPermissionDenied() {}
|
|
_onRecordPermissionDenied() {}
|
|
|
|
|
|
|
|
Future<void> _stopRecord() {
|
|
Future<void> _stopRecord() {
|
|
|
- return record.stop().then((_) => _changeRecordStatus(RecordStatus.paused));
|
|
|
|
|
|
|
+ return _record.pause().then((_) => _changeRecordStatus(RecordStatus.paused));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Future<File> _getCurrentRecordFile() async {
|
|
Future<File> _getCurrentRecordFile() async {
|
|
|
Directory documentDir = await getApplicationDocumentsDirectory();
|
|
Directory documentDir = await getApplicationDocumentsDirectory();
|
|
|
- File file = File("${documentDir.path}/.atmob/record/$lastRecordId");
|
|
|
|
|
|
|
+ File file = File("${documentDir.path}/.atmob/record/$_lastRecordId");
|
|
|
if (!file.existsSync()) {
|
|
if (!file.existsSync()) {
|
|
|
file.createSync(recursive: true);
|
|
file.createSync(recursive: true);
|
|
|
}
|
|
}
|
|
@@ -174,13 +180,13 @@ class RecordController extends BaseController {
|
|
|
Future<void> _saveCurrentRecord() async {
|
|
Future<void> _saveCurrentRecord() async {
|
|
|
await _stopRecord();
|
|
await _stopRecord();
|
|
|
talkRepository
|
|
talkRepository
|
|
|
- .talkCreate(lastRecordId, currentDuration.value.toInt())
|
|
|
|
|
|
|
+ .talkCreate(_lastRecordId, currentDuration.value.toInt())
|
|
|
.then((talkInfo) async {
|
|
.then((talkInfo) async {
|
|
|
File pcmFile = await _getCurrentRecordFile();
|
|
File pcmFile = await _getCurrentRecordFile();
|
|
|
if (pcmFile.existsSync()) {
|
|
if (pcmFile.existsSync()) {
|
|
|
File wavFile = await getRecordFile(talkInfo.id);
|
|
File wavFile = await getRecordFile(talkInfo.id);
|
|
|
- PcmWavConverter.convert(pcmFile, wavFile, recordConfig.sampleRate,
|
|
|
|
|
- recordConfig.numChannels, 16);
|
|
|
|
|
|
|
+ PcmWavConverter.convert(pcmFile, wavFile, _recordConfig.sampleRate,
|
|
|
|
|
+ _recordConfig.numChannels, 16);
|
|
|
pcmFile.delete();
|
|
pcmFile.delete();
|
|
|
Get.back();
|
|
Get.back();
|
|
|
TalkPage.start(talkInfo);
|
|
TalkPage.start(talkInfo);
|