1.作业内容:
编写一个C#程序,在作业二实现的本地播放功能的基础上,新增在线播放和在线下载功能。
2.作业要求:
- 能够读取mp3,wav,flac,ogg类型的文件并播放。
- 程序能够处理可能出现的异常,如文件不存在、文件读取错误等。
- 程序具有良好的用户界面,方便用户进行操作。
- 能够下载网络音乐。
3.代码实现:
using NAudio;
using NAudio.Wave;
using NAudio.Vorbis;
using NVorbis.Contracts;
namespace 音乐播放
{
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 extention = Path.GetExtension(filename);
if (extention == ".ogg")
{
Console.WriteLine("这是ogg文件");
listBox1.Items.Add(filename); // 将选中的ogg文件路径添加到列表框
localmusicList.Add(filename);
}
else
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "选择音频|*.mp3;*.flac;*.wav";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
localmusicList.Clear();
listBox1.Items.Clear();//清理控件
if (files != null)
{
Array.Clear(files, 0, files.Length);
}//清理文件
files = openFileDialog1.FileNames;
string[] arry = files;
foreach (string x in arry)
{
listBox1.Items.Add(x);
localmusicList.Add(x);
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (localmusicList.Count > 0)
{
int selectedIndex = listBox1.SelectedIndex;
if (selectedIndex >= 0)
{
string selectedFile = localmusicList[selectedIndex];
if (!selectedFile.EndsWith(".ogg"))
{
axWindowsMediaPlayer1.URL = selectedFile;
musicplay(axWindowsMediaPlayer1.URL);
label1.Text = Path.GetFileNameWithoutExtension(selectedFile);
}
}
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
}
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 button5_Click(object sender, EventArgs e)
{
OpenFileDialog openFiledialog = new OpenFileDialog();
openFiledialog.Filter = "打开音频|*.ogg";
string oggFilePath = "";
if (openFiledialog.ShowDialog() == DialogResult.OK)
{
oggFilePath = openFiledialog.FileName;
listBox1.Items.Add(oggFilePath); // 将选择的ogg文件路径添加到列表框
localmusicList.Add(oggFilePath); // 将选择的ogg文件路径添加到列表
Task.Run(() =>
{
using (var vorbis = new VorbisWaveReader(oggFilePath))
{
using (var outputDevice = new WaveOutEvent())
{
outputDevice.Init(vorbis);
outputDevice.Play();
// 等待播放结束,而不是使用Thread.Sleep进行轮询
while (outputDevice.PlaybackState == PlaybackState.Playing)
{
Thread.Sleep(100); // 减少轮询频率以降低CPU使用率
}
}
}
});
}
}
}
}
代码分析多线程处理音频播放:① 在按钮5的点击事件处理方法中,使用了 Task.Run() 方法创建了一个异步任务,用于播放 .ogg 格式的音频文件。这涉及到多线程编程,需要确保在异步任务中正确地初始化音频播放设备,并且需要处理音频播放的状态。
②文件格式处理: 在 musicplay 方法中,针对不同的音频文件格式(如 .ogg)做了不同的处理。这需要对不同的音频文件格式有一定的了解,以便正确地调用相应的播放方法或处理方法。
③用户界面交互: 对话框的使用、列表框的更新、滑动条的操作等涉及用户界面的交互操作,需要确保界面的响应性和用户体验。
④异常处理: 对异常情况
private async void button3_Click(object sender, EventArgs e)
{
if (selectedIndex == -1 || selectedIndex >= responseLines.Count)
{
MessageBox.Show("请选择要播放的歌曲");
return;
}
string playUrl = await GetPlayUrlAsync(selectedIndex); // 调用获取播放链接的异步方法
if (!string.IsNullOrEmpty(playUrl))
{
PlayAudio(playUrl); // 调用播放音乐的方法
}
else
{
MessageBox.Show("未找到播放链接");
}
}
public void PlayAudio(string url)
{
axWindowsMediaPlayer1.URL = url;
axWindowsMediaPlayer1.Ctlcontrols.play();
}
private async void button2_Click(object sender, EventArgs e)
{
if (responseLines.Count > 0)
{
int index = (selectedIndex + 1);
if (index >= responseLines.Count())
{
index = 0;
}
string playUrl = await GetPlayUrlAsync(index); // 调用获取播放链接的异步方法
PlayAudio(playUrl);
selectedIndex = index;
listBox1.SelectedIndex = index - 1;
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
}
private void button4_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop(); // 停止播放
}
用户点击播放按钮,调用button3_Click
事件处理程序,先检查是否有选中的歌曲,并异步调用GetPlayUrlAsync
方法获取播放链接,将链接传入PlayAudio
函数中调用AxWindowsPlayer
控件实现在线播放功能。
4.功能实现: