[英]Play multiple files in WPF listbox
大家好,最近我正在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();
}
我想知道我在該代碼中錯過了什么。 我應該將事件添加到列表框嗎?
對於您的幫助,我想說謝謝。
也許您忘了張貼它。 但這似乎並沒有告訴MediaPlayer
要播放什么文件。 您要做的就是獲取歌曲路徑,並告訴MediaPlayer
播放-而不給它播放什么文件。
假設列表框項目是歌曲路徑的字符串,則需要這樣做。
mediaPlayer.Open(new Uri(item)); //item == song path(Including extension, i.e. .mp3)
mediaPlayer.Play();
但是,您似乎也正在使用.SafeFileName
,這將使此問題成為問題。 因此,如果我們要直接從列表框字符串設置歌曲,那么您可能也想這樣做。
files = ofd.FileNames;
非常感謝@Toskr的回答和糾正!
我已經修改了我的代碼,並將其分享給需要它的任何人。
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.FileNames;
foreach (string song in files)
{
if (!listBox.Items.Contains(song))
{
listBox.Items.Add(song);
}
}
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)
{
lblName.Content = (listBox.SelectedValue).ToString();
mediaPlayer.Open(new Uri((listBox.SelectedValue).ToString()));
mediaPlayer.Play();
}
private void listBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
lblName.Content = (listBox.SelectedValue).ToString();
mediaPlayer.Open(new Uri((listBox.SelectedValue).ToString()));
mediaPlayer.Play();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.