I am trying to build an app that streams music to another app. The receiver side reads bytes, than saves them into temp file, and plays that temp file. But between parts of the song, there is a small delay (when one part starts, and the next one is getting saved). I already got the player in a background task. So I would like to try to write the stream into the file, while the player is reading said file. All of this is being done over working Bluetooth connection.
System.Threading.Tasks.Task.Run(() => { Java.IO.File temp = Java.IO.File.CreateTempFile("temp", "mp3"); Java.IO.FileInputStream fis = new Java.IO.FileInputStream(temp); temp.DeleteOnExit(); MediaPlayer player; while (true) { try { byte[] myReadBuffer = new byte[10000]; Java.IO.FileOutputStream fos = new Java.IO.FileOutputStream(temp); mmInStream.Read(myReadBuffer, 0, myReadBuffer.Length); fos.Write(myReadBuffer, 0, myReadBuffer.Length); player = new MediaPlayer(); player.SetDataSource(fis.FD); player.Prepare(); player.Start(); while (true) { if (!player.IsPlaying) { player.Release(); break; } } } catch (System.IO.IOException ex) { System.Diagnostics.Debug.WriteLine("Input stream was disconnected", ex); } } }).ConfigureAwait(false);
So the mmInStream is a System.IO.Stream. The second while loop checks if a chunk has been played, to release the resources.