1

私はC#でダウンロードマネージャーを作成しています。現在、URLを指定して、URLからファイルをセグメントでダウンロードするダウンロードコンポーネントを設計しています。

以下に示すように、現在、各チャンクに I/O ストリームを使用しています。

    MyChunks.InputStream = rs ; 
//rs is obtained by using GetResponse() from FtpWebRequest or HttpWebRequest
    MyChunks.OutputStream = fs ; //fs is FileStream for writing on local file
    chunksize = MyChunks.InputStream.Read(buffer, 0, bufferSize);
    MyChunks.OutputStream.Write(buffer, 0, (int)chunksize);

私が分析した他のメソッドについては、`WebClient.DownloadDataAsync` メソッドも使用できることがわかりました。
ただし、複数のスレッドでダウンロードされたデータのチャンクを利用してダウンロードを高速化することはできません。さらに、上記のコードは正常に動作しています。


私の質問は次のとおりです。

  1. チャンクをダウンロードする他の方法はありますか、または上記のコードで問題ありませんか?

  2. また、ダウンロードしたオーディオ (mp3)/ビデオ ファイルを再生したいのですが、同じことを行う方法を提案できますか?

4

1 に答える 1

2

With the posted code, you download only a part of the file, this is correct. Depending how large your buffer is, it's unlikely that the whole download fits in the buffer. So you'll have to repeat the code fragment from above until the end of stream is reached. That's how reading from a buffered stream works.

But by downloading a file in chunks, there is the common notion that the file is split in multiple parts and then these parts are downloaded seperately.

To implement that you'll have to use the HTTP Range header (see this question for a discussion) which allows you to download only a chunk of data and use multiple clients.

But the download wouldn't speed up even if you use multiple threads to download the chunks at the same time because your bandwidth is limiting download speed. Instead you'll have to manage the chunks offsets and redundant connections.

The mentioned method DownloadDataAsync is used for downloading the data in nonblocking way, you can look at this question for a discussion.

And now to the second part of your question: while it may work in certain scenarios to download your files in chunks during playback, this approach will fail on slower connections. Have you considered looking for a streaming solution?

Finally, this question points to NAudio, which even comes with an Mp3StreamingDemo that allows playback of streamed mp3 audio files.

于 2016-08-08T20:23:36.997 に答える