嗨,我正在尝试通过带有卡盘编码的 http 发送 mp3 数据。这个想法是 mp3 稍后将成为实时流(mp3 片段),所以我不知道内容长度会是多少。
这是我使用浏览器访问时无法播放的代码....尽管它识别出头文件文件根本无法播放。
有人知道我哪里出错了吗?
Private Sub ServeAudioBlock(stream As NetworkStream)
Dim Content As Byte() = System.IO.File.ReadAllBytes("tone.mp3")
Dim sb As New System.Text.StringBuilder
sb.Append("HTTP/1.1 200 OK" + ControlChars.CrLf)
sb.Append("Content-Type: audio/mpeg" + ControlChars.CrLf)
sb.Append("Transfer-Encoding: chunked" + ControlChars.CrLf)
sb.Append(ControlChars.CrLf)
'send header
Dim Header() As Byte = Encoding.ASCII.GetBytes(sb.ToString)
stream.Write(Header, 0, Header.Length)
'send five times
For i As Integer = 1 To 5
'send length in hex
Dim SizeString As String = Hex(Content.Length)
Dim chunksize() As Byte = Encoding.ASCII.GetBytes(SizeString + ControlChars.CrLf)
stream.Write(chunksize, 0, chunksize.Length)
'send data for this chunk
stream.Write(Content, 0, Content.Length)
Next
Dim EmptyChunk As String = "0" + ControlChars.CrLf
Dim EmptyBytes() As Byte = Encoding.ASCII.GetBytes(EmptyChunk)
stream.Write(EmptyBytes, 0, EmptyBytes.Length)
Console.WriteLine("Served audio block")
stream.Close()
End Sub
我使用 HTTP 分析器捕获的响应的前几行:
HTTP/1.1 200 OK
Content-Type: audio/mpeg
Transfer-Encoding: chunked
11F4
ÿûPÄ<rest of file data follows>