How to play a stream of AAC with NAudio and Drive API?

  • Thread starter Thread starter AdamDlt
  • Start date Start date
A

AdamDlt

Guest
Instead of download the whole uploaded audio from Google Drive (extension can be m4a) I would like to play as buffered content (eg on YouTube, Spotify, etc).

That's why I tried to write two functions that first download a part of stream and then play it with NAudio until it get to the end of content (or something interrupts).

First of all, I have no idea what would be the size of the buffer. In the other hand, when should I load the next chunk of stream (so how to get the remaining stream of currently playing chuck)?

Unfortunately, I can't get there because StreamMediaFoundationReader throws an exception: System.Runtime.InteropServices.COMException: 'The byte stream type of the specified URL is not supported. (Exception HRESULT value: 0xC00D36C4) '


public Stream GetStreamChunk(string fileId, RangeHeaderValue range)
{
var getrequest = driveService.Files.Get(fileId);
var stream = new MemoryStream();
getrequest.DownloadRange(stream, range);
return stream;
}

public void PlayStream(AudioObject file)
{
//size of chuck
long from = 0;
long to = file.size / 5;

RangeHeaderValue range = new RangeHeaderValue(from, to);
Stream stream = GetStreamChunk(file.Id, range);

while (stream.Length <= file.size - 1)
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
memoryStream.Position = 0;

using (WaveStream blockAlignedStream = new BlockAlignReductionStream(
WaveFormatConversionStream.CreatePcmStream(
new StreamMediaFoundationReader(memoryStream))))
{
using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
waveOut.Init(blockAlignedStream);
waveOut.Play();
while (waveOut.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(100);
}
}
}
}
stream = GetStreamChunk(file.Id, range);
}
}

Continue reading...
 
Back
Top