目录
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;
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 + 1;
if (nextIndex >= 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 = "path_to_your_ogg_file.ogg";
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()
方法播放音乐。
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);
if (extension == ".ogg") { Console.WriteLine("this is ogg file"); }
else
{
Console.WriteLine("not ogg");
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
}
3.部分运行结果展示
下面是播放ogg文件,点击播放ogg的button后,显示的添加文件界面与上一个添加MP3的有所不同,点击后即可播放。
4.小结
在本次的项目中,对于ogg文件的处理借助了AI工具的帮助,在AI的提示下安装了相应的库,最终完成代码。
5.完整代码展示
using NAudio.Wave;
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;
namespace Windowmusic
{
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);
if (extension == ".ogg") { Console.WriteLine("this is ogg file"); }
else
{
Console.WriteLine("not 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;
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 + 1;
if (nextIndex >= 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 = "path_to_your_ogg_file.ogg";
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);
}
}
}
}