C# winform框架 音乐播放器开发 联网下载音乐功能的实现原理及源码(纯原创--)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Giser_D/article/details/82914683

首先 ,我做下载音乐功能;主要是为了探究它是怎么实现的;所以界面很丑,不要在意哈----

接下来进入正题:

1.首先: 界面中下载音乐的部分主要是由3个segment组成::

一个textbox,用于输入比如你喜欢的歌曲名/歌手;;

第二个是button1 这是主要做的是联网搜索的功能,里面实现进行的联网搜索一大堆事都在里面弄,最后传给用户的只有一个链接;

第三个是button2 一键下载。主要就是打开这个链接 将音乐保存到本地上来。

接下来详细介绍一下如何实现的了::::

 

(一)textbox就不介绍了 --大家都知道传值进行匹配‘’‘

 

(二)获取下载链接button功能:

      首先你要拖一个web-browser控件进来窗体,主要的有原因就是联网操作,然后web-browser控件可以很方便用户去查看网页,而且这是很重要的一环,然后把visible设为false;设为false的原因是因为这样用户看不到 就显得高大上很多了哈哈哈哈’‘;

然后给web-browser设置url ==;

我这边选用的是http://www.gequdaquan.net/gqss/;最主要的原因是里面的音乐免费~~~’‘

然后调用的类就是HtmlDocument类;;这是我下载音乐功能的方法--不代表其他立场

主要的实现原理就是通过各种鼠标的自动操作。来一步一步的获取的下载链接。。我还是晚上再写把  有点困了我的天 ,,大家有空可以先看一下我的源码

 

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MediaPlayer;

namespace MusicPlayer
{
    public partial class Form1 : Form
    {
        MediaPlayerClass mediaPlayer = new MediaPlayerClass(); //初始化MediaPlayer类;
        WebClient webClient = new WebClient(); //实现下载文件的功能
        string downloadPath ="" ;//下载链接
        string cdPath = "";  //文件夹路径
        Timer timer = new Timer(); //用于定时执行 防止报错;
     
        public Form1()
        {
            InitializeComponent();
        }



 public static string RemoveLeft(string s, int len)
{ 
    return s.PadLeft(len).Remove(0, len); 
 } 

 public static string RemoveRight(string s, int len)
 { 
     s = s.PadRight(len); 
     return s.Remove(s.Length - len, len); 
 }


        private void Form1_Load(object sender, EventArgs e)
        {
            this.skinEngine1.SkinFile = System.Environment.CurrentDirectory + "\\Skins\\Warm.ssk";
            VScrollBar1.Maximum = 100;
            VScrollBar1.Minimum = 0;
            VScrollBar1.Value = 50;
            mediaPlayer.Volume = -5000;  //Volume范围:-10000~0
            WBrowserDownload.Url = new Uri("http://www.gequdaquan.net/gqss/");
            WBrowserDownload.Visible = false;
        }

        /*---界面美化---*/
        public static void SetFormRoundRectRgn(Form form, int rgnRadius)
        {

        }


        //添加音乐文件到列表
        private void Btn_AddFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog { };
            openFileDialog.Title = "打开音乐列表";
            openFileDialog.Filter = "musicfile|*.mp3;*.wav";  //
            openFileDialog.Multiselect = true;
            openFileDialog.RestoreDirectory = false;  //打开还是上次的文件夹
            if (openFileDialog.ShowDialog() == DialogResult.OK)  //if has open file;
            {
                string []pathMusic = openFileDialog.FileNames;  //获得音乐曲库路径
                //遍历音乐列表
                for (int i = 0; i < pathMusic.Length; i++)
                {
                    string[] path = pathMusic[i].Split('\\');
                    cdPath = "";
                    for (int j = 0; j < path.Length; j++)
                    {           
                        if (j < path.Length - 1)
                        {                    
                            cdPath += path[j] + '\\';
                        }
                        if(j == path.Length-1)
                        {
                            pathMusic[i] = path[j];
                            List_Song.Items.Add(pathMusic[i]);
                        }
                    }
                }
            }
        }

        /* --- ui ---*/
        private void Btn_AddFile_MouseMove(object sender, MouseEventArgs e)
        {
            Btn_AddFile.BackColor = Color.Red;
        }

        private void Btn_AddFile_MouseLeave(object sender, EventArgs e)
        {
            Btn_AddFile.BackColor = Color.White;
        }

        //播放音乐功能
        private void Btn_Play_Click(object sender, EventArgs e)
        {
            if (List_Song.SelectedItem == null)
            {
                mediaPlayer.Play();
                int i = mediaPlayer.Volume;
            }
            else
            {
                mediaPlayer.Open(cdPath + List_Song.SelectedItem);
                mediaPlayer.Play();
                mediaPlayer.ShowStatusBar = true;
                // int tet = mediaPlayer.Volume;
                //MessageBox.Show(tet.ToString());

            }
        }

