Windows Phone 8 mp3播放问题

时间:2013-10-02 20:57:35

标签: silverlight windows-phone-8 windows-phone

我想在我的wp 8应用程序中播放mp3,我想忘记了一些事情,请你帮忙

我的简化代码就是这样:

page.xaml.cs中的

代码是:

public void Play(object sender, RoutedEventArgs e)
        {


                if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
                {
                    BackgroundAudioPlayer.Instance.Pause();
                }
                else
                {
                    BackgroundAudioPlayer.Instance.Play();
                }

        }
App.xaml.cs代码中的

是:

using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string[] files = new string[] { "song.mp3"};

                foreach (var _fileName in files)
                {
                    if (!storage.FileExists(_fileName))
                    {
                        string _filePath = "Sounds/" + _fileName;
                        StreamResourceInfo resource = Application.GetResourceStream(new Uri(_filePath, UriKind.Relative));

                        using (IsolatedStorageFileStream file = storage.CreateFile(_fileName))
                        {
                            int chunkSize = 4096;
                            byte[] bytes = new byte[chunkSize];
                            int byteCount;

                            while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0)
                            {
                                file.Write(bytes, 0, byteCount);
                            }
                        }
                    }
                }
            }
        }

我可以看到我的BackgroundAudioPlayer.Instance状态永远不会改变,但我无法弄清楚为什么(游戏功能被嘲弄)

1 个答案:

答案 0 :(得分:1)

你需要告诉BackgroundAudioPlayer要播放的曲目 类似的东西:

var track = new AudioTrack(
                           new Uri("/song.mp3", UriKind.Relative),
                           "song name",
                           "artist name",
                           "album name",
                           null); // no artwork
BackgroundAudioPlayer.Instance.Track = track;
BackgroundAudioPlayer.Instance.Play();