0

我正在尝试在我的 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状态从未改变,但我不知道为什么(播放功能被触发)

4

1 回答 1

1

You need to tell the BackgroundAudioPlayer which track to play.
Something like:

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();
于 2013-10-03T12:08:38.470 回答