如何播放列表框中连续播放的项目
本文关键字:播放 项目 连续 播放列表 | 更新日期: 2023-09-27 18:17:41
我需要一些帮助,如何我可以拿我的列表框和读取选定的歌曲和播放这首歌,然后允许下一首歌曲之后播放这首歌。我的问题是,我有一个mediaend为我的MediaElement,但我不知道如何使它播放,当你点击项目或继续播放后,你已经完成了一首歌。下面是我的代码:
Xaml.cs:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MusicalCloud
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private int currentSongIndex = -1;
private bool mediaPlayerIsPlaying = false;
private bool userIsDraggingSlider = false;
ObservableCollection<string> mFileList;
public MainWindow()
{
InitializeComponent();
GetFiles(@"E:'Dropbox'Music");
this.DataContext = mFileList;
}
private void GetFiles(string folderPath)
{
string[] files = Directory.GetFiles(folderPath);
mFileList = new ObservableCollection<string>(files);
}
private void PlayPause(object sender, RoutedEventArgs e)
{
if (playPauseButton.Content == FindResource("Play"))
{
musicDisplay.Play();
mediaPlayerIsPlaying = true;
playPauseButton.Content = FindResource("Pause");
}
else
{
musicDisplay.Pause();
mediaPlayerIsPlaying = false;
playPauseButton.Content = FindResource("Play");
}
}
private void Player_MediaEnded(object sender, EventArgs e)
{
if(currentSongIndex == -1)
{
currentSongIndex = lstSongs.SelectedIndex;
}
currentSongIndex++;
if(currentSongIndex < lstSongs.Items.Count)
{
musicDisplay.Play(lstSongs.Items[currentSongIndex]);
}
else
{
}
}
private void openMenuItem_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Multiselect = true;
open.Filter = "All files (*.*) | *.*";
if (open.ShowDialog() == true)
{
musicDisplay.Source = new Uri(open.FileName);
}
}
private void exitMenuItem_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
}
}
}
Xaml: <Window x:Class="MusicalCloud.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Musical Cloud" Height="400" Width="600">
<Window.Resources>
<Image x:Key="Play" Source="Images/play.png" Height="30" Width="30" />
<Image x:Key="Pause" Source="Images/pause.png" Height="30" Width="30" />
</Window.Resources>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_Open"
Click="openMenuItem_Click"/>
<MenuItem Header="_Close"
Click="exitMenuItem_Click"/>
</Menu>
<Grid Background="DarkGray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35*" />
<ColumnDefinition Width="65*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="85*" />
<RowDefinition Height="15*" />
</Grid.RowDefinitions>
<StackPanel x:Name="musicFinder"
Grid.Row="0"
Grid.Column="0">
<ListBox x:Name="lstSongs"
ItemsSource="{Binding}">
</ListBox>
</StackPanel>
<MediaElement x:Name="musicDisplay"
Grid.Row="0"
Grid.Column="1"
LoadedBehavior="Manual"
MediaEnded="Player_MediaEnded"
Source="{Binding ElementName=musicFinder, Path=SelectedItem}">
</MediaElement>
<Grid x:Name="mediaControls"
Grid.Row="1"
Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20*" />
</Grid.RowDefinitions>
<Button x:Name="backButton"
Height="30"
Width="30"
Grid.Row="0"
Grid.Column="0">
<Image Source="Images/previous.png" />
</Button>
<Button x:Name="rewindButton"
Height="30"
Width="30"
Grid.Column="1">
<Image Source="Images/fast_backward.png" />
</Button>
<Button x:Name="playPauseButton"
Height="40"
Width="40"
Grid.Column="2"
Click="PlayPause">
<DynamicResource ResourceKey="Play" />
</Button>
<Button x:Name="ffButton"
Height="30"
Width="30"
Grid.Column="3">
<Image Source="Images/fast_forward.png" />
</Button>
<Button x:Name="skipButton"
Height="30"
Width="30"
Grid.Column="4">
<Image Source="Images/next.png" />
</Button>
</Grid>
<StackPanel x:Name="timeLine"
Grid.Row="1"
Grid.Column="1">
</StackPanel>
</Grid>
</DockPanel>
下面是我在注释中提到的示例
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_Open"
Click="openMenuItem_Click" />
<MenuItem Header="_Close"
Click="exitMenuItem_Click" />
</Menu>
<Grid Background="DarkGray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35*" />
<ColumnDefinition Width="65*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="85*" />
<RowDefinition Height="15*" />
</Grid.RowDefinitions>
<StackPanel x:Name="musicFinder"
Grid.Row="0"
Grid.Column="0">
<ListBox ItemsSource="{Binding FileList}"
SelectedItem="{Binding SelectedMedia}">
</ListBox>
</StackPanel>
<MediaElement x:Name="musicDisplay"
Grid.Row="0"
Grid.Column="1"
LoadedBehavior="Manual"
MediaEnded="Player_MediaEnded"
MediaOpened="Player_MediaOpened"
MediaFailed="Player_MediaEnded"
Source="{Binding CurrentMedia}">
</MediaElement>
<Grid x:Name="mediaControls"
Grid.Row="1"
Grid.Column="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20*" />
</Grid.RowDefinitions>
<Button x:Name="backButton"
Height="30"
Width="30"
Grid.Row="0"
Grid.Column="0">
|<
</Button>
<Button x:Name="rewindButton"
Height="30"
Width="30"
Grid.Column="1">
< <
</Button>
<Button x:Name="playPauseButton"
Height="40"
Width="40"
Grid.Column="2"
Click="PlayPause">
>
</Button>
<Button x:Name="ffButton"
Height="30"
Width="30"
Grid.Column="3">
> >
</Button>
<Button x:Name="skipButton"
Height="30"
Width="30"
Grid.Column="4">
>|
</Button>
</Grid>
<StackPanel x:Name="timeLine"
Grid.Row="1"
Grid.Column="1">
</StackPanel>
</Grid>
</DockPanel>
我已经替换了xaml中的一些图像,您可以将它们替换回来
背后的代码public partial class MainWindow : Window
{
private bool mediaPlayerIsPlaying = false;
public ObservableCollection<string> FileList { get; set; }
public MainWindow()
{
InitializeComponent();
GetFiles(@"E:'vids");
this.DataContext = this;
}
private void GetFiles(string folderPath)
{
string[] files = Directory.GetFiles(folderPath);
FileList = new ObservableCollection<string>(files);
}
private void PlayPause(object sender, RoutedEventArgs e)
{
if (mediaPlayerIsPlaying)
{
musicDisplay.Pause();
mediaPlayerIsPlaying = false;
}
else
{
musicDisplay.Play();
mediaPlayerIsPlaying = true;
}
}
private void Player_MediaEnded(object sender, EventArgs e)
{
int currentSongIndex = FileList.IndexOf(SelectedMedia);
currentSongIndex++;
if (currentSongIndex < FileList.Count)
{
SelectedMedia = FileList[currentSongIndex] as string;
Play();
}
else
{
CurrentMedia = null;
}
}
private void Player_MediaOpened(object sender, RoutedEventArgs e)
{
mediaPlayerIsPlaying = true;
}
void Play(string media = null)
{
musicDisplay.Stop();
mediaPlayerIsPlaying = false;
CurrentMedia = media ?? SelectedMedia;
PlayPause(null, null);
}
private void openMenuItem_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Multiselect = true;
open.Filter = "All files (*.*) | *.*";
if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Play(open.FileName);
}
}
private void exitMenuItem_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
}
public string CurrentMedia
{
get { return (string)GetValue(CurrentMediaProperty); }
set { SetValue(CurrentMediaProperty, value); }
}
// Using a DependencyProperty as the backing store for CurrentMedia. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CurrentMediaProperty =
DependencyProperty.Register("CurrentMedia", typeof(string), typeof(MainWindow), new PropertyMetadata(null));
public string SelectedMedia
{
get { return (string)GetValue(SelectedMediaProperty); }
set { SetValue(SelectedMediaProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedMedia. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedMediaProperty =
DependencyProperty.Register("SelectedMedia", typeof(string), typeof(MainWindow), new PropertyMetadata(null, (s, e) => (s as MainWindow).Play()));
}
我已经创建了两个属性SelectedMedia和CurrentMedia,并使用它们来控制列表框和媒体元素。
我也更改了一些现有的代码,您可以根据需要进行调整。
上面的示例将按顺序播放列表中的所有项目,也将播放菜单打开的任何项目,并在完成后恢复到列表。
试一下,看看是否足够接近
您可以使用这样的方法。这不是最好的方法,但已经足够了;
private void Media_Ended(object sender, EventArgs e)
{
media.Position = TimeSpan.Zero;
media.Play();
}