一、作业内容
请编写一个C#程序,实现音乐文件的播放功能。
要求1:
1. 程序应能够读取MP3文件,并播放其中的音频。
2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
3. 程序应具有良好的用户界面,方便用户进行操作。
4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。
提示:此功能可以使用WindowsMediaPlayer控件
要求2:
1. 程序应能够播放ogg文件。
2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
3. 程序应具有良好的用户界面,方便用户进行操作。
4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。
提示:此功能可以使用Nuget程序包中的Naudi.Vorbis控件
请确保你的代码是完整且可运行的,并附上必要的注释和说明。
二、功能及窗体设计
音乐播放器的功能应该包含音乐文件获取、显示音乐列表、显示当前音乐、开始播放、停止播放、切换到下一首、调节音量以及播放ogg类型音乐等基本功能。
各功能的实现思路如下:
- 音乐文件获取:可以用openFileDialog1控件以及button1控件来实现。
- 显示音乐列表:可以用listbox1控件来实现。
- 显示当前音乐:可以用label1控件来实现。
- 开始播放、停止播放、调节音量以及切换到下一首:可以用axWindowsMediaPlayer1控件来实现。
为了便捷,其中
- 停止播放:可以用button2控件来实现。
- 切换到下一首:可以用button3控件来实现。
- 调节音量:可以用trackBar1控件来实现。
另外,由于ogg音乐文件需要借助NAudio库来播放,所以可以专门添加button4控件来实现播放ogg音乐文件的功能
按照上述功能可以设计出窗体:
三、难点分析
- 实现不同音乐文件的播放:例如.ogg音乐文件播放需要借助额外的NAudio库来实现播放,这会增加该功能的复杂度。
- 选取多个音乐文件:需要借助list列表来存储多个音乐文件,以此来实现多个音乐文件的选取,并利用索引来确定当前播放的音乐文件。
- 循环播放音乐:细节问题,要考虑到音乐列表的循环播放功能。
- 处理异常: 音乐文件的播放中可能遇到多种异常情况,需要考虑到对应的异常处理。
-
四、代码实现
1.定义音频名称与音频列表
首先定义一个字符串,用于储存音乐名称。
定义一个字符串列表,用于存储选择的多个音乐文件。
string[] files;
List<string> localmusiclist = new List<string> { };
-
2.初始化所有组件
-
public Form1() { InitializeComponent(); }
3.选择MP3音乐文件
在界面中双击选择歌曲,也就是button1,实现选择MP3音乐文件的功能(同时还能更改代码"选择音频|*.mp3"为"选择音频|*.mp3;*.flac;*.wav"选择支持播放.flac和.wav)
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);
}
}
}
-
4.MP3音乐播放
-
可以实现判断音乐文件和.mp3音乐文件播放的功能
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("无法识别的文件格式"); } }
-
5.显示音乐列表和所播放的音乐
-
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]); } }
6.停止播放
-
private void button2_Click(object sender, EventArgs e) { axWindowsMediaPlayer1.Ctlcontrols.stop(); }
7.下一曲
-
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; } }
8.调节音量
-
private void trackBar1_Scroll(object sender, EventArgs e) { axWindowsMediaPlayer1.settings.volume = trackBar1.Value; }
9.播放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); } } } }
-
五、完整代码
- 完整代码如下:
-
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows.Forms; using NAudio.Vorbis; using NAudio.Wave; namespace MusicPlayer { 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 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) { 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#的相关控件以及控件之间的交流,实现了一个音乐播放器,非常有意义。