        private void Btn_Pause_Click(object sender, EventArgs e)
        {
            mediaPlayer.Pause();
        }

        private void Btn_Stop_Click(object sender, EventArgs e)
        {
            mediaPlayer.Stop();
        }

        private void Txt_Search_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //搜索框事件
                if (List_Song.Items.Contains(Txt_Search.Text + ".mp3"))
                {
                    mediaPlayer.Open(cdPath + Txt_Search.Text + ".mp3");
                }
            }
        }

        private void Txt_Search_KeyPress(object sender, KeyPressEventArgs e)
        {
         
        }

        private void Txt_Search_TextChanged(object sender, EventArgs e)
        {

        }

        //双击列表播放音乐
        private void List_Song_DoubleClick(object sender, EventArgs e)
        {
            if (List_Song.SelectedItem != null)
            {
                mediaPlayer.Open(cdPath + List_Song.SelectedItem);
                mediaPlayer.Play();
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {

        }

        private void VScrollBar1_ValueChanged(object sender, EventArgs e)
        {
           mediaPlayer.Volume = VScrollBar1.Value - 100;
        }

        private void VScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {

        }

        private void WEBBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

        private void Button1_Click(object sender, EventArgs e)
        {

            HtmlDocument doc = WBrowserDownload.Document;
            /*-- 歌曲搜索部分 --*/
            HtmlElementCollection searchResult = doc.GetElementsByTagName("span");
            HtmlElement searchValue = searchResult[3];
            MessageBox.Show(searchValue.OuterText);
            searchValue.InvokeMember("Click");
            //   MessageBox.Show("正在搜索歌曲-----");
            /*搜索歌曲名*/
            HtmlElementCollection inputCollection = doc.GetElementsByTagName("input");
            HtmlElement inputValue = inputCollection[0];  //搜索框---
            inputValue.InnerText = Txt_SongLink.Text;//模拟搜索光年之外;
             //MessageBox.Show(inputValue.Count.ToString());
            HtmlElementCollection searchButton = doc.GetElementsByTagName("button");
            searchButton[0].InvokeMember("Click"); //执行搜索--;
            MessageBox.Show("正在搜索歌曲::::::");
            for (int k = 0; k < 1000; k++)
            {
                k++;
            }
            MessageBox.Show("搜索歌曲完毕!");

            /*--打开链接--*/
            //HtmlDocument docSearchResult = WBrowserDownload.Document; //得到新的界面
            HtmlElementCollection searchCollection = doc.GetElementsByTagName("span");
            searchCollection[13].InvokeMember("Click");
           // HtmlDocument docDownloadLink = WBrowserDownload.Document; //得到新的界面
            HtmlElementCollection downloadLinkCollection = doc.GetElementsByTagName("span");
            int linkCount = 0;
            for (int i = 0; i < downloadLinkCollection.Count; i++)
            {
                if (downloadLinkCollection[i].OuterText == "外链(手机点我下载)")
                {
                    linkCount = i;
                }
            }
            downloadLinkCollection[linkCount].InvokeMember("Click");   //点击--
            MessageBox.Show("正在获取链接地址::::::");
            int qq = 400;
            for (int j = 0; j <400; j++)
            {
                if (qq == j)
                {
                    MessageBox.Show("正在返回链接地址::::");
                }
            }
            //MessageBox.Show(linkAddress.Count.ToString());
            HtmlElementCollection linkAddress = doc.GetElementsByTagName("input");
            HtmlElement parentElement = linkAddress[0].OffsetParent;
            string pathLinkFull = parentElement.InnerHtml;
            pathLinkFull = pathLinkFull.Replace("value","!");
            pathLinkFull = pathLinkFull.Replace("<p class", "!");
            string  []pathLinkk =  pathLinkFull.Split('!');
            pathLinkk[1] = RemoveLeft(pathLinkk[1],2);
            pathLinkk[1] = RemoveRight(pathLinkk[1],2); //得到路径---
            downloadPath = pathLinkk[1];
            downloadPath = downloadPath.Replace("amp;","");
            MessageBox.Show("下载路径为:" + downloadPath);
            // linkAddress[0].InvokeMember("MouseMove");
        }

        private void Button_MusicDownLoad_Click(object sender, EventArgs e)
        {
            WebBrowser_path.Url = new Uri(downloadPath);
        }

    }
}

 

源码下载链接:https://download.csdn.net/download/giser_d/11212946

展开阅读全文

没有更多推荐了,返回首页