Windows Phone 8 mp3 playing problems

2019-09-05 16:07发布

I am trying to play mp3 in my wp 8 app, i think forgot something, can you please help

my simplified code is as it goes:

in page.xaml.cs code is:

public void Play(object sender, RoutedEventArgs e)
        {


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

        }

in App.xaml.cs code is:

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);
                            }
                        }
                    }
                }
            }
        }

i can see that my BackgroundAudioPlayer.Instancestate never changes, but I can't figure out why (play function is trigered)

1条回答
放我归山
2楼-- · 2019-09-05 16:55

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();
查看更多
登录 后发表回答