【问题标题】:Iterate through the enumeration and check the state of the MediaPlayer before advancing to the next song遍历枚举并检查 MediaPlayer 的状态,然后再前进到下一首歌曲
【发布时间】:2011-08-10 07:21:03
【问题描述】:

我需要播放我的 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);
       }

【问题讨论】:

    标签: windows-phone-7 media-player ienumerator


    【解决方案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);
    

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

    干杯, 阿尔。

    【讨论】:

    • 谢谢 ajm。事实上,当我说从这个页面导航时,我的意思是当用户离开我的应用程序或关闭屏幕时。我真正想要的是让我的歌曲列表像背景音频代理一样播放背景。
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多