下载mp3并保存为铃声

时间:2012-03-27 09:21:04

标签: c# visual-studio windows-phone-7 isolatedstorage ringtone

我想从手机上的链接(隔离存储)下载mp3文件,然后将其另存为铃声。

但我的代码无法正常工作......它给了我一个错误:

  System.InvalidOperationException: Path must point to a file in your Isolated Storage or Application Data directory.

我这样称呼函数:

   private void getRingtone_Click(object sender, EventArgs e)
    {
        ringtone = new Ringtone();
        ringtone.DownloadFile(GlobalVariables.Url, GlobalVariables.filename);
        //ringtone.SaveRingtone();

    }

Globalvariables Url就像:www.example.com/mp3/M/myfile.dmf.mp3(如果你需要测试,我可以给你我真正的网址)

,文件名如下:myfile.dmf.mp3

这是铃声类:

   WebClient _webClient; // Used for downloading mp3
    private bool _playSoundAfterDownload;
    MediaElement mediaSound;
    SaveRingtoneTask saveRingtoneChooser;


    public void DownloadFile(string uri, string filename)
    {
        _webClient = new WebClient();
        saveRingtoneChooser = new SaveRingtoneTask();
        saveRingtoneChooser.Completed += new EventHandler<TaskEventArgs>(saveRingtoneChooser_Completed);
        _webClient.OpenReadCompleted += (s1, e1) =>
        {
            if (e1.Error == null)
            {
                try
                {
                    string fileName = GlobalVariables.filename;
                    bool isSpaceAvailable = IsSpaceIsAvailable(e1.Result.Length);

                    if (isSpaceAvailable)
                    {
                        // Save mp3 to Isolated Storage
                        using (var isfs = new IsolatedStorageFileStream(fileName,
                                            FileMode.CreateNew,
                                            IsolatedStorageFile.GetUserStoreForApplication()))
                        {
                            long fileLen = e1.Result.Length;
                            byte[] b = new byte[fileLen];
                            e1.Result.Read(b, 0, b.Length);
                            isfs.Write(b, 0, b.Length);
                            isfs.Flush();
                        }

                        if (_playSoundAfterDownload)
                        {
                            _playSoundAfterDownload = false;
                            SaveRingtone();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Not enough to save space available to download mp3.");
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show(e1.Error.Message);
            }
        };
        SaveRingtone();
    }

    // Check to make sure there are enough space available on the phone
    // in order to save the image that we are downloading on to the phone
    private bool IsSpaceIsAvailable(long spaceReq)
    {
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {

            long spaceAvail = store.AvailableFreeSpace;
            if (spaceReq > spaceAvail)
            {
                return false;
            }
            return true;
        }
    }

我这样做了:http://blog.toetapz.com/2010/11/29/how-to-download-and-save-mp3-to-isolatedstorage/

其余部分是安全的铃声部分。当我将mp3直接添加到我的projekt并使用appdata:xyz.mp3代码部分时,这是有效的。

     private void SaveRingtone()
    {
        try
        {
            //saveRingtoneChooser.Source = new Uri("appdata:/myTone.wma");
            saveRingtoneChooser.Source = new Uri("isostore:/"+GlobalVariables.filename); 

            saveRingtoneChooser.DisplayName = "My custom ringtone";

            saveRingtoneChooser.Show();
        }
        catch (System.InvalidOperationException ex)
        {
            MessageBox.Show("An error occurred."); //Error appears here.
        }
    }
    void saveRingtoneChooser_Completed(object sender, TaskEventArgs e)
    {
        switch (e.TaskResult)
        {
            //Logic for when the ringtone was saved successfully
            case TaskResult.OK:
                MessageBox.Show("Ringtone saved.");
                break;

            //Logic for when the task was cancelled by the user
            case TaskResult.Cancel:
                MessageBox.Show("Save cancelled.");
                break;

            //Logic for when the ringtone could not be saved
            case TaskResult.None:
                MessageBox.Show("Ringtone could not be saved.");
                break;
        }
    }
}

我希望我的问题是可以理解的。感谢。

1 个答案:

答案 0 :(得分:1)

我的问题的解决方案是添加:

        Uri url = new Uri(GlobalVariables.Url, UriKind.Absolute);
        _webClient.OpenReadAsync(url);

之后:

      _webClient.OpenReadCompleted += (s1, e1) =>
      {.... };
相关问题