在WPF列表框中播放多个文件

时间:2017-04-23 06:01:42

标签: c# wpf listbox media-player

This is how my music player looks like

大家好,最近我在Visual Studio的WPF上做了一个音乐播放器项目。 完成所有基本功能后,现在我想创建一个包含多个文件的文件播放列表。

因此,当它运行时,首先我点击“打开文件”按钮,选择一个文件。然后,它将被加载到列表框中。要播放该歌曲,我可以选择双击列表框中的歌曲,也可以选择之后点击播放按钮。和"正在播放"下的lblName信息我的歌曲选择改变了。

我试图谷歌搜索和编码(一些复制和修改),文件加载到我的列表框中,但它根本无法播放。

<Window x:Class="WPFplayer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPFplayer"
    mc:Ignorable="d"
    Title="WPF Music Player" Height="380.436" Width="507.483">
<Grid Margin="0,0,2,0" Height="350" VerticalAlignment="Top">
    <Grid.Background>
        <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0" SpreadMethod="Repeat">
            <GradientStop Color="Black" Offset="1"/>
            <GradientStop Color="#FFF3FF00" Offset="0.6"/>
        </LinearGradientBrush>
    </Grid.Background>
    <Button x:Name="btnOpen" Content="Open File..." HorizontalAlignment="Left" Margin="17,162,0,0" VerticalAlignment="Top" Width="75" Click="btnOpen_Click"/>
    <Button x:Name="btnPlay" Content="Play" HorizontalAlignment="Left" Margin="131,154,0,0" VerticalAlignment="Top" Width="75" Click="btnPlay_Click" Height="30"/>
    <Button x:Name="btnPause" Content="Pause" HorizontalAlignment="Left" Margin="211,154,0,0" VerticalAlignment="Top" Width="75" Click="btnPause_Click" Height="30"/>
    <Button x:Name="btnStop" Content="Stop" HorizontalAlignment="Left" Margin="291,154,0,0" VerticalAlignment="Top" Width="75" Click="btnStop_Click" Height="30"/>
    <Label x:Name="lblTime" Content="-----" HorizontalAlignment="Left" Margin="371,121,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.471,0.295"/>
    <Label x:Name="lblBiasa" Content="Now Playing :" HorizontalAlignment="Left" Margin="17,68,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
    <Label x:Name="lblName" Content="(No Song...)" HorizontalAlignment="Left" Margin="17,94,0,0" VerticalAlignment="Top"/>
    <Slider x:Name="sliProgress" Thumb.DragStarted="sliProgress_DragStarted"  Thumb.DragCompleted="sliProgress_DragCompleted" ValueChanged="sliProgress_ValueChanged" HorizontalAlignment="Left" Margin="17,125,0,0" VerticalAlignment="Top" Width="354"/>
    <Slider x:Name="sliderVol" Value="0.5" Minimum="0" HorizontalAlignment="Left" Margin="436,92,0,0" VerticalAlignment="Top" Width="33" TickPlacement="BottomRight" Cursor="Arrow" Orientation="Vertical" Height="121" ValueChanged="sliderVol_ValueChanged" TickFrequency="0.1" SmallChange="0.01" LargeChange="0.1" Maximum="1"/>
    <Label x:Name="label" Content="MUSIC PLAYER" HorizontalAlignment="Left" Margin="173,10,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="22"/>
    <Label x:Name="label1" Content="Volume" HorizontalAlignment="Left" Margin="423,64,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
    <CheckBox x:Name="chkKaraoke" Content="Karaoke" HorizontalAlignment="Left" Margin="286,78,0,0" VerticalAlignment="Top"/>
    <CheckBox x:Name="checkBox" Content="Bass Boost" HorizontalAlignment="Left" Margin="286,99,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.743,0.625"/>
    <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="139" Margin="17,189,0,0" VerticalAlignment="Top" Width="388" SelectionChanged="listBox_SelectionChanged" MouseDoubleClick="listBox_MouseDoubleClick"/>
</Grid>

string[] files;

private void btnOpen_Click(object sender, RoutedEventArgs e)
{
  OpenFileDialog ofd = new OpenFileDialog();
  ofd.AddExtension = true;
  ofd.DefaultExt = "*.*";
  ofd.Filter = "All files (*.*)|*.*";
  ofd.Multiselect = true;
  ofd.ShowDialog();

  files = ofd.SafeFileNames;

  foreach (string song in files)
  {
    if (!listBox.Items.Contains(song))
    {
        listBox.Items.Add(song);
    }
  }

  foreach (var item in listBox.SelectedItems)
  {      
    lblName.Content = ofd.SafeFileName;
    mediaPlayer.Play();
  }

  DispatcherTimer timer = new DispatcherTimer();
  timer.Interval = new TimeSpan(0, 0, 1);
  timer.Tick += timer_Tick;
  timer.Start();
}

private void btnPlay_Click(object sender, RoutedEventArgs e)
{
  foreach (var item in listBox.SelectedItems)
  {       
    mediaPlayer.Play();
  }
}

private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  lblName.Content = (listBox.SelectedValue).ToString();
}

private void listBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
  lblName.Content = (listBox.SelectedValue).ToString();    
  mediaPlayer.Play();
}

我想知道我在那段代码中错过了什么。我应该在列表框中添加一个事件吗?

为了你的帮助,我想说声谢谢。

2 个答案:

答案 0 :(得分:0)

也许你忘记发帖了。但看起来你甚至没有告诉MediaPlayer要播放什么文件。你所做的只是获取歌曲路径,告诉MediaPlayer播放 - 而不给它播放什么文件。

假设列表框项是歌曲路径的字符串,你会想要这个。

    mediaPlayer.Open(new Uri(item)); //item == song path(Including extension, i.e. .mp3)
    mediaPlayer.Play();

但是,您似乎也在使用.SafeFileName,这会导致此问题。因此,如果我们要直接从列表框字符串设置歌曲,您可能还想这样做。

files = ofd.FileNames;

答案 1 :(得分:0)

非常感谢@Toskr的回答和纠正!

我修改了我的代码,并且我会为任何需要它的人分享这个代码。

(fun n -> (n, n + 1))