Search code examples
windows-phone-7media-player

media player in windows phone 7 displaying next song


I want to do something similar to the media player in windows phone 7 where the next several song is shown. What approach I would take to accomplish this?

Example: previous song previous song CURRENT SONG next song next song

my code currently like this:

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

Like Dennis suggest, I code it like this:

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

Solution

  • Since you have MediaPlayer.Queue.ActiveSongIndex, you can also access existing items in the queue by their index through MediaPlayer.Queue[n] where n is the song index. You will get a Song instance with the same metadata. As long as you have the current index, you can decrement it by one to check for the previous song, and increment it by one to check for the next song.