分享

WPF开发较为完整的音乐播放器(一)

 五行和谐 2014-11-06

 

    近来闲来有事,便想到用自己这段时间学习的知识写一个音乐播放器。提前声明,我不擅长界面,因此做出来的界面的却有些次,但不是本系列文章的重点。

    先讲下我们开发此音乐播放器所用到的技术:数据绑定、Xml、MediaPlayer类、数据模板等,将在之后陆续讲解。

   来阐述下播放器开发的整体思路:构建音乐播放类用于播放音乐,用两个控件分别作为播放列表和播放控制,并且利用控件模板改变它们的界面,利用Xml数据读取类XmlListsReader来读取位于存放列表的xml,将歌曲名称、文件路径、持续时间歌手等信息读取到Product类中,并设置ListBox的ItemSouse为此类,采用数据模板显示数据。

    好了,开始我们第一部分的教程--音乐播放类的构建。

  

  话说利用WPF播放音乐有多种方法:MediaPlayer类,SoundPlayer类,以及使用DirectX Sound等。若要选择一种功能较多,方便易用的方法,定要属MediaPlayer类了,唯一的限制就是需要依赖Windows Media Player(WMP)。不过在Windows环境下,这一限制可以忽略不计,都是系统自带的,不是吗?

    当然,我们可以直接在窗口中防置MediaPlayer的操作代码,但是为了更正规化和可维护性,我们将它封装进MusicPlayer类中。  

  在类的开头,先来定义几个私有变量和公有的枚举(表示播放器的状态):

复制代码
       public enum PlayState : int
       {
           stoped = 0,
           playing = 1,
           paused = 2
       }
       private MediaPlayer player = null;
       private Uri musicfile;
       private PlayState state;

       public Uri MusicFile
       {
           set
           {
               musicfile = value;
           }
           get
           {
               return musicfile;
           }
       }
复制代码

接下来写构造函数,一个带参数(音乐文件路径),一个不带参数的:

复制代码
       public MusicPlay()
       {
           player = new MediaPlayer();
       }
       public MusicPlay(Uri file)
       {
           Load(file);          
       }
复制代码

构造函数将传入的文件路径传到Load方法中处理,以下是Load方法的代码:

复制代码
       public void Load(Uri file)
       {
           player = new MediaPlayer();
           MusicFile = file;
           player.Open(musicfile);

       }
复制代码


Load方法中设置了MusicFile(公有变量,指示文件路径),用MediaPlayer的Open方法加载了音乐文件。

接下来是播放、暂停、停止的代码:

复制代码
       public void Play()
       {
           player.Play();
           state = PlayState.playing;
       }
       public void Pause()
       {
           player.Pause();
           state = PlayState.paused;
       }
       public void Stop()
       {
           player.Stop();
           state = PlayState.stoped;
       }
复制代码


以上三个方法的第一句代码分别是设置播放、暂停、停止,第二句代码是设置播放器当前的状态。

然后是获取音乐文件的自然持续时间:

复制代码
       public TimeSpan GetMusicDurationTime()
       {
           while (!player.NaturalDuration.HasTimeSpan)
           {
               if (player.NaturalDuration.HasTimeSpan)
               {
                   return player.NaturalDuration.TimeSpan;
               }
           }
           return new TimeSpan();
       }
复制代码

这里用了MediaPlayer的NaturalDuration.HasTimeSpan检查是否可以读取音乐的自然持续时间,还用了While循环避免了读取到空值或读取不到的情况。

这是设置和获取当前进度的方法:

复制代码
       public void SetPosition(TimeSpan tp)
       {
           player.Position = tp;
       }
       public TimeSpan GetPosition()
       {
           return player.Position;
       }
复制代码

设置音量和读取音量,传入参数为0即是静音:

复制代码
       public void SetVolume(double volume)
       {
           player.Volume = volume;
       }
       public double GetVolume(double volume)
       {
          return player.Volume;
       }
复制代码


获取和设置当前播放器的状态,这里只是三种状态,但实际可能会更多:

复制代码
       public PlayState GetPlayState()
       {
           return state;
       }
       public void SetPlayState(PlayState state)
       {
           if (state == PlayState.playing)
           {
               this.Play();
           }
           else if (state == PlayState.paused)
           {
               this.Pause();
           }
           else if (state == PlayState.stoped)
           {
               this.Stop();
           }
       }
复制代码


再加个根据文件路径获取音乐名字的方法:

       public string GetMusicTitle()
       {
           string title=player.Source.ToString();
           return title.Substring(title.LastIndexOf("/")+1, title.Length - title.LastIndexOf("/")-1);
       }

这个方法的第二句代码就是截取字符串最后一个"/"后边的部分。

还有我在这里还定义了个  DispatcherTimer,用来更新音乐的位置,完全可以用更改通知来完成相同的任务,但我没有用到,大家可以尝试下。

以下是完整代码,没写注释,凑合看吧,应该不难:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
namespace WpfApplication10
{
   public  class MusicPlay
    {
       public enum PlayState : int
       {
           stoped = 0,
           playing = 1,
           paused = 2
       }
       private MediaPlayer player = null;
       private Uri musicfile;
       private PlayState state;

       public Uri MusicFile
       {
           set
           {
               musicfile = value;
           }
           get
           {
               return musicfile;
           }
       }

      public DispatcherTimer dt = null;
       public MusicPlay()
       {
           player = new MediaPlayer();
       }
       public MusicPlay(Uri file)
       {
           Load(file);
           
           
       }

       public void Load(Uri file)
       {
           player = new MediaPlayer();
           MusicFile = file;
           player.Open(musicfile);

       }

       public void Play()
       {
           player.Play();
           dt.Start();
           state = PlayState.playing;
       }
       public void Pause()
       {
           player.Pause();
           state = PlayState.paused;
       }
       public void Stop()
       {
           player.Stop();
           state = PlayState.stoped;
       }
       public string GetMusicTitle()
       {
           string title=player.Source.ToString();
           return title.Substring(title.LastIndexOf("/")+1, title.Length - title.LastIndexOf("/")-1);
           //return "";
       }
       public TimeSpan GetMusicDuringTime()
       {
           while (!player.NaturalDuration.HasTimeSpan)
           {
               if (player.NaturalDuration.HasTimeSpan)
               {
                   return player.NaturalDuration.TimeSpan;
               }
           }
           return new TimeSpan();
          
       }
       public void SetPosition(TimeSpan tp)
       {
           player.Position = tp;
       }
       public TimeSpan GetPosition()
       {
           return player.Position;
       }
       public void SetVolume(double volume)
       {
           player.Volume = volume;
       }
       public double GetVolume(double volume)
       {
          return player.Volume;
       }
       public PlayState GetPlayState()
       {
           return state;
       }
       public void SetPlayState(PlayState state)
       {
           if (state == PlayState.playing)
           {
               this.Play();
           }
           else if (state == PlayState.paused)
           {
               this.Pause();
           }
           else if (state == PlayState.stoped)
           {
               this.Stop();
           }
       }
    }
}
复制代码


可别急着构建界面。。。在下篇文章中我们将构建Xml读取类,敬请期待,同时期待拍砖者的到来。。。。。。。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多