环境: Visual Studio 2022 和 Windows 窗体应用程序 (.NET Framework)
一、需求分析
要实现在线播放音乐器,我们需要音乐API来调用音乐url,要实现在线播放就需要使用 System.Net.Http
类下载和播放流媒体,要下载音乐文件就需要使用HttpClient
类下载音乐文件。
二、创建项目
打开 Visual Studio 2022,创建一个新的 Windows 窗体应用程序项目 (.NET Framework)。
在窗体上添加以下控件:
- 一个
TextBox
控件用于输入音乐 URL。 - 一个
Button
控件用于播放音乐。 - 一个
Button
控件用于下载音乐。 - 一个
Button
控件用于添加到喜爱歌曲列表。 - 一个
ListBox
控件用于显示喜爱歌曲列表。 - 一个
MediaElement
或WindowsMediaPlayer
控件用于播放音乐(需要添加 COM 组件)。
三、代码编写
1.库调用
首先需要引用以下库
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using WMPLib;
using Newtonsoft.Json.Linq;
其中,using System.Net.Http作用
:引用 System.Net.Http
命名空间,提供用于发送 HTTP 请求和接收 HTTP 响应的类,例如 HttpClient
。
using WMPLib作用
:引用 WMPLib
命名空间,提供 Windows Media Player 控件的类和接口,用于在应用程序中嵌入和控制 Windows Media Player。
using Newtonsoft.Json.Linq作用
:引用 Newtonsoft.Json.Linq
命名空间,提供用于处理 JSON 数据的 LINQ to JSON API。
2.API调用
要实现在线播放音乐的功能,必须调用音乐网站的API来实现。具体代码如下:
private async Task SearchSongs(string keyword)
{
using (HttpClient client = new HttpClient())
{
// 替换为实际的搜索API URL
string searchUrl = $"API URL={keyword}";
string searchResponse = await client.GetStringAsync(searchUrl);
// 打印 JSON 响应
MessageBox.Show(searchResponse);
JObject searchJson = JObject.Parse(searchResponse);
// 根据实际的 JSON 结构修改路径
foreach (var song in searchJson["result"]["songs"])
{
string songId = song["id"].ToString();
string songName = song["name"].ToString();
lstResults.Items.Add(songName);
// 获取歌曲URL
string songUrl = $"https://dataiqs.com/api/netease/music/?type=songid&id={songId}";
songUrls.Add(songUrl);
}
}
}
SearchSongs 方法是一个异步方法,用于根据提供的关键词(keyword)搜索歌曲。它通过HTTP请求发送至一个预设的API URL,并接收返回的JSON格式的歌曲数据。然后,它解析这个JSON数据,提取出每个歌曲的名称,并将这些名称添加到列表中。同时,它还为每个搜索到的歌曲生成一个对应的URL,并将这些URL添加到另一个列表中。
3.下载歌曲
int selectedIndex = lstResults.SelectedIndex;
if (selectedIndex >= 0 && selectedIndex < songUrls.Count)
{
string url = songUrls[selectedIndex];
using (HttpClient client = new HttpClient())
{
byte[] data = await client.GetByteArrayAsync(url);
string fileName = "downloaded_song.mp3";
System.IO.File.WriteAllBytes(fileName, data);
MessageBox.Show("下载完成:" + fileName);
}
}
else
{
MessageBox.Show("请选择一首歌曲。");
}
4.添加到喜欢
int selectedIndex = lstResults.SelectedIndex;
if (selectedIndex >= 0 && selectedIndex < songUrls.Count)
{
string songName = lstResults.Items[selectedIndex].ToString();
lstFavorites.Items.Add(songName);
SaveFavorites();
}
else
{
MessageBox.Show("请选择一首歌曲。");
}
private void LoadFavorites()
{
if (System.IO.File.Exists("favorites.txt"))
{
using (System.IO.StreamReader file = new System.IO.StreamReader("favorites.txt"))
{
string line;
while ((line = file.ReadLine()) != null)
{
lstFavorites.Items.Add(line);
}
}
}
}
四:程序实现
在搜索框内输入想听的歌曲,程序自动读取URL。
五、总结
该程序利用 Windows Forms 提供的图形界面,结合 HttpClient
和 JSON 解析,构建了一个简单但功能齐全的在线音乐播放器。通过整合 Windows Media Player 控件,实现了音乐的在线播放和下载功能,同时还支持保存和加载用户的收藏列表,为用户提供了良好的使用体验。