none
求用XML制作的WPF音乐播放器源码 RRS feed

  • 常规讨论

  • 我用WPF制作了一个音乐播放器,主要实现窗口加载音乐、播放、停止、快进、快退、声音控制等功能,源代码如下:

    XMAL代码:

    <UserControl x:Class="俗文化.Control.VideoControl.videoControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:sys="clr-namespace:System;assembly=System"
                 mc:Ignorable="d"
                 d:DesignHeight="300" d:DesignWidth="182"
                 HorizontalAlignment="Left" VerticalAlignment="Top" >
       
        <UserControl.Resources>
            <ResourceDictionary Source="videoControlDictionary.xaml" />
        </UserControl.Resources>
       
        <Grid Height="300" >
            <Image Source="/俗文化;component/Control/VideoControl/ico/back.jpg"/>
            <StackPanel Style="{x:Null}" Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" >
                <StackPanel  Height="45" Orientation="Vertical" Style="{x:Null}" HorizontalAlignment="Left" VerticalAlignment="Top" >
                <MediaElement Name="mediaElement" Margin="0" Width="182" Height="0" HorizontalAlignment="Center" VerticalAlignment="Top" LoadedBehavior="Manual" MouseLeftButtonDown="MouseLeftButtonDown_Click" MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"></MediaElement>
                <StackPanel Style="{StaticResource StackPanelResource}">
                    <Image Source="/俗文化;component/Control/VideoControl/ico/install.png" MouseLeftButtonDown="newuri_Click" Margin="28,0,0,0" />
                    <Image Name="play" MouseLeftButtonDown="play_Click" Source="/俗文化;component/Control/VideoControl/ico/play__next.png" />
                    <Image Name="pause" MouseLeftButtonDown="pause_Click" Source="/俗文化;component/Control/VideoControl/ico/play_pause.png" />
                    <Image Name="stop" MouseLeftButtonDown="stop_Click" Source="/俗文化;component/Control/VideoControl/ico/play_stop.png" />
                    <Image Name="forward" MouseLeftButtonDown="forward_Click" Source="/俗文化;component/Control/VideoControl/ico/play_fw.png" />
                    <Image Name="back" MouseLeftButtonDown="back_Click" Source="/俗文化;component/Control/VideoControl/ico/play_rev.png" />
                    <!--<Slider Style="{StaticResource SliderResource}" Maximum="1" LargeChange="0.01" Minimum="-1" Value="{Binding ElementName=mediaElement,Path=Balance, Mode=TwoWay, UpdateSourceTrigger=Default}" />
                    <Slider Style="{StaticResource SliderResource}" Maximum="2" LargeChange="0.01" Minimum="0" Value="{Binding ElementName=mediaElement,Path=SpeedRatio, Mode=TwoWay, UpdateSourceTrigger=Default}" />-->
                </StackPanel>
                <Slider Style="{StaticResource SliderResource}" Maximum="1" LargeChange="0.01" Minimum="0" Value="{Binding ElementName=mediaElement,Path=Volume, Mode=TwoWay, UpdateSourceTrigger=Default}" />
            </StackPanel>
                <Canvas Style="{StaticResource CanvasResource}">
                    <TreeView Style="{StaticResource TreeViewResource}"></TreeView>
                </Canvas> 
            </StackPanel>
        </Grid>
    </UserControl>

    后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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 Microsoft.Win32;
    using System.IO;
    using System.Xml.Linq;


    namespace 俗文化.Control.VideoControl
    {
        /// <summary>
        /// MediaPlayerControl.xaml 的交互逻辑
        /// </summary>
        public partial class videoControl : UserControl
        {
            public videoControl()
            {
                InitializeComponent();
            }

            /// 音乐(屏幕放映结束)
            private void Element_MediaEnded(object sender, RoutedEventArgs e)
            {
                //mediaElement.Pause();
                //mediaElement.Position = TimeSpan.Zero;
                mediaElement.Stop();
            }
            /// 音乐屏幕操作事件)
            private void MouseLeftButtonDown_Click(object sender, MouseButtonEventArgs e)
            {
                mediaElement.Pause();
            }
            /// 音乐(加载执行命令)
            private void Element_MediaOpened(object sender, RoutedEventArgs e)
            {
                //mediaElement.Position = TimeSpan.Zero;
                mediaElement.Play();
            }
            /// 音乐打开文件
            private void newuri_Click(object sender, MouseButtonEventArgs e)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "All Files(*.*)|*.*";
                openFileDialog.InitialDirectory = "";
                openFileDialog.ShowDialog();
                String musicName = openFileDialog.FileName;
                if ((musicName != null) && (musicName != ""))
                {
                    mediaElement.LoadedBehavior = MediaState.Manual;
                    mediaElement.Source = new Uri(musicName);
                    mediaElement.Play();
                }
            }
            /// 播放
            private void play_Click(object sender, MouseButtonEventArgs e)
            {
                mediaElement.Play();
            }
            /// 音乐暂停
            private void pause_Click(object sender, MouseButtonEventArgs e)
            {
                mediaElement.Pause();
            }
            /// 音乐停止播放
            private void stop_Click(object sender, MouseButtonEventArgs e)
            {
                mediaElement.Stop();
            }
            /// 音乐前进
            private void forward_Click(object sender, MouseButtonEventArgs e)
            {
                mediaElement.Position = mediaElement.Position + TimeSpan.FromSeconds(20);
            }
            /// 音乐后退
            private void back_Click(object sender, MouseButtonEventArgs e)
            {
                mediaElement.Position = mediaElement.Position - TimeSpan.FromSeconds(20);
            } /// 音乐时间轴控制
            private void SliderValueChanged_Click(object sender, RoutedPropertyChangedEventArgs<double> e)
            {
            }
            /// 音乐时间轴控制(按下)
            private void SliderMouseLeftButtonDown_Click(object sender, MouseButtonEventArgs e)
            {
            }
            /// 音乐时间轴控制(松开)
            private void SliderMouseLeftButtonUp_Click(object sender, MouseButtonEventArgs e)
            {
            }
           
        }
    }

    我现在想利用XML作为数据存储,制作一个XML播放列表,实现象千千播放器一样列表的功能,但不知道怎么制作,请各位高人指点,或给我传一个具有上述功能的WPF播放器源代码,我的邮箱:dszzlmc@live.cn ,谢谢了。

    • 已移动 Mike Feng 2012年7月20日 5:54 WPF (发件人:.NET Framework 一般性问题讨论区)
    • 已更改类型 Min ZhuModerator 2012年8月10日 8:45
    2012年7月19日 8:43

全部回复

  • Hi,

    我把这个帖子移到WPF论坛了。

    谢谢。


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年7月20日 5:56