播放列表不播放歌曲

时间:2014-08-20 15:06:40

标签: c# wpf

这是我的问题,我试图只显示我想要播放的文件的名称,但是当我这样做时,我的播放列表只是跳过这首歌并且实际上没有播放它。我不知道它为什么要这样做。所以我的问题是,如何才能显示歌曲名称以便实际播放?

的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" />
    <Image x:Key="Pause" Source="Images/pause.png" />
    <Image x:Key="Muted" Source="Images/volume_0.png" />
    <Image x:Key="Low" Source="Images/volume_low.png" />
    <Image x:Key="High" Source="Images/volume_high.png" />
</Window.Resources>
<DockPanel>
    <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="playlist"
                     ItemsSource="{Binding Path=FileList}"
                     SelectedItem="{Binding Path=SelectedMedia}">
            </ListBox>
        </StackPanel>

        <MediaElement x:Name="mediaPlayer"
                    Margin="3"
                    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="25*" />
                <ColumnDefinition Width="25*" />
                <ColumnDefinition Width="25*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="20*" />
            </Grid.RowDefinitions>

            <Button x:Name="playPauseButton"
                    Margin="3"
                    Grid.Column="1"
                    Click="PlayPause">
                <DynamicResource ResourceKey="Play" />
            </Button>

        </Grid>

        </Grid>
</DockPanel>

代码背后:

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;
using System.Windows.Threading;

namespace MusicalCloud
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    DispatcherTimer timer;

    public delegate void timerTick();
    timerTick tick;

    string sec, min, hours;
    string[] result;
    private bool mediaPlayerIsPlaying = false;
    private bool userIsDraggingSlider = false;
    public ObservableCollection<string> FileList { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        GetFiles(@"C:\Users\Donald Rohlman\Dropbox\Music");
        this.DataContext = this;
    }

    private void GetFiles(string folderPath)
    {
        string[] files = Directory.GetFiles(folderPath).Select(path => System.IO.Path.GetFileName(path)).ToArray();
        FileList = new ObservableCollection<string>(files);
    }

    private void PlayPause(object sender, RoutedEventArgs e)
    {
        if (playPauseButton.Content == FindResource("Play"))
        {
            mediaPlayer.Play();
            mediaPlayerIsPlaying = true;
            playPauseButton.Content = FindResource("Pause");
        }
        else
        {
            mediaPlayer.Pause();
            mediaPlayerIsPlaying = false;
            playPauseButton.Content = FindResource("Play");
        }
    }

    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)
    {
        mediaPlayer.Stop();
        mediaPlayerIsPlaying = false;
        CurrentMedia = media ?? SelectedMedia;
        PlayPause(null, null);
    }

    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()));

}
}

0 个答案:

没有答案
相关问题