如何在 Windows Phone 7 上从互联网保存 MP3 文件?

发布于 2025-01-05 06:52:00 字数 793 浏览 3 评论 0原文

我正在尝试从互联网下载并保存 .mp3 文件,但被来自外部链接的流卡住了:

private void saveSound()
    {
        IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
        using (var fs = new IsolatedStorageFileStream("123.mp3", FileMode.Create, iso))
        {
            //Here should be this Stream from the Internet...
            //Uri: "http://my-site.com/mega-popular-song.mp3"
            StreamResourceInfo rs = new StreamResourceInfo(stream, "audio/mpeg");
            int count = 0;
            byte[] buffer = new byte[4096];
            while (0 < (count = rs.Stream.Read(buffer, 0, buffer.Length)))
            {
                fs.Write(buffer, 0, count);
            }
            fs.Close();

        }
    }

这个流应该是什么样子?下载和保存 .mp3 文件的最佳方式是什么?

I'm trying to download and save an .mp3 file from the internet, but got stuck with stream from the external link:

private void saveSound()
    {
        IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
        using (var fs = new IsolatedStorageFileStream("123.mp3", FileMode.Create, iso))
        {
            //Here should be this Stream from the Internet...
            //Uri: "http://my-site.com/mega-popular-song.mp3"
            StreamResourceInfo rs = new StreamResourceInfo(stream, "audio/mpeg");
            int count = 0;
            byte[] buffer = new byte[4096];
            while (0 < (count = rs.Stream.Read(buffer, 0, buffer.Length)))
            {
                fs.Write(buffer, 0, count);
            }
            fs.Close();

        }
    }

What should this stream look like? What is the best way to download and save .mp3 files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2025-01-12 06:52:00

我确信这篇文章可以帮助您实现这一点。正如 Bob 提到的,您必须使用 WebClient。基本上,这就是发挥魔力的代码:

wc.OpenReadCompleted += ((s, args) =>
{
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (store.FileExists(fileName))
            store.DeleteFile(fileName);

        using (var fs = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
        {
            byte[] bytesInStream = new byte[args.Result.Length];
            args.Result.Read(bytesInStream, 0, (int)bytesInStream.Length);
            fs.Write(bytesInStream, 0, bytesInStream.Length);
            fs.Flush();
        }
    }
});

但我会阅读完整的文章以完全理解发生的情况。希望这有帮助!

I'm sure this article gets you there. Like Bob mentioned, you'll have to use a WebClient. Basically this is the code that does the magic:

wc.OpenReadCompleted += ((s, args) =>
{
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (store.FileExists(fileName))
            store.DeleteFile(fileName);

        using (var fs = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
        {
            byte[] bytesInStream = new byte[args.Result.Length];
            args.Result.Read(bytesInStream, 0, (int)bytesInStream.Length);
            fs.Write(bytesInStream, 0, bytesInStream.Length);
            fs.Flush();
        }
    }
});

But I would read the complete article to fully understand what happens. Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文