Windows Phone 7中的媒体播放器显示下一首歌曲

时间:2011-08-02 03:53:44

标签: windows-phone-7 media-player

我想做类似于Windows Phone 7中的媒体播放器,其中显示下一首歌曲。我将采取什么方法来实现这一目标?

实施例: 上一首歌 上一首歌 当前的歌 下一首歌 下一首歌

我的代码目前是这样的:

        void MainPage_Loaded(object sender, RoutedEventArgs e)         
    {         
        List<string> songlist = new List<string>();         
        MediaLibrary library = new MediaLibrary();         
        mySongCollection = library.Songs;         
        MediaPlayer.ActiveSongChanged += new EventHandler<EventArgs>(MediaPlayer_ActiveSongChanged);         
        MediaPlayer.MediaStateChanged += new EventHandler<EventArgs>(MediaPlayer_MediaStateChanged);         

        UpdateCurrentSongInformation();         
    }         

    void UpdateCurrentSongInformation()         
    {         
        try         
        {
            MediaLibrary lb = new MediaLibrary();
            int i = MediaPlayer.Queue.ActiveSongIndex;

           textBlock1.Text= lb.Songs.ToString();

            txtAlbumName.Text = MediaPlayer.Queue.ActiveSong.Album.Name;         
            txtArtistName.Text = MediaPlayer.Queue.ActiveSong.Artist.Name;         
            txtSongName.Text = MediaPlayer.Queue.ActiveSong.Name;
            progressBar1.Maximum = MediaPlayer.Queue.ActiveSong.Duration.Minutes*60+MediaPlayer.Queue.ActiveSong.Duration.Seconds;
            double max = MediaPlayer.Queue.ActiveSong.Duration.Milliseconds;


            BitmapImage bmp = new BitmapImage();         
            bmp.SetSource(MediaPlayer.Queue.ActiveSong.Album.GetAlbumArt());

            imgAlbumCover.Source = bmp;         
        }         
        catch         
        {         
            imgAlbumCover.Source = null;         
        }         
    }

像丹尼斯建议的那样,我这样编码:

            MediaLibrary lb = new MediaLibrary();
            int i = MediaPlayer.Queue.ActiveSongIndex;
            NextSong.Text = lb.Songs[i+1].Name;

1 个答案:

答案 0 :(得分:2)

由于您有MediaPlayer.Queue.ActiveSongIndex,您还可以通过MediaPlayer.Queue[n]的索引访问队列中的现有项目,其中 n 是歌曲索引。您将获得具有相同元数据的Song实例。只要您拥有当前索引,就可以将其减1以检查上一首歌曲,并将其递增1以检查下一首歌曲。