目录
一、内容
二、操作
三、运行结果
四、全部代码
五、小结
一、内容
编写一个C#程序,实现音乐文件的播放功能。
要求1:
1. 程序应能够读取MP3文件,并播放其中的音频。
2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
3. 程序应具有良好的用户界面,方便用户进行操作。
4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。
提示:此功能可以使用WindowsMediaPlayer控件
要求2:
1. 程序应能够播放ogg文件。
2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
3. 程序应具有良好的用户界面,方便用户进行操作。
4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。
提示:此功能可以使用Nuget程序包中的Naudi.Vorbis控件
二、操作
1、使用axWindowsMediaPlaver的COM组件
(1)加载COM组件:工具->选择工具箱->COM Components->Windows Media Player如下图:
(2)把Windows Media Plaver控件拖放到Winform窗体中,把axWindowsMediaPlaver1中URL
属性设置为MP3或是AVI的文件路径,并运行。
2、添加Nuget程序包并添加Naudi.Vorbis控件和Naudi.Wave
![](https://img-blog.csdnimg.cn/direct/eea7de1b156f4f769e4b193ffc545f92.png)
![](https://img-blog.csdnimg.cn/direct/6cd6be4ca36a43a8912310a2f7abbf49.png)
![](https://img-blog.csdnimg.cn/direct/e99fd91107164683af8dcc28393a25d5.png)
![](https://img-blog.csdnimg.cn/direct/18bf73b936da42e7ab3eb5e2d1cd8b47.png)
![](https://img-blog.csdnimg.cn/direct/6ca17f3e3d884721aa4c2943a9ef795c.png)
using NAudio;
using NAudio.Wave;
using NAudio.Vorbis;
3、添加musicplay函数
private void musicplay(string filename)
{
axWindowsMediaPlayer1.URL = filename;
string extension = Path.GetExtension(filename);
if (extension == ".ogg") { Console.WriteLine("这是ogg文件。"); }
else
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
4、把Listbox控件拖放到Winform窗体中(可将颜色改为MenuHighlight,字体改为微软雅黑五号)。
把Button1控件拖放到Winform窗体中,名称改为“选择歌曲”。进入编写相应代码。
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]);
}
}
5、把Label控件拖放到Winform窗体中,可将空间颜色改为红色。
6、添加调整声音大小控件拖放到Winform窗体中。编写相应代码。
private void trackBar1_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
}
7、把Button1控件拖放到Winform窗体中,名称改为“选择歌曲”。进入编写相应代码。
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "选择音频|*.mp3;*.flac;*.wav";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
localmusiclist.Clear();
listBox1.Items.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);
}
}
}
8、把Button2控件拖放到Winform窗体中,名称改为“停止播放”。进入编写相应代码。
private void button2_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
9、把Button3控件拖放到Winform窗体中,名称改为“下一首”。进入编写相应代码。
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];
musicplay(axWindowsMediaPlayer1.URL);
label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[index]);
listBox1.SelectedIndex = index;
}
}
10、把Button4控件拖放到Winform窗体中,名称改为播放ogg。进入编写相应代码以处理ogg播放
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 vorbisReader = new VorbisWaveReader(oggFilePath))
{
using (var outputDevice = new WaveOutEvent())
{
outputDevice.Init(vorbisReader);
outputDevice.Play();
// 等待播放完成,或者您可以根据需要添加其他逻辑
while (outputDevice.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(1000);
}
}
}
}
三、运行结果
四、全部代码
using NAudio;
using NAudio.Wave;
using NAudio.Vorbis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp5
{
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
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "选择音频|*.mp3;*.flac;*.wav";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
localmusiclist.Clear();
listBox1.Items.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 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];
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 vorbisReader = new VorbisWaveReader(oggFilePath))
{
using (var outputDevice = new WaveOutEvent())
{
outputDevice.Init(vorbisReader);
outputDevice.Play();
// 等待播放完成,或者您可以根据需要添加其他逻辑
while (outputDevice.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(1000);
}
}
}
}
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]);
}
}
}
}
五、小结
本次任务是开发一个能够播放MP3和OGG格式音乐文件的C#Windows Forms应用程序。经过一系列的设计、编码和测试过程。首先,用户界面设计:成功创建了一个Windows Forms应用程序,并添加了必要的UI控件,如播放/暂停/停止按钮、音量滑块、文件选择对话框等。界面设计简洁直观,用户能够方便地操作和管理音乐文件。第二,音频播放功能:应用程序能够支持MP3和OGG两种音频格式的播放。对于MP3文件,使用了Windows Media Player控件;对于OGG文件,则集成了NAudio库进行音频解码和播放。第三,文件管理:实现了文件选择对话框,允许用户浏览并选择要播放的音乐文件。同时,还设计了一个播放列表,用于管理用户选择的音乐文件。最后,NAudio的学习与使用:NAudio库虽然功能强大,但使用它需要一定的学习和理解时间。在开发过程中,需要熟悉NAudio的API和音频处理的相关知识。在开发过程中,对第三方了解和熟悉程度对项目的进展有着重要影响。