1. 作业内容
编写一个C#程序,要求实现常见音乐文件的播放功能,具体要求如下:
1. 播放MP3文件: 程序应能够读取MP3文件,并播放其中的音频。
2. 播放OGG文件: 应能够播放ogg文件。
3. 用户界面: 一个简单的窗口,包含播放和暂停按钮以及文件选择功能。
4. 异常处理: 程序应能够处理可能出现的异常,如文件不存在、文件读取错误等。
5. 兼容性: 确保代码在多个C#版本上均可运行。
2. 设计思路
1. 界面设计:使用Windows Forms工具箱中的控件(按钮、标签、对话等)框构建用户界面。
2. 功能编码: 允许用户选择音频文件。 根据文件扩展名调用相应的播放器:对象进行播放。 播放控制,包括开始、暂停和停止。
3. 窗体设计
3.1窗体如下
3.2各按钮功能
listBox1:显示音乐文件列表,用户可以选择要播放的音乐文件。
selectFilesButton:允许用户选择本地的音乐文件,并将选择的文件添加到列表框中。
label1: 用于显示当前播放的音乐文件名,以提供用户反馈。
trackBar1: 用于控制音乐的播放进度。
button2:用于控制音乐进程,播放音乐。
button3:用于控制音乐进程,暂停音乐。
axWindowsMediaPlayer1: 控件用于播放音乐文件,提供了音乐播放的功能。
3.3功能编码
1、文件选择功能:
SelectFilesButton_Click方法处理了选择文件按钮的点击事件,使用文件选择对话框允许用户选择音乐文件,并将选择的文件添加到列表框中。
2. 音乐播放功能:
PlayMusic方法用于播放音乐文件,根据文件的扩展名判断是否为.ogg文件,如果不是则使用 axWindowsMediaPlayer1控件进行播放。
3. 音乐文件选择和播放:
ListBox1_SelectedIndexChanged方法处理了列表框选中项改变事件,根据用户选择的音乐文件路径设置 axWindowsMediaPlayer1控件的 URL,并播放音乐,同时更新标签 label1显示当前播放的音乐文件名。
4.代码实现
4.1窗体显示
using NAudio.Wave;
using NAudio.Vorbis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace WindowsMusicApp
{
public partial class MainForm : Form
{
private string[] selectedFiles;
private readonly List<string> localMusicList = new List<string>();
public MainForm()
{
InitializeComponent();
}
private void PlayMusic(string filename)
{
string extension = Path.GetExtension(filename);
if (extension != ".ogg")
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
private void SelectFilesButton_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "选择音频|*.mp3;*.wav;*.flac";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
localMusicList.Clear();
selectedFiles = openFileDialog1.FileNames;
foreach (string file in selectedFiles)
{
listBox1.Items.Add(file);
localMusicList.Add(file);
}
}
}
private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (localMusicList.Count > 0)
{
axWindowsMediaPlayer1.URL = localMusicList[listBox1.SelectedIndex];
PlayMusic(axWindowsMediaPlayer1.URL);
label1.Text = Path.GetFileNameWithoutExtension(localMusicList[listBox1.SelectedIndex]);
}
}
private void PlayOggFileButton_Click(object sender, EventArgs e)
{
string oggFilePath = "path_to_your_ogg_file.ogg"; // 替换为您的OGG文件路径
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "播放音频|*.ogg";
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);
}
}
}
}
}
}
4.1.1功能解释:
1. 成员变量定义:
-`selectedFiles`:用于存储用户选择的音频文件的路径数组。
-`localMusicList`:用于存储本地音乐文件的路径列表。
2. 构造函数 `MainForm()`:
-初始化窗体。
3. 方法 `PlayMusic(string filename)`:
-根据文件扩展名判断是否为 `.ogg` 文件。
-如果不是 `.ogg` 文件,则使用 `axWindowsMediaPlayer1` 控件播放音乐。
4. 事件处理方法 `SelectFilesButton_Click(object sender, EventArgs e)`:
- 处理“选择文件”按钮的点击事件。
- 设置文件选择对话框的筛选条件为 `.mp3`、`.wav` 和 `.flac` 格式的音频文件。
- 如果用户选择了文件,将清空音乐列表和已选择文件数组,并将用户选择的文件添加到列表框中和本地音乐列表中。
5. 事件处理方法 `ListBox1_SelectedIndexChanged(object sender, EventArgs e)`:
- 处理列表框选中项改变事件。
- 如果本地音乐列表中有音乐文件,根据用户选择的文件路径设置 `axWindowsMediaPlayer1` 控件的 URL,并播放音乐,并更新标签 `label1` 显示当前播放的音乐文件名(不包括扩展名)。
6. 事件处理方法 `PlayOggFileButton_Click(object sender, EventArgs e)`:
- 处理播放 `.ogg` 文件按钮的点击事件。
- 弹出文件选择对话框,筛选条件为 `.ogg` 格式的音频文件。
- 如果用户选择了文件,创建 `VorbisWaveReader` 对象读取 `.ogg` 文件,然后使用 `WaveOutEvent` 对象初始化输出设备并播放音频。
4.2音乐播放器界面,并提供了选择音乐文件、播放、暂停、控制播放进度和播放 `.ogg` 文件的功能。
namespace WindowsMusicApp
{
partial class MainForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.listBox1 = new System.Windows.Forms.ListBox();
this.selectFilesButton = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.label1 = new System.Windows.Forms.Label();
this.trackBar1 = new System.Windows.Forms.TrackBar();
this.label2 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.playOggFileButton = new System.Windows.Forms.Button();
this.axWindowsMediaPlayer1 = new AxWMPLib.AxWindowsMediaPlayer();
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).BeginInit();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 21;
this.listBox1.Location = new System.Drawing.Point(69, 73);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(320, 193);
this.listBox1.TabIndex = 0;
//
// selectFilesButton
//
this.selectFilesButton.Location = new System.Drawing.Point(69, 286);
this.selectFilesButton.Name = "selectFilesButton";
this.selectFilesButton.Size = new System.Drawing.Size(127, 36);
this.selectFilesButton.TabIndex = 1;
this.selectFilesButton.Text = "选择音频";
this.selectFilesButton.UseVisualStyleBackColor = true;
this.selectFilesButton.Click += new System.EventHandler(this.SelectFilesButton_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(455, 73);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(59, 21);
this.label1.TabIndex = 2;
this.label1.Text = "label1";
//
// trackBar1
//
this.trackBar1.Location = new System.Drawing.Point(69, 350);
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(320, 45);
this.trackBar1.TabIndex = 3;
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(455, 286);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(59, 21);
this.label2.TabIndex = 4;
this.label2.Text = "label2";
//
// button2
//
this.button2.Location = new System.Drawing.Point(239, 286);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 36);
this.button2.TabIndex = 5;
this.button2.Text = "播放";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(357, 286);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 36);
this.button3.TabIndex = 6;
this.button3.Text = "暂停";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// playOggFileButton
//
this.playOggFileButton.Location = new System.Drawing.Point(455, 350);
this.playOggFileButton.Name = "playOggFileButton";
this.playOggFileButton.Size = new System.Drawing.Size(75, 36);
this.playOggFileButton.TabIndex = 7;
this.playOggFileButton.Text = "播放OGG";
this.playOggFileButton.UseVisualStyleBackColor = true;
this.playOggFileButton.Click += new System.EventHandler(this.PlayOggFileButton_Click);
//
// axWindowsMediaPlayer1
//
this.axWindowsMediaPlayer1.Enabled = true;
this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(455, 151);
this.axWindowsMediaPlayer1.Name = "axWindowsMediaPlayer1";
this.axWindowsMediaPlayer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWindowsMediaPlayer1.OcxState")));
this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(320, 204);
this.axWindowsMediaPlayer1.TabIndex = 8;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 21F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.axWindowsMediaPlayer1);
this.Controls.Add(this.playOggFileButton);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.label2);
this.Controls.Add(this.trackBar1);
this.Controls.Add(this.label1);
this.Controls.Add(this.selectFilesButton);
this.Controls.Add(this.listBox1);
this.Name = "MainForm";
this.Text = "MainForm";
4.2.1功能解释
1. 选择音频文件功能:
- 用户点击 `selectFilesButton` 按钮,触发 `SelectFilesButton_Click` 方法。
- 在方法中弹出文件选择对话框 `openFileDialog1`,允许用户选择音频文件。
- 用户选择文件后,将文件路径添加到 `listBox1` 列表框中,并显示在界面上。
2. 播放功能:
- 用户点击 `button2` 播放按钮,触发 `button2_Click` 方法。
- 方法中调用 `axWindowsMediaPlayer1.Ctlcontrols.play()` 方法播放当前选中的音频文件。
3. 暂停功能:
- 用户点击 `button3` 暂停按钮,触发 `button3_Click` 方法。
- 方法中调用 `axWindowsMediaPlayer1.Ctlcontrols.pause()` 方法暂停当前正在播放的音频文件。
4. 控制播放进度功能:
- 用户通过滑动 `trackBar1` 滑动条来控制音乐的播放进度。
- 当用户拖动滑动条时,触发 `trackBar1_Scroll` 方法。
- 方法中将滑动条的值设置为当前播放位置,通过 `axWindowsMediaPlayer1.Ctlcontrols.currentPosition` 属性控制音乐播放进度。
5. 播放 `.ogg` 文件功能:
- 用户点击 `playOggFileButton` 播放OGG按钮,触发 `PlayOggFileButton_Click` 方法。
- 方法中弹出文件选择对话框,允许用户选择 `.ogg` 格式的音频文件。
- 选择文件后,使用 `VorbisWaveReader` 和 `WaveOutEvent` 类来播放 `.ogg` 文件。
4.3程序入口
using System;
using System.Windows.Forms;
namespace WindowsMusicApp
{
internal static class EntryPoint
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
5.完整代码
代码仓库链接见评论区