wp8-如何将下载的文件存入隔离存储

时间:2014-05-31 17:06:48

标签: c# windows-phone-8 isolatedstorage

您好我的应用程序中我下载" .mp3"将文件存入隔离存储,用户应该能够听到这个" .mp3"文件,但似乎我无法达到" .mp3"文件播放点击事件

这是我的代码

 private IsolatedStorageFile isoStore;
        public mp3kuran()
        {
            InitializeComponent();
            using ( isoStore= IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!isoStore.DirectoryExists("/shared/transfers"))
                {
                    isoStore.CreateDirectory("/shared/transfers");
                }
            }
        }

        string link= "https://dl.dropboxusercontent.com/u/75638865/001.mp3";

        private BackgroundTransferRequest transferRequest;

这是我的下载按钮动作,它下载了mp3文件

        private void download_Click(object sender, RoutedEventArgs e)
        {

            Uri transferuri = new Uri(Uri.EscapeUriString(link), UriKind.RelativeOrAbsolute);
             // Create the new transfer request, passing in the URI of the file to 
            // be transferred.
            transferRequest = new BackgroundTransferRequest(transferuri);
            // Set the transfer method. GET and POST are supported.
            transferRequest.Method = "GET";
            string downloadFile = link.Substring(link.LastIndexOf("/") + 1);
            Uri downloadUri = new Uri("shared/transfers/" + downloadFile, UriKind.RelativeOrAbsolute);
            transferRequest.DownloadLocation = downloadUri;
            transferRequest.Tag = downloadFile;
            // Add the transfer request using the BackgroundTransferService. Do this in 
            // a try block in case an exception is thrown.
            try
            {
                BackgroundTransferService.Add(transferRequest);
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show("Unable to add background transfer request. " + ex.Message);
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to add background transfer request.");
            }

        }

此处播放按钮点击事件

        private void play_Click(object sender, RoutedEventArgs e)
        {
            string fileName = transferRequest.Tag;
            MessageBox.Show(fileName);

            using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isoStore.FileExists(fileName))
                {MessageBox.Show("here");
                    using (var isoStream = isoStore.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                    {
                        mediaSound.Stop();
                        mediaSound.SetSource(isoStream);

                        mediaSound.Position = System.TimeSpan.FromSeconds(0);
                        mediaSound.Volume = 20;
                        mediaSound.Play();
                    }
                }

            }
        }

在play_clic事件中我尝试从孤立存储中获取mp3,但我无法解决错误,因为当我点击按钮时,它什么都不做

1 个答案:

答案 0 :(得分:0)

一些想法......

您是否在允许点击播放按钮之前检查BackgroundTransferRequest已完成?

您是否确定已成功下载完整文件,方法是检查是否存在与原始文件大小相同的物理文件?您可以使用Windows Phone Toolkit之类的工具来检查此内容。

下载完成后,BackgroundTransferRequest上的Tag属性是否保持正确的值?

通常,您要检查BackgroundTransferRequest的状态,并将文件从“/ shared / transfers”复制到您自己的位置。然后,您将从该位置播放该文件。