目录
0.题目内容
编写一个C#程序,实现音乐文件的播放功能要求能点开音乐文件,对于MP3文件能进行播放,在列表循环播放下一曲,停止播放等功能。对于ogg文件不需要转码就能进行音乐的播放。
1.准备工作
在工具箱中拖入需要的button,openFileDialog,listbox等等需要的工具。
2.代码实现
2.1打开音乐文件,添加音乐
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "选择音频|*mp3;*.wav;*.flac";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();//将新文件导入,将原文件清空
if (files != null)
{
Array.Clear(files, 0, files.Length);
}
files = openFileDialog1.FileNames;//因为filenames必须为数组
string[] array = files;
foreach (string x in array)
{
listBox1.Items.Add(x);
localmusiclist.Add(x);
}
}
}
2.2导入文件后显示在列表
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停止音乐
private void button2_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
2.4下一曲
在这里设置了索引超过音乐文件数量,就对索引进行重置,保证在列表循环播放,防止出现下标超过索引,导致的无法播放问题 。
private void button3_Click(object sender, EventArgs e)
{
int nextIndex = listBox1.SelectedIndex;
if (localmusiclist.Count > 0)
{
if (listBox1.SelectedIndex + 1 > localmusiclist.Count)
{
nextIndex = 0;
}
axWindowsMediaPlayer1.URL = localmusiclist[nextIndex];
musicplay(axWindowsMediaPlayer1.URL);
label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[nextIndex]);
listBox1.SelectedIndex = nextIndex;
}
}
2.5显示音量
为了界面的美观,设置了一个调设音量大小的组件,并且在旁边的label组件中显示音量的具体大小
private void trackBar1_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
label2.Text = trackBar1.Value + "%";
}
2.6播放ogg文件格式音乐
在这里用到了库NAudio ,需要安装这个库,下方代码才可运行。
private void button4_Click(object sender, EventArgs e)
{
string oggFilePath = "";
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "播放音频|*.ogg";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
oggFilePath = openFileDialog.FileName;
}
using (var vorbis = new VorbisWaveReader(oggFilePath))
{
using (var waveOut = new WaveOutEvent())
{
waveOut.Init(vorbis);
waveOut.Play();
while (waveOut.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(1000);
}
}
}
}
2.7窗体变量的含义
在 Form1 的窗体类中定义包含了一个成员变量 files,一个方法 musicplay 和一个构造函数。
- files 是一个字符串数组,用于存储文件路径。
- localmusiclist 是一个 List<string>,用于存储本地音乐文件的路径。
- Form1 构造函数是类的初始化方法,通常用于设置窗体的初始状态和加载必要的资源。
- musicplay 方法用于播放音乐文件。它接受一个文件名作为参数,并根据文件的扩展名来判断文件类型。如果文件的扩展名是 ".ogg",则在控制台输出 "this is ogg file";否则,调用 axWindowsMediaPlayer1.Ctlcontrols.play() 方法播放音乐。
string[] files;
List<string> localmusiclist = new List<string> { };
public Form1()
{
InitializeComponent();
}
private void musicplay(string filename)
{
string extension = Path.GetExtension(filename);//fanhuiwenjiandekuozhanhouzhuiming
if (extension == ".ogg") { Console.WriteLine("这是ogg文件"); }
else
{
Console.WriteLine("这不是ogg文件");
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
3.部分运行结果显示
下面是播放ogg文件,点击播放ogg的button后,显示的添加文件界面与上一个添加MP3的有所不同,点击后即可播放。
4.小结
在本次的项目中,对于ogg文件的处理借助了AI工具的帮助,在AI的提示下安装了相应的库,最终完成代码。
5.完整代码展示
using NAudio.Wave;
using NAudio.Vorbis;
using System;
using System.IO;
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;
namespace MusicPlay
{
public partial class Form1 : Form
{
string[] files;
List<string> localmusiclist = new List<string> { };
public Form1()
{
InitializeComponent();
}
private void musicplay(string filename)
{
string extension = Path.GetExtension(filename);//fanhuiwenjiandekuozhanhouzhuiming
if (extension == ".ogg") { Console.WriteLine("这是ogg文件"); }
else
{
Console.WriteLine("这不是ogg文件");
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "选择音频|*mp3;*.wav;*.flac";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();//将新文件导入,将原文件清空
if (files != null)
{
Array.Clear(files, 0, files.Length);
}
files = openFileDialog1.FileNames;//因为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;
label2.Text = trackBar1.Value + "%";
}
private void button2_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
private void button3_Click(object sender, EventArgs e)
{
int nextIndex = listBox1.SelectedIndex;
if (localmusiclist.Count > 0)
{
if (listBox1.SelectedIndex + 1 > localmusiclist.Count)
{
nextIndex = 0;
}
axWindowsMediaPlayer1.URL = localmusiclist[nextIndex];
musicplay(axWindowsMediaPlayer1.URL);
label1.Text = Path.GetFileNameWithoutExtension(localmusiclist[nextIndex]);
listBox1.SelectedIndex = nextIndex;
}
}
private void button4_Click(object sender, EventArgs e)
{
string oggFilePath = "";
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "播放音频|*.ogg";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
oggFilePath = openFileDialog.FileName;
}
using (var vorbis = new VorbisWaveReader(oggFilePath))
{
using (var waveOut = new WaveOutEvent())
{
waveOut.Init(vorbis);
waveOut.Play();
while (waveOut.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(1000);
}
}
}
}
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
}
}
}