2015-08-23 25 views
0

我想以编程方式设置我的手机的铃声。 通过一些搜索,我发现这个代码WP8.1 SaveRingtoneTask InvalidOperationException

private void RingToneSave() 
     { 
      SaveRingtoneTask ringtoneTask = new SaveRingtoneTask(); 
      ringtoneTask.Completed += saveRingtoneChooser_Completed; 
      ringtoneTask.Source = new Uri(@"C:\Data\Programs\{9519D660-4D38-497F-9584-6497FF78C693}\Install\Craig David.wma"); 
      ringtoneTask.DisplayName = "Ringtone"; 
      ringtoneTask.Show(); 
     } 

然而ringtoneTask.Show();抛出System.InvalidOperationException 以下是完整的详细的异常:

An exception of type 'System.InvalidOperationException' occurred in Microsoft.Phone.ni.dll but was not handled in user code 

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

然而,路径指向我的独立存储一个文件,因为我以前曾下载并将该文件保存到我的隔离存储中。 我还使用IsoStorySpy(一种检查手机的隔离存储的工具)来确保文件位于独立存储中。

我理解错了吗?有没有另一种方法来设置我的手机的铃声,而不使用SaveRingtoneTask

更新

private void RingToneSave(Uri sURI) 
     { 
      SaveRingtoneTask ringtoneTask = new SaveRingtoneTask(); 
      ringtoneTask.Completed += saveRingtoneChooser_Completed; 
      ringtoneTask.Source = sURI; 
      ringtoneTask.DisplayName = "Ringtone"; 
      ringtoneTask.Show(); 
     } 

    public async Task<Problem> DownloadFileFromWeb(Uri uriToDownload, string fileName, CancellationToken cToken) 
     { 
      try 
      { 
       using (Stream mystr = await DownloadFile(uriToDownload)) 
       using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        if (ISF.FileExists(fileName)) 
        { 
         ISF.DeleteFile(fileName); 
        } 
        using (IsolatedStorageFileStream file = ISF.CreateFile(fileName)) 
        { 
         const int BUFFER_SIZE = 8192; 
         byte[] buf = new byte[BUFFER_SIZE]; 

         int bytesread = 0; 

         while ((bytesread = await mystr.ReadAsync(buf, 0, BUFFER_SIZE)) > 0) 
         { 
          double percentage = ((double)file.Length/(double)mystr.Length) * (double)100; 
          textBlock.Text = Math.Round(percentage).ToString() + "%"; 
          cToken.ThrowIfCancellationRequested(); 
          file.Write(buf, 0, bytesread); 
         } 
         sRingTonePath = file.Name; 
        } 

       } 
       RingToneSave(new Uri(sRingTonePath)); 
       return Problem.Ok; 
      } 
      catch (Exception exc) 
      { 
       if (exc is OperationCanceledException) 
        return Problem.Cancelled; 
       else 
       { 
        MessageBox.Show(exc.Message); 
        return Problem.Other; 
       } 
      } 
     } 

回答

1

您正在使用的路径是无效的,你应该做的就是下载你想要的音乐文件在你的独立存储,然后指向路径作为源。

而且没有其他方法来设置铃声。

如果您只是需要一种方法来下载铃声使用app

Stream st = await new WebClient().OpenReadTaskAsync(Link); 

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       using (IsolatedStorageFileStream file = new IsolatedStorageFileStream(FileName, FileMode.OpenOrCreate, isf)) 
       { 
        using (var fs = new StreamWriter(file)) 
        { 
         byte[] bytesInStream = new byte[st.Length]; 
         st.Read(bytesInStream, 0, (int)bytesInStream.Length); 
         file.Write(bytesInStream, 0, bytesInStream.Length); 
         file.Flush(); 
        } 
       } 
      } 
     } 


SaveRingtoneTask task = new SaveRingtoneTask(); 
task.Source = new Uri(string.Format("isostore:/{0}"selected.FileName),UriKind.Absolute);      
task.Completed += task_Completed; 
task.Show(); 

一定要改变文件名

+0

我想是这样的sRingTonePath = Path.GetFullPath(ISF.GetFileNames()[0 ]);然后使用sRingTonePath作为源,但仍然是同样的问题。 – user2530266

+1

我以前做过这些,为了创建应用程序,我需要它,我遵循的步骤是 1)使用HttpClient将文件下载到Stream对象中 2)在隔离存储(Local Storage)中创建一个新文件)并将流保存在其中 3)开始一个新的RingToneTask并将Source属性设置为之前创建的文件。完成! –

+0

我正在做同样的事情。我更新了这个问题。你是这个意思吗? – user2530266