J'ai fait mon propre lecteur de musique en C# WPF. Mais, je ne peux pas le faire jouer automatiquement le prochain fichier dans la liste de lecture quand une chanson a fini. J'ai un curseur pour montrer la progression du jeu, une case à cocher et les boutons suivants.Comment lire automatiquement le fichier suivant dans la liste de lecture lorsqu'un fichier est terminé?
Voici le code pour le bouton suivant:
private void btnNext_Click(object sender, RoutedEventArgs e)
{
if (listBox.SelectedIndex < listBox.Items.Count - 1)
{
listBox.SelectedIndex = listBox.SelectedIndex + 1;
TagLib.File tagFile = TagLib.File.Create((listBox.SelectedValue).ToString());
string album = tagFile.Tag.Album;
string artist = tagFile.Tag.FirstAlbumArtist;
string title = tagFile.Tag.Title;
uint year = tagFile.Tag.Year;
string genre = tagFile.Tag.FirstGenre;
lblName.Content = artist + " - " + title;
lblAlbum.Content = album;
lblArtist.Content = artist;
lblTitle.Content = title;
lblYear.Content = year;
lblGenre.Content = genre;
lblBit.Content = tagFile.Properties.AudioBitrate + " kbps";
lblTime2.Content = tagFile.Properties.Duration.ToString(@"mm\:ss");
mediaPlayer.Open(new Uri((listBox.SelectedValue).ToString()));
mediaPlayer.Play();
btnPlay2.Visibility = Visibility.Hidden;
btnPause.IsEnabled = true;
}
}
Et voici les codes pour le curseur:
private void sliProgress_DragStarted(object sender, DragStartedEventArgs e)
{
userIsDraggingSlider = true;
}
private void sliProgress_DragCompleted(object sender, DragCompletedEventArgs e)
{
userIsDraggingSlider = false;
mediaPlayer.Position = TimeSpan.FromSeconds(sliProgress.Value);
}
private void sliProgress_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
lblTime.Content = TimeSpan.FromSeconds(sliProgress.Value).ToString(@"mm\:ss");
}
J'ai essayé de mettre en œuvre la procédure sur le bouton Suivant dans cette case lorsqu'on clique dessus en comparant quand la durée maximale d'une chanson/fichier (lblTime2) et la durée progressant (lblTime) est égale:
private void radioAll_Checked(object sender, RoutedEventArgs e)
{
if (lblTime.Content.ToString() == lblTime2.Content.ToString())
{
if (listBox.SelectedIndex < listBox.Items.Count - 1)
{
listBox.SelectedIndex = listBox.SelectedIndex + 1;
TagLib.File tagFile = TagLib.File.Create((listBox.SelectedValue).ToString());
string album = tagFile.Tag.Album;
string artist = tagFile.Tag.FirstAlbumArtist;
string title = tagFile.Tag.Title;
uint year = tagFile.Tag.Year;
string genre = tagFile.Tag.FirstGenre;
lblName.Content = artist + " - " + title;
lblAlbum.Content = album;
lblArtist.Content = artist;
lblTitle.Content = title;
lblYear.Content = year;
lblGenre.Content = genre;
lblBit.Content = tagFile.Properties.AudioBitrate + " kbps";
lblTime2.Content = tagFile.Properties.Duration.ToString(@"mm\:ss");
mediaPlayer.Open(new Uri((listBox.SelectedValue).ToString()));
mediaPlayer.Play();
}
}
}
Malheureusement, cela ne fonctionne pas du tout. Est-ce que j'ai raté quelque chose? Que devrais-je faire?
Merci pour votre aide, ça m'a aidé! –
J'ai lu ce [thread] (https://stackoverflow.com/questions/24320487/how-to-run-the-mediaplayer-on-repeat-mode-in-c-sharp-wpf) pour m'aider plus. Maintenant, je peux répéter un fichier en continu aussi. –
@StevenAldo excellent monsieur bien! :) – MickyD