c# - wp8-如何将下载的文件访问到隔离存储中

标签 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,但我无法解决问题,因为当我单击按钮时,它什么都不做

最佳答案

一些想法...

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

通过检查是否存在与原始文件大小相同的物理文件,您确定已成功下载完整文件吗?您可以使用类似 Windows Phone Toolkit 的工具检查这个。

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

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

关于c# - wp8-如何将下载的文件访问到隔离存储中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23973114/

相关文章:

c# - 如何将模式包含在 xml 中

c# - 为什么 System.Net.Http.Headers.HttpResponseHeaders 不可索引?

c# - LongListSelector:项目点击?

unit-testing - Window Phone App 的单元测试

c# - 不允许对 IsolatedStorageFileStream 进行操作。错误

c# - 检查用户是否属于 AD 组 .net core

c# - KeepScreenOnRequest.RequestActive 不工作

c# - 为什么是 RestSharp AddHeader ("Accept", "application/json"); = 到项目列表?

.net - 什么时候应该选择IsolatedStorage和AppData文件存储?

optimization - 清除 Windows Phone 7 应用程序的 isolatedstoragestore 的最快方法是什么?