【www.bbyears.com--WPF】
好了,开始我们第一部分的教程--音乐播放类的构建。
话说利用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();
}
}
}
}
好了,代码就全部写完了,大家可参考一下。