live audio stream over UDP
<p>As the title says, I'm trying to stream audio over UDP. I'm using a raspberry pi (v3) that has a microphone connected to it (using windows 10 IoT). I record to a stream and I use that stream to send the data over UDP.</p>

<pre><code>var socket = new DatagramSocket();
        var outputStream = (await socket.GetOutputStreamAsync(new Windows.Networking.HostName(target), targetPortNo)).AsStreamForWrite();

        byte[] buffer = new byte[1024];
        var streamForRead = _stream.AsStreamForRead();
        int bytesRead;
        while((bytesRead = await streamForRead.ReadAsync(buffer, 0, buffer.Length)) &gt; 0)
        {
            await outputStream.WriteAsync(buffer, 0, buffer.Length);
            await outputStream.FlushAsync();
        }
</code></pre>

<p>and at the receiving end, I place the data back into a stream that is used by MediaElement control.</p>

<pre><code>private IRandomAccessStream _stream = new MemoryStream().AsRandomAccessStream();
private async void MessageReceivedFromSocket(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
    var incomingStream = args.GetDataStream().AsStreamForRead();
    var writeStream = _stream.AsStreamForWrite();
    var buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = await incomingStream.ReadAsync(buffer, 0, buffer.Length)) &gt; 0)
    {
        await writeStream.WriteAsync(buffer, 0, buffer.Length);
        await writeStream.FlushAsync();
    }
}
</code></pre>

<p>and then setting the source on the mediaelement:</p>

<pre><code>mediaElement.SetSource(_stream, "audio/x-wav");
mediaElement.Play();
</code></pre>

<p>The problem I'm having now is that the audio won't play. The data transfers correctly, but mediaelement won't play it. 
After some testing with streaming audio not using UDP I've come to the conclusion that mediaelement will only play audio if the file is completely written to the stream. I have tested this with this code:</p>

<pre><code>var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
var file = await folder.GetFileAsync("WP_20160625_13_36_33_Pro.mp4");
var stream = (await file.OpenAsync(Windows.Storage.FileAccessMode.Read)).AsStreamForRead();
var writeStream = _stream.AsStreamForWrite();

var buffer = new byte[1];
int bytesRead;
while((bytesRead = await stream.ReadAsync(buffer,0,buffer.Length)) &gt; 0)
{
    await writeStream.WriteAsync(buffer, 0, buffer.Length);
    await writeStream.FlushAsync();
}
</code></pre>

<p>the file I used was 350Mb. So it took a while. At first I thought is was because of the async-await and that the call to play was only reached when the task would complete, but after debugging I've noticed it wasn't so. It's seems that mediaelement won't play as long as the stream is being "filled up". Is there any way to make it play while data is being added to the stream.</p>

<p>I don't have much experience with streams. So I don't know if what i'm doing is correct. Can you use streams for live data? To me it makes sense that you can use it. I always thought of streams like an asynchronous queue. data is being taken from the front while other data is being added to the back.</p>