1

音频文件保存到独立存储中,但单击时不会播放。我只是在这里显示我的代码。该代码实际上是从 xap 内容中获取音频文件并存储到隔离存储中。它如图所示回读,但没有按预期播放。

回读方法

using (var ISF = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (ISF.FileExists(MusicFileToPlay))
                {
                    using (var FS = ISF.OpenFile(MusicFileToPlay, FileMode.Open, FileAccess.Read))
                    {
                        mediaelement.Stop();
                        mediaelement.SetSource(FS);

                        mediaelement.Position = System.TimeSpan.FromSeconds(0);
                        mediaelement.Volume = 20;
                        mediaelement.Play();
                        StatusTextBlock.Text = "Playing....";
                        //MediaPlayer.Play(
                    }
                }
                else
                {
                    StatusTextBlock.Text = "No file present to play....";
                }
            }

存储到隔离存储的方法

private void SaveSoundsToStorage()
    {
        foreach (string music in fileNames)
        {
            string filename = String.Format("Sounds/{0}.mp3", music);
            SaveHelper(filename, music + ".mp3");
        }
    }

private void SaveHelper(string filename, string savename)
    {
        using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (appStorage.FileExists(savename))
            {
                //MessageBox.Show("File already exists");
                return;
            }
            StreamResourceInfo SRI = Application.GetResourceStream(new Uri(filename, UriKind.Relative));
            using (IsolatedStorageFileStream FS = new IsolatedStorageFileStream(savename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, appStorage))
            {
                try
                {
                    using (BinaryWriter BW = new BinaryWriter(FS))
                    {
                        long lg = 0;
                        Stream s;
                        try
                        {
                            if (SRI != null)
                            {
                                s = SRI.Stream;
                                lg = s.Length;
                            }
                        }
                        catch (Exception error)
                        {
                            MessageBox.Show("Stream resource exception:  "+error.Message);
                        }
                        try
                        {
                            if (lg > appStorage.AvailableFreeSpace)
                            {
                                //No space available.Request more space
                                Int64 spaceToAdd = lg;
                                Int64 curAvail = appStorage.AvailableFreeSpace;

                                // Request more quota space.
                                if (!appStorage.IncreaseQuotaTo(appStorage.Quota + spaceToAdd))
                                {
                                    // The user clicked NO to the
                                    // host's prompt to approve the quota increase.
                                    return;
                                }
                                else
                                {
                                    // The user clicked YES to the
                                    // host's prompt to approve the quota increase.
                                }
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("quota increase problem.");
                        }

                        byte[] buff = new byte[32];
                        int count = 0;
                        try
                        {
                            using (BinaryReader br = new BinaryReader(SRI.Stream))
                            {
                                while (count < lg)
                                {
                                    int actual = br.Read(buff, 0, buff.Length);
                                    count += actual;
                                    BW.Write(buff, 0, actual);
                                    BW.Flush();
                                }
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Error at binary reader.");
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Binarywriter exception.");
                }
                //MessageBox.Show(filename + " saved successfully.");
                StatusTextBlock.Text = savename + " saved succesfully";
            }
        }
    }

媒体元素 XAML 代码是

<MediaElement Name="mediaelement" Height="120" Width="160" AutoPlay="True" MediaFailed="mediaelement_MediaFailed_1"/>

提前致谢。

4

0 回答 0