[Windows Phone 7 Q&A, Help & Troubleshooting] Aleksiev:
I'm trying to create simple mediaplayer and using MediaPlayer Class, but I have some problems. When I select song from the list, my buttons Next and Prev stopped working. I try some things but nothing works, what I have to do, to fix this problem. I don't know how to use EventHandler - MediaStateChanged, will see this in the code below. I want to add simple progress bar, but this class don't have current position event. I read some about DispatcherTimer, but ...

The code:

namespace MediaViewer
{

public partial class MainPage : PhoneApplicationPage
{

private App app;
int songIndexForDetails;

public MainPage()
{
InitializeComponent();

app = Application.Current as App;

List<CategoryDetailsViewModel> items = new List<CategoryDetailsViewModel>();

items.Add(app.MusicModel);

PivotControl.ItemsSource = items;

ProgressBar.Visibility = Visibility.Collapsed;

txtCurrentState.Text = MediaPlayer.State.ToString();
}

private void ItemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox list = (ListBox)sender;

int selectedCategoryIndex = list.SelectedIndex;
songIndexForDetails = list.SelectedIndex;

if (selectedCategoryIndex == -1)
return;

List<CategoryDetailsViewModel> model = PivotControl.ItemsSource as List<CategoryDetailsViewModel>;

if (model[PivotControl.SelectedIndex].IsAnyItemAvailable())
{
if (model[PivotControl.SelectedIndex].PrepareItemForPreview(selectedCategoryIndex))
{
if (model[PivotControl.SelectedIndex] is MusicCategoryModel)
{

MediaLibrary library = new MediaLibrary();
FrameworkDispatcher.Update();
MediaPlayer.Play(library.Songs[selectedCategoryIndex]);
ProgressBar.Visibility = Visibility.Visible;
txtCurrentState.Text = MediaPlayer.State.ToString();
}

}

else
MessageBox.Show("Warining!\n\nUnable to open selected file.\nMake sure that your device is disconnected from the computer.");
}

}

private void detailsButton_Click(object sender, RoutedEventArgs e)
{
if (songIndexForDetails == -1)
{
return;
}

NavigationService.Navigate(new Uri("/Pages/SongDetailsPage.xaml?SelectedItem=" + songIndexForDetails, UriKind.Relative));
}

private void pauseButton_Click(object sender, RoutedEventArgs e)
{
MediaPlayer.Pause();
ProgressBar.Visibility = Visibility.Collapsed;
txtCurrentState.Text = MediaPlayer.State.ToString();
}

private void stopButton_Click(object sender, RoutedEventArgs e)
{
MediaPlayer.Stop();
ProgressBar.Visibility = Visibility.Collapsed;
txtCurrentState.Text = MediaPlayer.State.ToString();
}

private void playButton_Click(object sender, RoutedEventArgs e)
{

MediaLibrary library = new MediaLibrary();
FrameworkDispatcher.Update();
if (MediaPlayer.State == MediaState.Paused)
{
MediaPlayer.Resume();
}
else
{
MediaPlayer.Play(library.Songs);
ProgressBar.Visibility = Visibility.Visible;
txtCurrentState.Text = MediaPlayer.State.ToString();
}
}

private void nextButton_Click(object sender, RoutedEventArgs e)
{

MediaPlayer.MoveNext();

}
private void previousButton_Click(object sender, RoutedEventArgs e)
{
MediaPlayer.MovePrevious();
}

public Song GetModelForIndex(int itemIndex)
{
if (itemIndex < Songs.Count)
return Songs[itemIndex];
else
return null;
}

private List<Song> Songs { get; set; }

}
}