Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

save stream to file in c# and winrt

I have in c# and winrt:

var stream = await speech.GetSpeakStreamAsync(SpeechText.Text, language);

stream is a Windows.Storage.Streams.IRandomAccessStream

So I am completly new to c# and winrt. How i save this stream containg a wav-file to a file? Thanks in advance, Basilius

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The IRandomAccessStream has a method called GetInputStreamAt http://msdn.microsoft.com/en-US/library/windows/apps/windows.storage.streams.irandomaccessstream

IInputStream inputStream = stream.GetInputStreamAt(0);

This gets you an IInputStream.

The IInputStream interface defines just one method, ReadAsync, which lets you read bytes into an IBuffer object. Windows.Storage.Stream also includes a DataReader class that you create based on an IInputStream object and then read numerous .NET objects from the stream as well as arrays of bytes. http://www.charlespetzold.com/blog/2011/11/080203.html , http://msdn.microsoft.com/library/windows/apps/BR208119

using (var stream = new InMemoryRandomAccessStream())
{
    // for example, render pdf page
    var pdfPage = document.GetPage((uint)i);
    await pdfPage.RenderToStreamAsync(stream);

    // then, write page to file
    using (var reader = new DataReader(stream))
    {
        await reader.LoadAsync((uint)stream.Size);
        var buffer = new byte[(int)stream.Size];
        reader.ReadBytes(buffer);
        await Windows.Storage.FileIO.WriteBytesAsync(file, buffer);
    }
}

now you have a buffer containing all the read bytes.

you can now save this buffer to a file http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html

var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("MyWav.wav", Windows.Storage.CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteBytesAsync(file, buffer);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...