Hello I am a beginner and I am trying to record audio and upload it to firebase but I am facing an error which probably says that file does not exists.
Currently Flutter Audio Recorder is reading audio data but when Audio Recorder is set to stop and the process of uploading begins then the error is generated. I have checked file existence using existsSync() method which returned false
Kindly help me out in this. Here is my code
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:avatar_glow/avatar_glow.dart';
import 'package:flutter_audio_recorder/flutter_audio_recorder.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:path_provider/path_provider.dart';
class HomeView extends StatefulWidget {
@override
_HomeViewState createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
bool _isRecording;
String _convertedText;
String _fileName;
String _filePath;
FlutterAudioRecorder _audioRecorder;
@override
void initState() {
super.initState();
_isRecording = false;
_fileName = "";
_convertedText = "Press the Record button to convert text";
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Urdu Speech to Text Converter"),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: AvatarGlow(
animate: _isRecording,
glowColor: Theme
.of(context)
.primaryColor,
endRadius: 75.0,
duration: const Duration(milliseconds: 2000),
repeatPauseDuration: const Duration(milliseconds: 100),
repeat: true,
child: FloatingActionButton(
onPressed: _onRecordButtonPressed,
child: Icon(_isRecording ? Icons.mic : Icons.mic_none),
),
),
body: SingleChildScrollView(
reverse: true,
child: Container(
padding: const EdgeInsets.fromLTRB(30.0, 30.0, 30.0, 150.0),
child: Text(_convertedText)
),
),
);
}
Future<void> _uploadFile() async {
FirebaseStorage firebaseStorage = FirebaseStorage.instance;
try {
await firebaseStorage
.ref()
.child(_fileName)
.putFile(File(_filePath));
} catch (error) {
print('Error occured while uplaoding to Firebase ${error.toString()}');
// Scaffold.of(context).showSnackBar(
// SnackBar(
// content: Text('Error occured while uplaoding'),
// ),
// );
}
}
Future<void> _onRecordButtonPressed() async {
if (_isRecording) {
_audioRecorder.stop();
_isRecording = false;
File(_filePath).existsSync() ? _uploadFile() : print("*************** File Doesn't Exists **************");
} else {
_isRecording = true;
await _startRecording();
}
setState(() {});
}
Future<void> _startRecording() async {
final bool hasRecordingPermission =
await FlutterAudioRecorder.hasPermissions;
if (hasRecordingPermission) {
Directory directory = await getApplicationDocumentsDirectory();
String filepath = directory.path +
'/' +
DateTime
.now()
.millisecondsSinceEpoch
.toString() +
'.wav';
_audioRecorder =
FlutterAudioRecorder(filepath, audioFormat: AudioFormat.WAV);
await _audioRecorder.initialized;
_audioRecorder.start();
_filePath = filepath;
_fileName = _filePath.substring(_filePath.lastIndexOf('/') + 1, _filePath.length);
_convertedText = _fileName;
setState(() {});
} else {
print("*************************** No Permissions *****************************");
}
}
}