I have a radio live streaming link which I need to record when the user clicks the start button. Then once the user clicks stop I need to save the stream into a file.
I was able to successfully do this using java by reading the data as a byte array and writing a simple while loop stating as long as the data is not null write the byte into an array and when it is null then write the byte array to a file. Below I can provide a snippet of the code I have used in Java.
URL url = new URL(radioStream);
inputStreamRecording = url.openStream();
fileOutputStreamRecording = new FileOutputStream(OutputSourceStream + "/temp.mp3");
recordingBuffer = inputStreamRecording.read();
while (recordingBuffer > -1) {
fileOutputStreamRecording.write(recordingBuffer);
recordingBuffer = inputStreamRecording.read();
}
However, I am at a loss on how to do it in dart/flutter and cannot find any sort of example on this. So my question is how would I convert the Java code I have above into something I could use in Flutter. I am assuming I would use Future but how would you stop a future during the recording on a click event? Any help would be appreciated. Thank you in advance!
Stream.listen
methodonDone
at all, all you need isreadStream.listen(writeSink.add)
wherewriteSink
is taken fromFile.openWrite
methodvar out = File('out.dat').openWrite(); var ss = Stream.periodic(3.seconds, (i) => 'line $i\n'.codeUnits) .listen(out.add); Future.delayed(15.seconds, () => ss.cancel());
- hereStream.periodic
simpulates infinite stream of data andFuture.delayed
stops writing after 15 seconds