微信音樂小程序開發實踐

使用微信小程序實現我的音樂播放平臺node

 

一,效果展現git

                     

二,實現的功能github

1,音樂播放,暫停,切換,歌詞同步展現。sql

2,音樂收藏,根據搜索和播放歷史推薦,播放排行榜展現。數據庫

 

三,數據庫設計小程序

1,基於功能需求,設計概念模型微信小程序

2,概念模型向邏輯關係模型轉化微信

轉化的通常原則:數據庫設計

1) 一個1:1聯繫能夠轉換爲一個獨立的關係模式,也能夠與任意的一端對應的關係模式合併。ide

2)一個1:n的聯繫能夠轉換爲一個獨立的關係模式,也能夠與n端對應的關係模式合併。

3) 一個n:m聯繫轉化爲一個關係模式,與該聯繫相連的各實體的碼以及聯繫自己的屬性均轉換爲關係的屬性。

4)具備相同碼的關係模式合併。

爲避免關係模式出現如下問題:數據冗餘,更新異常,插入異常,刪除異常等。關係模式須要符合一些基本的條件。

1) 每個份量必須是不可分割的數據項(1NF)

2) 且,每個非主屬性徹底依賴於任何一個候選碼(2NF)

3) 且每個決定因素都包含碼(BCNF)

轉換結果以下:

 

四,實際開發總結

一)提取音樂信息

後臺語言使用nodejs,使用jsmediatags模塊提取音樂封面,歌手,專輯等信息。

二)歌詞編碼類型轉換

nodejs讀取非utf8編碼類型的歌詞出現亂碼,須要把gb2312的歌詞轉換成utf8格式。

使用.net core讀取音樂文件夾,生成音樂資源腳本文件(sql),並把編碼格式gb2312歌詞文件轉換成utf8格式。

using System;
using System.IO;
using System.Text;

namespace readmysic
{
    class Program
    {


        //生成音樂sql腳本
    static void Main(string[] args)
        {
          //  string path = "G:\\ftpmusic";

          //  string path2 = "G:\\已上傳\\";
            string path = "G:\\ftpmusic"; //本地音樂文件路徑
            int musicid = 1;//插入數據庫表(music)主鍵開始id
            string sql = "";
            DirectoryInfo filepath = new DirectoryInfo(path);
            FileInfo[] files = filepath.GetFiles();
            foreach(FileInfo f in files){
               
                string tex = f.Extension;

                string name = f.Name;

                if(tex == ".mp3"){
                    string temp = name.Split('.')[0];
                    string title = "";
                    string songer = "";
                    if(temp.IndexOf('-') > 0){
                        songer = temp.Split('-')[0];
                        title =  temp.Split('-')[1];
                    }else{
                        title =  temp;
                    }
                    string temsql  = "insert into music(id,name,title,songer,totalPlay) VALUES ({0},'{1}','{2}','{3}',0);\n";
                    temsql = string.Format(temsql,musicid,name,title,songer);
                    sql+=temsql;
                    musicid++;
                }
                //刪除重複文件
               /*  string  filetem = path2 + name;
                if(File.Exists(filetem)){
                    Console.WriteLine(name);
                    File.Delete(filetem);
                } */

                //編碼格式轉換
                if(f.Extension == ".lrc"){
                    FileStream fs = new FileStream(f.FullName,FileMode.Open);
                    Encoding tr =  GetType(fs);
                    Console.WriteLine(name+tr.BodyName);
                    if(tr.BodyName == "gb2312"){
                        gb2312toutf8(f.FullName);
                    }
                }
             

                
            }
            Console.Write(sql);
            string sqlname = "G:\\ftpmusic\\temsql.txt";//生成的腳本文件地址
            Writesql(sqlname,sql);

        }
    
    
    /// <summary> 
    /// 給定文件的路徑,讀取文件的二進制數據,判斷文件的編碼類型 
    /// </summary> 
    /// <param name="FILE_NAME">文件路徑</param> 
    /// <returns>文件的編碼類型</returns> 
    public static System.Text.Encoding GetType(string FILE_NAME) 
    { 
        FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read); 
        Encoding r = GetType(fs); 
        fs.Close(); 
        return r; 
    } 
 
    /// <summary> 
    /// 經過給定的文件流,判斷文件的編碼類型 
    /// </summary> 
    /// <param name="fs">文件流</param> 
    /// <returns>文件的編碼類型</returns> 
    public static System.Text.Encoding GetType(FileStream fs) 
    { 
        byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 }; 
        byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 }; 
        byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //帶BOM 
        Encoding reVal = Encoding.Default; 
 
        BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default); 
        int i; 
        int.TryParse(fs.Length.ToString(), out i); 
        byte[] ss = r.ReadBytes(i); 
        if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF)) 
        { 
            reVal = Encoding.UTF8; 
        } 
        else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00) 
        { 
            reVal = Encoding.BigEndianUnicode; 
        } 
        else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41) 
        { 
            reVal = Encoding.Unicode; 
        }
        else if(ss[0] == 91 && ss[1] == 116 && ss[2] == 105){
            System.Text.Encoding.RegisterProvider (System.Text.CodePagesEncodingProvider.Instance);
            reVal = Encoding.GetEncoding("GB2312"); 
        }
        r.Close(); 
        return reVal; 
 
    } 
 
    /// <summary> 
    /// 判斷是不是不帶 BOM 的 UTF8 格式 
    /// </summary> 
    /// <param name="data"></param> 
    /// <returns></returns> 
    private static bool IsUTF8Bytes(byte[] data) 
    { 
        int charByteCounter = 1; 
          //計算當前正分析的字符應還有的字節數 
        byte curByte; //當前分析的字節. 
        for (int i = 0; i < data.Length; i++) 
        { 
            curByte = data[i]; 
            if (charByteCounter == 1) 
            { 
                if (curByte >= 0x80) 
                { 
                    //判斷當前 
                    while (((curByte <<= 1) & 0x80) != 0) 
                    { 
                        charByteCounter++; 
                    } 
                    //標記位首位若爲非0 則至少以2個1開始 如:110XXXXX...........1111110X  
                    if (charByteCounter == 1 || charByteCounter > 6) 
                    { 
                        return false; 
                    } 
                } 
            } 
            else 
            { 
                //如果UTF-8 此時第一位必須爲1 
                if ((curByte & 0xC0) != 0x80) 
                { 
                    return false; 
                } 
                charByteCounter--; 
            } 
        } 
        if (charByteCounter > 1) 
        { 
            throw new Exception("非預期的byte格式"); 
        } 
        return true; 
    } 
   

    /// <summary> 
    /// 實現gb2312歌詞轉utf8
    /// </summary> 
    /// <param name="path">歌詞路徑</param> 
    private static void gb2312toutf8(string path) 
    { 
    
        var data = File.ReadAllBytes(path);
        System.Text.Encoding.RegisterProvider (System.Text.CodePagesEncodingProvider.Instance);
        data = Encoding.UTF8.GetBytes(Encoding.GetEncoding("GB2312").GetString(data));

        File.WriteAllBytes(path, data);
    } 

        //生成腳本文件
         public static void Writesql(string path,string sql)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //開始寫入
            sw.Write(sql);
            //清空緩衝區
            sw.Flush();
            //關閉流
            sw.Close();
            fs.Close();
        }
   
    }

  


}
View Code

 

五,體驗小程序

微信搜索 :DB小陽光

掃碼體驗:

登陸小程序後,發送token至電子郵箱353227876@qq.com

代碼已在Gitee開源。

感謝閱讀。

相關文章
相關標籤/搜索