我正在制作一个媒体播放器,我将媒体文件添加到列表框中。我想做的是让媒体元素在当前音乐结束后自动开始播放列表框中的下一首歌曲/视频。另外我想制作一个doubleclickevent,如果listitem点击了应播放的歌曲/视频。这就是我目前所拥有的:
的Xaml:
<MediaElement Name="objMediaPlayer" LoadedBehavior="Manual" UnloadedBehavior="Stop" MediaOpened="objMediaPlayer_MediaOpened" MediaEnded="objMediaPlayer_MediaEnded" Margin="20,19,20,40" ButtonBase.Click="mediaItemList_MouseDoubleClick" />
<ListBox Canvas.Left="882" Canvas.Top="12" Height="467" Name="mediaItemList" Width="260" Background="Gray" MouseDoubleClick="mediaItemList_MouseDoubleClick" />
CS-片:
private void BrowseButtonClick(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
dlg.Multiselect = true;
dlg.InitialDirectory = "c:\\";
dlg.Filter = "All Files (*.*)|*.*";
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (string file in dlg.FileNames)
{
FileInfo fileName = new FileInfo(file);
mediaItemList.Items.Add(fileName);
}
string selectedFileName = dlg.FileName;
fileNameLabel.Content = selectedFileName;
objMediaPlayer.Source = new Uri(selectedFileName);
objMediaPlayer.Play();
lblCoverUp.Visibility = Visibility.Hidden;
}
}
private int currentSongIndex = -1;
private void objMediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
{
if (currentSongIndex == -1)
{
currentSongIndex = mediaItemList.SelectedIndex;
}
currentSongIndex++;
if (currentSongIndex < mediaItemList.Items.Count)
{
objMediaPlayer.Play();
}
else
{
// last song in listbox has been played
}
}
private void mediaItemList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
对于Doubleclick功能
private void mediaItemList_MouseDoubleClick(object sender, RoutedEventArgs e)
{
System.Windows.Controls.Button prevButton = objMediaPlayer.Tag as System.Windows.Controls.Button;
System.Windows.Controls.Button button = (sender as System.Windows.Controls.Button);
FileInfo fileInfo = button.DataContext as FileInfo;
// If a file is playing, stop it
if (prevButton != null)
{
objMediaPlayer.Tag = null;
objMediaPlayer.Stop();
prevButton.Background = Brushes.LightYellow;
// if the one thats playing is the one that was clicked -> don't play it
if (prevButton == button)
return;
}
// Play the one that was clicked
objMediaPlayer.Tag = button;
objMediaPlayer.Source = new Uri(fileInfo.FullName);
objMediaPlayer.Play();
}
对于Media_ended,我已经尝试过:objMediaPlayer.Play(mediaItemList.Items [currentSongIndex]);
当激活Doubleclick事件时,我收到以下错误:对象引用未设置为对象的实例。 (FileInfo fileInfo = button.DataContext as FileInfo)。
我希望有人可以帮助我。如果您需要更多信息,请询问
编辑: 我通过使用以下代码修复了大部分媒体结束问题:
private void objMediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
{
objMediaPlayer.Stop();
if (mediaItemList.SelectedIndex <= mediaItemList.Items.Count)
{
mediaItemList.SelectedIndex = mediaItemList.SelectedIndex += 1;
fileNameLabel.Content = mediaItemList.SelectedItem;
objMediaPlayer.Play();
}
else
{
objMediaPlayer.Stop();
fileNameLabel.Content = " ";
}
}
现在唯一的问题是播放器在完成最后一首歌(listitem)后没有停止。你是怎么解决的。
答案 0 :(得分:1)
当然你需要在播放之前打开每个文件吗?:
private void objMediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
{
if (currentSongIndex == -1)
{
currentSongIndex = mediaItemList.SelectedIndex;
}
currentSongIndex++;
if (currentSongIndex < mediaItemList.Items.Count)
{
objMediaPlayer.Open(new Uri(mediaItemList.ElementAt(currentSongIndex),
UriKind.Absolute));
}
else
{
// last song in listbox has been played
}
}
private void MediaPlayer_MediaOpened(object sender, EventArgs e)
{
objMediaPlayer.Play();
}