在当今的数字化时代,音乐已成为我们日常生活中不可或缺的一部分。随着技术的不断进步,构建自己的音乐播放器变得越来越简单。在本文中,我将向您展示如何使用 C# 和 Windows Forms 应用程序,结合强大的 NAudio 库,来构建一个基础但功能齐全的音乐播放器。
一、项目设置与初始化
首先,创建一个新的 Windows Forms 应用程序项目,并为其命名,例如“WindowsFormsApp4”。接下来,通过 NuGet 包管理器安装 NAudio 库,这是一个开源的音频处理库,它将帮助我们处理各种音频格式。
二、界面设计
在 Windows Forms 设计器中,拖拽并放置所需的控件,如 ListBox 用于显示歌曲列表,Button 用于控制播放、停止和下一曲等操作,Label 用于显示当前播放的歌曲名,以及 TrackBar 用于控制音量。
三、功能实现
-
加载音乐文件
当用户点击“加载音乐”按钮时,我们打开一个 OpenFileDialog,允许用户选择多个 MP3 文件。选定的文件路径将存储在字符串数组中,并同时添加到 ListBox 和一个本地音乐列表(localmusiclist)中。
-
播放音乐
当用户从 ListBox 中选择一个歌曲时,或者点击“下一曲”按钮时,我们将使用 Windows Media Player 控件(axWindowsMediaPlayer1)来播放选定的歌曲。此外,我们还使用了一个名为 musicplay
的辅助函数来处理不同格式的文件。
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]);
}
}
-
支持 OGG 格式
为了支持 OGG 格式的音乐文件,我们使用了 NAudio 库中的 VorbisWaveReader
类。当用户点击一个专门为 OGG 文件设计的播放按钮时,我们将使用 NAudio 来播放选定的 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 reader = new VorbisWaveReader(oggFilePath))
{
using (var outputDevice = new WaveOutEvent())
{
outputDevice.Init(reader);
outputDevice.Play();
while (outputDevice.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(100);
}
}
}
}
-
音量控制
通过 TrackBar 控件,用户可以轻松地调整播放音量。当 TrackBar 的值改变时,我们将更新 Windows Media Player 控件的音量设置。
private void trackBar1_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
}
四、总结
通过结合 Windows Forms 和 NAudio 库,我们能够创建一个简单但功能强大的音乐播放器。这个播放器支持多种音频格式,提供了基本的播放控制,以及音量调节功能。这个项目不仅展示了如何使用 C# 和 Windows Forms 构建 GUI 应用程序,还演示了如何集成第三方库来扩展应用程序的功能。希望这个项目能激发您对音频处理和桌面应用程序开发的兴趣!
代码运行结果:
完整代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio;
using NAudio.Wave;
using NAudio.Vorbis;
namespace WindowsFormsApp4
{
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";
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 label1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
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 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 reader = new VorbisWaveReader(oggFilePath))
{
using (var outputDevice = new WaveOutEvent())
{
outputDevice.Init(reader);
outputDevice.Play();
while (outputDevice.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(100);
}
}
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
}
}
}
clone地址:GitHub - niyuhan666/windows-