C#:将音频文件从服务器流式传输到客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/719800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:23:48  来源:igfitidea点击:

C#: Streaming an Audio file from a Server to a Client

c#wpfaudiostreaming

提问by Andreas Grech

I am currently writing an application that will allow a user to install some form of an application (maybe a Windows Service) that will open a port on it's PC and given a particular destination on the hard disk, will then be able to stream mp3 files.

我目前正在编写一个应用程序,该应用程序将允许用户安装某种形式的应用程序(可能是 Windows 服务),该应用程序将在其 PC 上打开一个端口并在硬盘上指定特定目的地,然后将能够流式传输 mp3 文件.

I will then have another application that will connect to the server (being the user's pc) and be able to browse the hosted data by connecting to that PC (remotely ofcourse) given the port, and stream mp3 files from the server to the application

然后,我将拥有另一个应用程序,该应用程序将连接到服务器(即用户的 PC),并能够通过连接到该 PC(当然是远程)来浏览托管数据给定端口,并将mp3 文件从服务器流式传输到应用程序



I have found some tutorials online but most of them are about File Servers in C# and they download allow you to download a whole file. What I want is to stream an mp3 file so that it starts playing when a certain number of bytes are download (ie, whilst it is being buffered)

我在网上找到了一些教程,但其中大部分是关于 C# 中的文件服务器,它们下载允许您下载整个文件。我想要的是流式传输一个 mp3 文件,以便它在下载一定数量的字节时开始播放(即,当它被缓冲时)



How do I go about in accomplishing such a task? What I need to know specifically is how to write this application (that I will turn into a Windows Service later on) that will listen on a specified port a stream files, so that I can then access the files by something of the sort: http://<serverip>:65000/acdc/wholelottarosie.mp3and hopefully be able to stream that file in a WPF MediaPlayer.

我该如何完成这样的任务?我需要特别知道的是如何编写这个应用程序(稍后我将把它变成一个 Windows 服务),它将在指定的端口上侦听流文件,以便我可以通过某种方式访问​​文件:http://<serverip>:65000/acdc/wholelottarosie.mp3和希望能够在 WPF 中流式传输该文件MediaPlayer



[Update]

[更新]

I was following this tutorialabout building a file server and sending the file from the server to the client. Is what I have to do something of the sort?

我正在关注有关构建文件服务器并将文件从服务器发送到客户端的教程。我必须做的事情是这样的吗?

[Update]

[更新]

Currently reading this post: Play Audio from a Stream using C#and I think it looks very promising as to how I can play streamed files; but I still don't know how I can actually stream the files from the server.

目前正在阅读这篇文章:Play Audio from a Stream using C#,我认为关于如何播放流文件看起来很有希望;但我仍然不知道如何从服务器实际流式传输文件。

采纳答案by Erik Funkenbusch

There is no effective difference between streaming and downloading. They're the same thing. Any difference is purely semantic.

流式传输和下载之间没有有效区别。它们是同一回事。任何差异纯粹是语义上的。

If you wanted to, you could "download" an MP3 from any web server and start playing it while you were downloading it. It just requires that you buffer some of the data and start sending it to your decoding and playback routines right away.

如果您愿意,您可以从任何网络服务器“下载”MP3,并在下载时开始播放。它只需要您缓冲一些数据并立即开始将其发送到您的解码和播放例程。

Similarly, even so called "streaming" servers can be downloaded. You just have to save the bytes as they are being sent across the wire to a file.

类似地,甚至可以下载所谓的“流”服务器。您只需要保存字节,因为它们通过网络发送到文件。

"Streaming" applications are just apps that are not designed to save the files to disk.

“流媒体”应用程序只是不用于将文件保存到磁盘的应用程序。

EDIT:

编辑:

There is an exception. Two really:

有一个例外。两个真的:

First, if you are streaming "live" audio, such as radio or other types where you don't need 100% reliability, then they stream using UDP. This can still be saved if you want, but it's more packet oriented than stream oriented.

首先,如果您正在流式传输“实时”音频,例如广播或其他不需要 100% 可靠性的类型,那么它们将使用 UDP 进行流式传输。如果您愿意,这仍然可以保存,但它更面向数据包而不是面向流。

The second is when encryption is used, in which case you can still probably save the file, but it would be useless without the encryption algorithm and keys.

第二种是使用加密时,在这种情况下,您可能仍然可以保存文件,但是如果没有加密算法和密钥,它将毫无用处。

回答by Fred Chateau

This is simply not true.

这是不正确的。

The difference between a file download and an HTTP multimedia stream is the encoding header, which is set to chunked encoding for a stream. In addition, a file download has a Content-Length header, so the recipient system can know the file size in advance.

文件下载和 HTTP 多媒体流之间的区别在于编码头,它被设置为流的分块编码。另外,文件下载有一个Content-Length头,所以接收系统可以提前知道文件大小。

There is no Content-Length header with a multimedia stream, therefore there is no expected end point. Rather, just a continual series of chunks of data are received and processed, for as long as they continue to appear.

多媒体流没有 Content-Length 标头,因此没有预期的终点。相反,只要它们继续出现,就会接收和处理一系列连续的数据块。