2013-07-15 20 views
3

我与访问声音文件(MP3)下载独立存储中的这个问题挣扎在报警使用,如何通过URI(WP8)访问IsolatedStorage中下载的声音?

问题提到before

我收到此错误:

BNS错误:动作请求的声音uri无效

请帮助我,但请记住我正在使用闹钟的声音文件 关于代码,它与上面的链接相同。

这是下载并保存声音文件的代码:

Public Async Function DownloadFile(url As Uri) As Task(Of Stream) 

    wc = New WebClient() 
    AddHandler wc.OpenReadCompleted, AddressOf OpenReadCompleted 
    AddHandler wc.DownloadProgressChanged, AddressOf DownloadProgress 

    wc.OpenReadAsync(url) 
    Dim r As IO.Stream = Await tcs.Task 
    Return r 
End Function 


Private Sub OpenReadCompleted(sender As Object, e As OpenReadCompletedEventArgs) 
    If e.[Error] IsNot Nothing Then 
     tcs.TrySetException(e.[Error]) 
    ElseIf e.Cancelled Then 
     tcs.TrySetCanceled() 
    Else 
     tcs.TrySetResult(e.Result) 
     Dim file As IsolatedStorageFile 
     file = IsolatedStorageFile.GetUserStoreForApplication() 

     Using Stream As IsolatedStorageFileStream = New IsolatedStorageFileStream("Sound.mp3", System.IO.FileMode.Create, file) 

      Dim buffer As Byte() = New Byte(1023) {} 

      While (e.Result.Read(buffer, 0, buffer.Length) > 0) 
       Stream.Write(buffer, 0, buffer.Length) 

      End While 
     End Using 


    End If 


End Sub 

Private Sub DownloadProgress(sender As Object, e As DownloadProgressChangedEventArgs) 
    Proind.Value = e.ProgressPercentage/100 
    Proind.Text = e.ProgressPercentage.ToString & " %" & " (" & (e.BytesReceived \ 1000).ToString & "/" & (e.TotalBytesToReceive \ 1000).ToString & ") KB" 
End Sub 
+0

搜索:报警作为'alarm.Sound =新的URI( “/共享/传输/ custom.mp3”,UriKind.Relative);' – anderZubi

+0

试了一下,没有工作给了我同样的错误。 –

+0

演示如何下载并保存歌曲 – anderZubi

回答

2

的问题是,您要设置独立存储作为闹钟的声音文件,而这不允许的。报警的只有打包的.xap文件可以设置为声源:

Remarks

The Sound URI must point to a file packaged in the application’s .xap file. Isolated storage is not supported. When the alarm is launched, the sound is played quietly and then gradually increases in volume. There is no way to modify this behavior.

来源:

Alarm.Sound Property

但是,你可以使用下载的歌曲作为阿拉姆的声音的方式。在OpenReadCompleted方法中,不是将下载的文件保存在隔离存储器中,而是使用File.Create方法创建文件,并将数据存储在那里。然后,将有可能使用这个文件作为闹钟声音:

下面是C#代码,我想你会很容易转化为VB:

byte[] buffer = new byte[e.Result.Length]; 
e.Result.Read(buffer, 0, buffer.Length); 

using (var fs = File.Create("file.mp3")) 
{ 
    fs.Write(buffer, 0, buffer.Length); 
} 

然后,您可以设置声音属性

alarm.Sound = new Uri("/file.mp3", UriKind.Relative); 
+0

谢谢,但这种有限行为的目的是什么? –

+0

@alihaider我不知道。 – anderZubi

+0

@alihaider你是lukcy,还有一种方法可以使用下载的歌曲。见编辑的答案。它使用C#,但您可以轻松翻译它。 – anderZubi