赞
踩
1. 程序应能够读取MP3文件,并播放其中的音频。
2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
3. 程序应具有良好的用户界面,方便用户进行操作。
4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。
提示:此功能可以使用WindowsMediaPlayer控件
1. 程序应能够播放ogg文件。
2. 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
3. 程序应具有良好的用户界面,方便用户进行操作。
4. 程序应具有良好的兼容性,能在不同版本的C#中正常运行。
提示:此功能可以使用Nuget程序包中的Naudi.Vorbis控件
考虑到要求中的“对用户界面友好”和“跨版本兼容性”,我们选择visual studio 2022中的windows Forms(.NETFramework)作为项目。
根据题目要求选择需要的控如下:
OpenFileDialog:使用户选择音频文件,根据文件扩展名调用相应的播放器对象进行播放,可以选择播放开始,暂停,停止。
listbox:作为显示选中的音乐播放列表
label:显示当前播放的音乐
axWindowsMediaPlayer:专门使用WindowsMediaPlayer控件做音乐播放。
trackBar:作为一个音量控制器
button:作为多个对音乐操作控制的按钮触发事件
ps:为了美化一点界面我还插入了图片
结果如下:
- //选择音乐文件
- string[] files;
- List<string > localmusiclist=new List<string> { };
- private VorbisWaveReader currentVorbisReader;
- private void button1_Click(object sender, EventArgs e)
- {
- openFileDialog1.Filter = "选择音频|*.mp3;*.flac;*.wav";//先过滤,选择我要的文件
- openFileDialog1.Multiselect = true;//多选属性,打开文件对话框,可以同时选择多个我要的文件
-
- if(openFileDialog1.ShowDialog() == DialogResult.OK)//对话框'ok'='选择确定'
- {
- localmusiclist.Clear();
- listBox1.Items.Clear();//清控件,把文件放进播放器,但是要先清空,以免上次的结果影响这次
-
- if (files != null)
- {
- Array.Clear(files,0, files.Length);//清文件,把上一次的结果清空
- }
-
- files= openFileDialog1.FileNames;//所有符合要求的文件的文件名
- string[] array = files;//这些文件名分别作为字符串作为array数组的每一元素
- foreach(string file in array )
- {
- listBox1.Items.Add(file);//列表项,返回int值的下标
- localmusiclist.Add(file);//歌单,是string 的列表形式
- }
- }
- }

- //显示播放音乐的列表
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e)//listbox1的事件响应
- {
- if(localmusiclist.Count>0)//判断歌单有没有各歌
- {
- //把索引号放过来
- axWindowsMediaPlayer1.URL = localmusiclist[listBox1.SelectedIndex];//[]内需要一个int类型的索引
- // axWindowsMediaPlayer1.Ctlcontrols.play()这里为了可以处理多种类型的文件二写成一个处理函数
- yytmusicplayer(axWindowsMediaPlayer1.URL);
- label1.Text =Path.GetFileNameWithoutExtension(localmusiclist[listBox1.SelectedIndex]);//把正在播放的歌曲名字放出来,传入大参数是被选中的歌
- }
- }
- //控制划尺大小和音量有关
- private void trackBar1_Scroll(object sender, EventArgs e)
- {
- //设置值
- axWindowsMediaPlayer1.settings.volume=trackBar1.Value;//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];
-
- yytmusicplayer(axWindowsMediaPlayer1.URL);
- label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[index]);
-
- listBox1.SelectedIndex = index;
- }
- }

- //播放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;
-
- }
- // string oggFilePath = @"C:\path\to\your\file.ogg"; // 替换为您的OGG文件路径
- 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 (var vorbisStream = new VorbisWaveReader(oggFilePath))
- {
- using (var outputDevice = new WaveOutEvent())
- {
- outputDevice.Init(vorbisStream);
- outputDevice.Play();
-
- //Console.WriteLine("Press any key to stop playback...");
- //Console.ReadKey();
-
- //outputDevice.Stop();
- while (outputDevice.PlaybackState == PlaybackState.Playing)
- {
- System.Threading.Thread.Sleep(1000);
- }
- }
-
- }
-
- }
-
- }

代码仓库:Windows程序设计作业: 放作业
在完成这次作业的过程中,我深入了解了音频文件的播放和异常处理的相关知识,同时也对C#的Windows Forms应用程序开发有了更实际的操作体验。
以下是我具体的掌握方面:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。