chat voice message implementation

merge-requests/116/head
Aamir Muhammad 3 years ago
parent ddba673970
commit f60394cc38

@ -33,7 +33,6 @@ class ChatApiClient {
}, },
); );
if (!kReleaseMode) { if (!kReleaseMode) {
print("Status Code is ================" + response.statusCode.toString());
logger.i("res: " + response.body); logger.i("res: " + response.body);
} }
if (response.statusCode == 200) { if (response.statusCode == 200) {

@ -55,9 +55,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
Future<void> getUserAutoLoginToken() async { Future<void> getUserAutoLoginToken() async {
userLoginToken.UserAutoLoginModel userLoginResponse = await ChatApiClient().getUserLoginToken(); userLoginToken.UserAutoLoginModel userLoginResponse = await ChatApiClient().getUserLoginToken();
print("======================================= Chat Login Token Check =====================================");
logger.d(userLoginResponse.toJson());
print("======================================= Chat Login Token Check =====================================");
if (userLoginResponse.response != null) { if (userLoginResponse.response != null) {
AppState().setchatUserDetails = userLoginResponse; AppState().setchatUserDetails = userLoginResponse;
} else { } else {
@ -677,7 +674,9 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
String? getFileExtension(String fileName) { String? getFileExtension(String fileName) {
try { try {
print("Ext: " + "." + fileName.split('.').last); if (kDebugMode) {
print("ext: " + "." + fileName.split('.').last);
}
return "." + fileName.split('.').last; return "." + fileName.split('.').last;
} catch (e) { } catch (e) {
return null; return null;
@ -886,8 +885,12 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
} else { } else {
await deleteFile(userID); await deleteFile(userID);
Uint8List decodedBytes = base64Decode(encodedBytes); Uint8List decodedBytes = base64Decode(encodedBytes);
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); // 1 Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
late File imageFile = File("${appDocumentsDirectory.path}/$userID.jpg"); String dirPath = '${appDocumentsDirectory.path}/chat_images';
if (!await Directory(dirPath).exists()) {
await Directory(dirPath).create();
}
late File imageFile = File("$dirPath/$userID.jpg");
imageFile.writeAsBytesSync(decodedBytes); imageFile.writeAsBytesSync(decodedBytes);
return imageFile; return imageFile;
} }
@ -895,7 +898,8 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
Future deleteFile(String userID) async { Future deleteFile(String userID) async {
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
late File imageFile = File('${appDocumentsDirectory.path}/$userID.jpg'); String dirPath = '${appDocumentsDirectory.path}/chat_images';
late File imageFile = File('$dirPath/$userID.jpg');
if (await imageFile.exists()) { if (await imageFile.exists()) {
await imageFile.delete(); await imageFile.delete();
} }
@ -972,7 +976,11 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
Future<void> initAudio({required int receiverId}) async { Future<void> initAudio({required int receiverId}) async {
appDirectory = await getApplicationDocumentsDirectory(); appDirectory = await getApplicationDocumentsDirectory();
path = "${appDirectory.path}/${AppState().chatDetails!.response!.id}-$receiverID-${DateTime.now().microsecondsSinceEpoch}.aac"; String dirPath = '${appDirectory.path}/chat_audios';
if (!await Directory(dirPath).exists()) {
await Directory(dirPath).create();
}
path = "$dirPath/${AppState().chatDetails!.response!.id}-$receiverID-${DateTime.now().microsecondsSinceEpoch}.aac";
recorderController = RecorderController() recorderController = RecorderController()
..androidEncoder = AndroidEncoder.aac ..androidEncoder = AndroidEncoder.aac
..androidOutputFormat = AndroidOutputFormat.mpeg4 ..androidOutputFormat = AndroidOutputFormat.mpeg4
@ -996,7 +1004,9 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
startRecoding(); startRecoding();
} else { } else {
recorderController.reset(); recorderController.reset();
logger.d(recorderController.isRecording);
await recorderController.record(path); await recorderController.record(path);
logger.d(recorderController.isRecording);
_recodeDuration = 0; _recodeDuration = 0;
_startTimer(); _startTimer();
isRecoding = !isRecoding; isRecoding = !isRecoding;
@ -1013,27 +1023,16 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
}); });
} }
void _pauseTimer() {
_timer?.cancel();
_timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
_recodeDuration++;
// buildTimer();
notifyListeners();
});
}
Future<void> pauseRecoding() async { Future<void> pauseRecoding() async {
isPause = true; isPause = true;
isPlaying = true; isPlaying = true;
recorderController.pause(); recorderController.pause();
path = await recorderController.stop(false); path = await recorderController.stop(false);
print(path);
File file = File(path!); File file = File(path!);
file.readAsBytesSync(); file.readAsBytesSync();
await playerController.preparePlayer(file.path, 1.0); await playerController.preparePlayer(file.path, 1.0);
var tempDuration = _recodeDuration; // var tempDuration = _recodeDuration;
_recodeDuration = tempDuration; // _recodeDuration = tempDuration;
_pauseTimer();
_timer?.cancel(); _timer?.cancel();
notifyListeners(); notifyListeners();
} }
@ -1047,19 +1046,20 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
} }
Future<void> deleteRecoding() async { Future<void> deleteRecoding() async {
print(path);
_recodeDuration = 0; _recodeDuration = 0;
_timer?.cancel(); _timer?.cancel();
// path = await recorderController.stop(false); // path = await recorderController.stop(true);
recorderController.reset(); recorderController.reset();
print(path); print(path);
if (path != null && path!.isNotEmpty) { if (path != null && path!.isNotEmpty) {
File delFile = File(path!); File delFile = File(path!);
double fileSizeInKB = delFile.lengthSync() / 1024; double fileSizeInKB = delFile.lengthSync() / 1024;
double fileSizeInMB = fileSizeInKB / 1024; double fileSizeInMB = fileSizeInKB / 1024;
debugPrint("Deleted file size: ${delFile.lengthSync()}"); if (kDebugMode) {
debugPrint("Deleted file size in KB: " + fileSizeInKB.toString()); debugPrint("Deleted file size: ${delFile.lengthSync()}");
debugPrint("Deleted file size in MB: " + fileSizeInMB.toString()); debugPrint("Deleted file size in KB: " + fileSizeInKB.toString());
debugPrint("Deleted file size in MB: " + fileSizeInMB.toString());
}
if (await delFile.exists()) { if (await delFile.exists()) {
delFile.delete(); delFile.delete();
} }
@ -1095,12 +1095,13 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
} }
void sendVoiceMessage(BuildContext context, {required int targetUserId, required int userStatus, required String userEmail, required String targetUserName}) async { void sendVoiceMessage(BuildContext context, {required int targetUserId, required int userStatus, required String userEmail, required String targetUserName}) async {
recorderController.pause(); //recorderController.pause();
path = await recorderController.stop(false); path = await recorderController.stop(false);
print(path); if (kDebugMode) {
print(path);
}
File voiceFile = File(path!); File voiceFile = File(path!);
voiceFile.readAsBytesSync(); voiceFile.readAsBytesSync();
_pauseTimer();
_timer?.cancel(); _timer?.cancel();
isPause = false; isPause = false;
isPlaying = false; isPlaying = false;
@ -1110,17 +1111,16 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
logger.d(value); logger.d(value);
String? ext = getFileExtension(voiceFile.path); String? ext = getFileExtension(voiceFile.path);
Utils.hideLoading(context); Utils.hideLoading(context);
sendChatToServer( // sendChatToServer(
chatEventId: 2, // chatEventId: 2,
fileTypeId: getFileType(ext.toString()), // fileTypeId: getFileType(ext.toString()),
targetUserId: targetUserId, // targetUserId: targetUserId,
targetUserName: targetUserName, // targetUserName: targetUserName,
isAttachment: true, // isAttachment: true,
chatReplyId: null, // chatReplyId: null,
isReply: false, // isReply: false,
isImageLoaded: true, // isImageLoaded: true,
image: voiceFile.readAsBytesSync()); // image: voiceFile.readAsBytesSync());
notifyListeners(); notifyListeners();
} }
} }

@ -234,22 +234,22 @@ class _ChatDetailScreenState extends State<ChatDetailScreen> {
).paddingAll(10).onPress(() { ).paddingAll(10).onPress(() {
m.deleteRecoding(); m.deleteRecoding();
}), }),
if (m.isPause) // if (m.isPause)
const Icon( // const Icon(
Icons.mic, // Icons.mic,
size: 26, // size: 26,
color: MyColors.lightGreenColor, // color: MyColors.lightGreenColor,
).paddingOnly(right: 15).onPress(() { // ).paddingOnly(right: 15).onPress(() {
m.resumeRecoding(); // m.resumeRecoding();
}), // }),
if (!m.isPause) // if (!m.isPause)
const Icon( // const Icon(
Icons.pause_circle_outline, // Icons.pause_circle_outline,
size: 26, // size: 26,
color: MyColors.lightGreenColor, // color: MyColors.lightGreenColor,
).paddingOnly(right: 15).onPress(() { // ).paddingOnly(right: 15).onPress(() {
m.pauseRecoding(); // m.pauseRecoding();
}), // }),
SvgPicture.asset("assets/icons/chat/chat_send_icon.svg", height: 26, width: 26) SvgPicture.asset("assets/icons/chat/chat_send_icon.svg", height: 26, width: 26)
.onPress( .onPress(
() => m.sendVoiceMessage(context, () => m.sendVoiceMessage(context,

Loading…
Cancel
Save