我需要在IEnumerable collection中播放歌曲,但是这种方法有很多问题。如果我使用计时器来检查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);
}
如果我理解正确的话,您希望在离开页面后保留集合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);
添加到集合时可能会有一些线程问题,但这应该允许您将歌曲添加到播放列表并从页面导航。如果有帮助,请告诉我。
欢呼,Al .