3

I am struggling with this problem of accessing the sound file (mp3) download in isolated storage to be used in Alarm ,

The problem mentioned before

I am getting this error:

BNS Error: The action request's sound uri is invalid

Please help me but remember I am using the sound file for Alarm Regarding the code it is the same as the link above.

This is download and save code of the sound file :

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
4

1 回答 1

2

问题是您试图将隔离存储中的文件设置为警报声,这是不允许的。只有 .xap 中打包的文件可以设置为报警声源:

评论

Sound URI 必须指向打包在应用程序的 .xap 文件中的文件。不支持独立存储。当闹钟启动时,声音会安静地播放,然后音量逐渐增大。无法修改此行为。

从:

Alarm.Sound 属性

但是,有一种方法可以将下载的歌曲用作 alam 的声音。在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);
}

然后,您可以将警报的 Sound 属性设置为:

alarm.Sound = new Uri("/file.mp3", UriKind.Relative);
于 2013-07-19T16:26:58.123 回答