如何通过URI(WP8)访问IsolatedStorage中下载的声音?

时间:2013-07-15 19:47:53

标签: windows-phone-8 isolatedstorage

我正在努力解决在隔离存储中访问声音文件(mp3)下载的问题,以便在闹钟中使用,

问题提到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

1 个答案:

答案 0 :(得分:2)

问题是您正在尝试将隔离存储中的文件设置为警报声,而这是不允许的。只有.xap中打包的文件才能设置为警报的声源:

  

说明

     

Sound URI必须指向应用程序的.xap中打包的文件   文件。不支持隔离存储。警报启动时   声音安静地播放,然后音量逐渐增加。   无法修改此行为。

自:

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);
}

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

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