如何访问WebClient.DownloadDataAsync获取的数据?

时间:2017-07-07 06:46:24

标签: .net vb.net audio-streaming

(这是this one的后续问题。)

我想异步读取无限的音频流,以便我可以对获得的数据进行JIT分析。使用WebClient对象的DownloadDataAsync方法,我可以轻松启动下载,请参阅以下Win表单项目:

Imports System.Net      'For WebClient

Public Class Form1
    Private WithEvents c As New WebClient()

    Protected Overrides Sub Finalize()
        c.Dispose()
        MyBase.Finalize()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim u As New Uri("http://flower.serverhostingcenter.com:8433/;")
        c.DownloadDataAsync(u)
    End Sub

    Private Sub c_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles c.DownloadProgressChanged
        Debug.Print(e.BytesReceived.ToString & " B received.")
    End Sub
End Class

,根据DownloadProgressChanged事件输出到即时窗口,流似乎正在流动:

2920 B received.
48180 B received.
56940 B received.
61320 B received.
87600 B received.
94900 B received.
160436 B received.
162060 B received.
227596 B received.
...

但是,我找不到一种方法来读取获得的数据。由于这是一个源源不断的流,DownloadDataCompleted永远不会发射(除了使用的事件之外,任何其他事件也不会发射)。

如何访问获取的数据?

1 个答案:

答案 0 :(得分:2)

OpenReadAsyncOpenReadCompleted事件结合使用。

Private Sub c_OpenReadCompleted(sender As Object, e As OpenReadCompletedEventArgs) Handles c.OpenReadCompleted
   Dim stream = e.Result
End Sub