Write continuous stream to file flutter/dart
var out = File('out.dat').openWrite();
var ss = Stream.periodic(3.seconds, (i) {
print('generating event #$i');
return 'line $i\n'.codeUnits;
})
.listen(out.add);
Future.delayed(15.seconds, () {
print('calling ss.cancel');
ss.cancel();
});
This answer is very close to what I was looking for in case anyone else is trying to find this answer. All credit goes to @pskink!
kva1992
Updated on December 25, 2022Comments
-
kva1992 over 1 year
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!
-
kva1992 over 3 years@pskink would this work for a streamable link? file.openRead is using local assets no? Also based on the method it looks like you need to stream it until it is completed which the user needs to be able to control. Meaning the users needs to stop the stream to get an end how would that work with this method? Could you give me some sort of example?
-
pskink over 3 yearssee
Stream.listen
method -
kva1992 over 3 years@pskink could you provide some sort of example on how to use the stream.listen method? not exactly sure how to implement a stop method? since ondone would never be called technically speaking.
-
pskink over 3 yearsYou don't need
onDone
at all, all you need isreadStream.listen(writeSink.add)
wherewriteSink
is taken fromFile.openWrite
method -
pskink over 3 yearsso the minimal code would be:
var 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 -
kva1992 over 3 years@pskink you my friend are a freaken genius !!! Thank you so much for your help. The advice you gave me above is exactly what I needed and I got it working to an extent. But I am pretty sure I can figure it out from this point!!! Once again thank you so much for helping and not just giving me the answer and making me work for it!
-
pskink over 3 yearswow what a time coincidence: you posted your comment 10 seconds after mine... ;-) i hope the code i posted with
Future.delayed
explains how to stop writing -
kva1992 over 3 years@pskink I actually took a different approach but for whatever reason, I am getting the stream overriding itself not sure why. mind if I message you directly?
-
kva1992 over 3 yearsLet us continue this discussion in chat.
-
-
vontdeux about 2 yearshi, can this solve my problem here? stackoverflow.com/questions/72007879/…