簡體   English   中英

如何在Windows Phone 7上從Internet保存MP3文件?

[英]How to save MP3 file from the internet on Windows Phone 7?

我正在嘗試從互聯網下載並保存.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文件的最佳方法是什么?

我相信這篇文章能使您到達那里。 就像鮑勃提到的那樣,您必須使用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();
        }
    }
});

但是我會閱讀完整的文章以完全了解會發生什么。 希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM