1

我需要在我的 IEnumerable 集合中播放歌曲,但是这种方法存在很多问题。如果我使用计时器检查 MediaState,它可能会起作用,但是当我从该页面导航时,课程将被取消并且音乐将停止。我想这样做的原因是能够播放不同专辑中的歌曲:

我的代码:

    private SongCollection mySongCollection;
    IEnumerable<Song> ultimateCollection;

    mySongCollection = library.Albums[index].Songs;
    ultimateCollection = mySongCollection.Concat(library.Albums[1].Songs);

    foreach (Song a in ultimateCollection)
      {
      while (MediaPlayer.State == MediaState.Playing || MediaPlayer.State == MediaState.Paused)
                    {
                       //while MediaState still playing, dont play next song
                    }
                        MediaPlayer.Play(a);
       }
4

1 回答 1

1

ultimateCollection如果我理解正确,您希望在您离开页面后保留收藏。在您的示例中,它被销毁是有道理的,因为它是页面的字段变量。您想要做的是拥有一个静态播放列表,可以从您的应用程序的任何地方访问。

我建议转移ultimateCollection到 App.xaml

public IList<Song> UltimateCollection {get; private set;}

// and then somewhere else in App.xaml.cs where your player is looping through the songs
    int i=0;
    while(i<UltimateCollection.Count)
    {
        Song a = UltimateCollection[i];
        MediaPlayer.Play(a);
        while (MediaPlayer.State == MediaState.Playing || MediaPlayer.State == MediaState.Paused)
        {
            //while MediaState still playing, dont play next song
        }
    }

然后从您应用程序的其他地方,比如说另一个页面,您可以通过

App.UltimateCollection.Add(someSong);

添加到集合时可能会出现一些线程问题,但这应该允许您将歌曲添加到播放列表并离开页面。让我知道这是否有帮助。

干杯,艾尔。

于 2011-10-11T16:06:10.933 回答