一、功能说明
要求1:
1. 程序应能够读取MP3文件,并播放其中的音频。
2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
3. 程序应具有良好的用户界面,方便用户进行操作。
4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。
提示:此功能可以使用WindowsMediaPlayer控件
要求2:
1. 程序应能够播放ogg文件。
2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
3. 程序应具有良好的用户界面,方便用户进行操作。
4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。
提示:此功能可以使用Nuget程序包中的Naudi.Vorbis控件
二、窗体设计
button1修改为“选择歌曲”,用于播放mp3音乐文件。
button2修改为“下一首”,用于停止音乐文件。
button3修改为“停止播放”,用于切换到下一个音乐文件。
button4修改为“播放ogg”,用于播放ogg音乐文件。
listBox1用于显示音乐播放列表。label1用于显示当前播放音乐名称。
openFileDialog1用于获取音乐文件。
添加axWindowsMediaPlayer控件,实现音乐的播放、暂停、音量调节等操作。
三、代码实现
3.1添加依赖项NAudio包和NAudio.Vorbis包
NAudio库和NAudio.Vorbis库提供了丰富的音频处理功能,NAudio.Vorbis库中的VorbisWaveReader类能够帮助实现.ogg音频文件的播放。
using NAudio;
using NAudio.Wave;
using NAudio.Vorbis;
3.2定义音频名称和音频列表
string[] files;
List<string> localmusiclist = new List<string> { };
3.3.mp3音乐播放方法
private void musicplay(string filename)
{
axWindowsMediaPlayer1.URL = filename;//URL属性设置为传入的filename参数
string extension = Path.GetExtension(filename);//判断是MP3还是ogg文件
if (extension == ".ogg")
{
Console.WriteLine("这是.ogg文件");
}
else if (extension == ".mp3")
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
else
{
Console.WriteLine("无法识别的文件格式");
}
}//控制播放音乐
3.4 button1:选择音乐
在窗口设计中双击button1,实现选择音乐功能。用openFileDialog1.Filter设置openFileDialog1的过滤器,只显示.mp3文件,并且在对话框中显示“选择音频”,代码如下:
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "选择音频|*.mp3";//设置openFileDialog1的过滤器,只显示.mp3文件
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
localmusiclist.Clear();//清除原来的旧列表
if (files != null)
{
Array.Clear(files, 0, files.Length);//检查files中是否为空,不为空则要完全清除
}
files = openFileDialog1.FileNames;
string[] array = files;//避免foreach循环会直接修改files,用于存放多个音频文件
foreach (string x in array)
{
listBox1.Items.Add(x);
localmusiclist.Add(x);//遍历数组中的所有元素
}
}
}
3.5 button2:音频切换到下一曲
private void button3_Click(object sender, EventArgs e)
{
if (localmusiclist.Count > 0)
{
int index = listBox1.SelectedIndex + 1;//索引加1,获取下一个音频文件
if (index >= localmusiclist.Count())
{
index = 0;
}//最后一个音频文件,索引加1后,没有音频文件
axWindowsMediaPlayer1.URL = localmusiclist[index];
musicplay(axWindowsMediaPlayer1.URL);//调用musicplay方法,实现播放下一曲
label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[index]);
listBox1.SelectedIndex = index;//修改label1中显示的文本,并将listBox1的选取对象修改为当前播放的音乐
}
}
3.6 button2:音频停止播放
private void button2_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
3.7 button4:播放.ogg音频文件
首先获取音频文件地址。创建一个新的OpenFileDialog实例,用openFileDialog.Filter过滤其余类型文件,在对话框中选取.ogg文件,用户点击“确认”之后将文件地址赋值给对应的变量oggFilePath。
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "打开音频|*.ogg";
string oggFilePath = "";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
oggFilePath = openFileDialog.FileName;
}
使用VorbisWaveReader类来读取oggFilePath指定的 .ogg 音频文件。
再创建一个WaveOutEvent实例,用于播放音频。
初始化outputDevice,使用reader作为音频源,然后使用play函数来播放选取的音频源。
即可实现.ogg音频文件的播放,代码如下:
using (var reader = new VorbisWaveReader(oggFilePath))
{
using (var outputDevice = new WaveOutEvent())
{
outputDevice.Init(reader);
outputDevice.Play();
}
}
3.8 listbox1:显示音频列表
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (localmusiclist.Count > 0)
{
axWindowsMediaPlayer1.URL = localmusiclist[listBox1.SelectedIndex];
musicplay(axWindowsMediaPlayer1.URL);//传递URL参数
label1.Text=Path.GetFileNameWithoutExtension(localmusiclist[listBox1.SelectedIndex]);//label1中显示这个除去扩展名以外的音频文件名
}
}
四、完整代码
using System.Diagnostics.Eventing.Reader;
using System.Threading;
using NAudio;
using NAudio.Wave;
using NAudio.Vorbis;
using System.Diagnostics;
namespace WinFormsApp4._23_1
{
public partial class Form1 : Form
{
string[] files;
List<string> localmusiclist = new List<string> { };
public Form1()
{
InitializeComponent();
}
private void musicplay(string filename)
{
axWindowsMediaPlayer1.URL = filename;
string extension = Path.GetExtension(filename);
if (extension == ".ogg") { Console.WriteLine("这是.ogg文件"); }
else if (extension == ".mp3")
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
else
{
Console.WriteLine("无法识别的文件格式");
}
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "选择音频|*.mp3";//;*.flac;*.wav
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
localmusiclist.Clear();
if (files != null)
{
Array.Clear(files, 0, files.Length);
}
files = openFileDialog1.FileNames;
string[] array = files;
foreach (string x in array)
{
listBox1.Items.Add(x);
localmusiclist.Add(x);
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (localmusiclist.Count > 0)
{
axWindowsMediaPlayer1.URL = localmusiclist[listBox1.SelectedIndex];
musicplay(axWindowsMediaPlayer1.URL);
label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[listBox1.SelectedIndex]);
}
}
private void button2_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
private void button3_Click(object sender, EventArgs e)
{
if (localmusiclist.Count > 0)
{
int index = listBox1.SelectedIndex + 1;
if (index >= localmusiclist.Count())
{
index = 0;
}
axWindowsMediaPlayer1.URL = localmusiclist[index];
//axWindowsMediaPlayer1.Ctlcontrols.play();
musicplay(axWindowsMediaPlayer1.URL);
label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[index]);
listBox1.SelectedIndex = index;
}
}
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "打开音频|*.ogg";
string oggFilePath = "";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
oggFilePath = openFileDialog.FileName;
}
using (var reader = new VorbisWaveReader(oggFilePath))
{
using (var outputDevice = new WaveOutEvent())
{
outputDevice.Init(reader);
outputDevice.Play();
while (outputDevice.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(100);
}
}
}
}
}
}
五、总结
在C#中实现音乐文件的播放功能,我们考虑了MP3和OGG两种常见的音频格式。对于MP3文件的播放,我们可以选择使用Windows Media Player控件(适用于Windows Forms且受限于Windows平台)或NAudio库(跨平台且功能强大)。对于OGG文件的播放,由于NAudio本身可能不直接支持这种格式,我们可以使用Naudi.Vorbis库作为NAudio的扩展来处理OGG Vorbis编码的音频文件。