一、代码目标
请编写一个C#程序,实现音乐文件的播放功能。
要求1:
1. 程序应能够读取MP3文件,并播放其中的音频。
2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
3. 程序应具有良好的用户界面,方便用户进行操作。
4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。
提示:此功能可以使用WindowsMediaPlayer控件
要求2:
1. 程序应能够播放ogg文件。
2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
3. 程序应具有良好的用户界面,方便用户进行操作。
4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。
提示:此功能可以使用Nuget程序包中的Naudi.Vorbis控件
二、问题分析
1.播放器组成成分
要播放音乐,具体步骤为选择音乐并添加至歌曲列表,播放歌曲,除了基础步骤外,我们还应该具有切换下一首歌曲、控制音量大小的功能。根据我们需要的功能,我们大致需要一个歌曲列表、控制音乐播放的控件、音量调节以及按钮来触发不同的功能。
2.难点分析及注意事项
首先需要注意的是WindowsMediaPlayer控件是Windows Media Player的ActiveX控件的封装,它可能不支持所有类型的音频文件,特别是Ogg格式的音频文件。所以,对于Ogg格式的音频文件,我们需要使用一个支持该格式的库。即Nuget程序包中的Naudi.Vorbis控件。其次要注意的点为处理可能出现的异常情况。
三、具体思路
1.大致布局
模拟一个音乐播放器,我们需要一个公共空间Listbox来模拟歌曲列表、一个WindowsMediaPlayer控件来控制音乐的播放与暂停、一个TrackBar来实现音量调节、多个控件button来触发功能、还有两个对话框openFileDialog来实现从个人文档中调入歌曲。
页面布局如下:
2.具体步骤
我们可以根据操作顺序一步一步来设置:
2.1选择歌曲并添加到列表
2.1.1需要用到的部件
button、openFileDialog、listbox
2.1.2思路
1.设置文件对话框可以显示的文件属性;
2.显示文件对话框并处理结果;
3.清空现有列表和数组;
4.获取选择的文件路径并添加到列表和列表框中;
5.这里还要处理可能出现的异常,如文件不存在、文件读取错误等,我选择使用try-catch语句来处理。
2.1.3代码
private void button1_Click(object sender, EventArgs e)
{
try{
openFileDialog1.Filter = "选择音频|*.mp3;*.flac;*.wav";
//使文件对话框其只显示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);
}
//将歌曲添加至列表中
}
}
catch (Exception ex)
{
MessageBox.Show("发生了一个错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//选择歌曲
2.2开始播放
2.2.1需要用到的部件
listbox
2.2.2思路
1.检查localmusiclist是否有内容
2.获取选中的音频文件路径
3.设置媒体播放器的播放URL
4.调用musicplay函数
5.更新label1的文本,显示正在播放的歌曲
2.2.3代码
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]);
}
}
2.3下一曲
2.3.1需要用到的部件
button
2.3.2思路
1.检查localmusiclist是否有内容
2.计算下一首音乐的索引
3.处理列表末尾的情况
4.设置媒体播放器的播放URL
5.调用musicplay函数
6.更新label1的文本,显示正在播放的歌曲
7.更新列表框的选中项
2.3.3代码
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;
}
}
2.4停止播放
2.4.1需要用到的部件
button
2.4.2思路
用函数停止播放。
2.4.3代码
private void button2_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
2.5调节音量
2.5.1需要用到的部件
trackBar
2.5.2思路
用一个函数使windowsMediaPlayer音量的数值与trackBar中的数值相等
2.5.3代码
private void trackBar1_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
}
2.6播放ogg文件
2.6.1需要用到的部件
button、openFileDialog
2.6.2思路
1.想要播放ogg文件,需要先导入库,即Nuget程序包中的Naudi.Vorbis控件。
2.初始化变量
3.创建文件选择对话框
使用OpenFileDialog类来创建一个文件选择对话框,该对话框允许用户从文件系统中选择一个文件。设置Filter属性为"打开音频|*.ogg",这限制了用户只能选择OGG格式的音频文件。
4.显示文件选择对话框
5.获取用户选择的文件路径
如果用户选择了文件并确认了选择,将openFileDialog.FileName的值赋给oggFilePath变量,从而更新为用户选择的文件路径。
6.由于OGG不是AudioFileReader直接支持的格式,使用VorbisWaveReader播放OGG文件:
7.这里还要处理可能出现的异常,如文件不存在、文件读取错误等,我选择使用try-catch语句来处理。
2.6.3代码
using NAudio;
using NAudio.Wave;
using NAudio.Vorbis;
//导入库
private void button4_Click(object sender, EventArgs e)
{
string oggFilePath = @"D:\a windows程序设计"; // 替换为你的OGG文件路径
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "打开音频|*.ogg";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
oggFilePath = openFileDialog.FileName;
}
try
{
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);
}
}
}
}
catch (Exception ex) // 捕获其他所有异常
{
MessageBox.Show("发生了一个未知错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
3.完整代码
using NAudio;
using NAudio.Wave;
using Un4seen.Bass;
using NAudio.Vorbis;
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 System.Windows.Forms.VisualStyles;
namespace WindowsFormsApp1
{
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 button1_Click(object sender, EventArgs e)
{
try
{
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);
}
}
}
catch (Exception ex)
{
MessageBox.Show("发生了一个错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
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 trackBar1_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
}
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)
{
string oggFilePath = @"D:\a windows程序设计"; // 替换为你的OGG文件路径
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "打开音频|*.ogg";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
oggFilePath = openFileDialog.FileName;
}
try
{
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);
}
}
}
}
catch (Exception ex) // 捕获其他所有异常
{
MessageBox.Show("发生了一个未知错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}