Search code examples
c#uwpinputstream

How to implement IInputStream UWP


I should get an IInputStream to pass to the method: CreateFromStreamAsync, I have to specifically use this method because I use an AudioGraph (the uri that I pass to the method is a uri to listen to the radio)

"HLS" I guessed it from the following link

Code class StreamRadio:

 public class StreamRadio : IInputStream
 {
    public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
    {
        return AsyncInfo.Run<IBuffer, uint>(async (token, progress) =>
        {
            await Task.Delay(2000);
            var output = buffer.AsStream();
            var outputRadio = output.AsInputStream();
            IBuffer bufferRadio = new DataReader(outputRadio).ReadBuffer(64);
            return bufferRadio;
        });
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
 }

Code Method:

StreamRadio streamRadio = new StreamRadio();

var adaptiveMediaSourceResult = await AdaptiveMediaSource.CreateFromStreamAsync(streamRadio, new Uri(uriRadio), "HLS");
if (adaptiveMediaSourceResult.Status != AdaptiveMediaSourceCreationStatus.Success)
{
     return null;
}

Thanks in advance!


Solution

  • Solution:

     var random = RandomAccessStreamReference.CreateFromUri(new Uri(uriRadio));
     IRandomAccessStream stream = await random.OpenReadAsync();
     var adaptiveMediaSourceResult = await AdaptiveMediaSource.CreateFromStreamAsync(stream, new Uri(uriRadio), "HLS");