在 c# 和 winrt 中将流保存到文件
- 作者: 联合国灭猫灭狗组织--行动部部长
- 来源: 51数据库
- 2023-02-13
问题描述
我在 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);
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源