问题描述
我在 c# 和 winrt 中有:
I have in c# and winrt:
var stream = await speech.GetSpeakStreamAsync(SpeechText.Text, language);
stream 是一个 Windows.Storage.Streams.IRandomAccessStream
所以我对 c# 和 winrt 完全陌生.我如何将此包含 wav 文件的流保存到文件中?提前致谢,巴西利乌斯
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
推荐答案
IRandomAccessStream 有一个方法叫做 GetInputStreamAt http://msdn.microsoft.com/en-US/library/windows/apps/windows.storage.streams.irandomaccessstream
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);
这会为您提供一个 IInputStream.
This gets you an IInputStream.
IInputStream 接口只定义了一种方法 ReadAsync,它允许您将字节读入 IBuffer 对象.Windows.Storage.Stream 还包括您基于 IInputStream 对象创建的 DataReader 类,然后从流中读取大量 .NET 对象以及字节数组.http://www.51sjk.com/Upload/Articles/1/0/343/343139_20230213091621619.html , http://msdn.microsoft.com/library/windows/apps/BR208119
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.51sjk.com/Upload/Articles/1/0/343/343139_20230213091621619.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.
您现在可以将此缓冲区保存到文件 http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html
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);