C# 獲取 mp3文件信息

一個(gè)筆記:筆者整理的 MP3 文件信息獲取的API ,Unity 適用。

完整代碼:

using System;
using System.IO;
using UnityEngine;

namespace mp3infos
{
    public struct MP3Info
    {
        public string Title;
        public string Singer;
        public string Album;
        public string Year;
        public string Comment;

        public override string ToString()
        {
            return "MP3附加信息:" + Environment.NewLine +
             "-----------------------------" + Environment.NewLine +
             "標(biāo) 題: " + Title + Environment.NewLine +
             "歌 手: " + Singer + Environment.NewLine +
             "唱片集: " + Album + Environment.NewLine +
             "出版期: " + Year + Environment.NewLine +
             "備 注: " + Comment;
        }
    }
    public class MP3Helper
    {
        public static MP3Info ReadMP3Info(string path)
        {
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    return StreamHandler(fs);
                }
            }
            catch (Exception e)
            {
                Debug.Log(e.Message);
            }
            return default(MP3Info);
        }
        public static MP3Info ReadMP3Info(Stream stream)
        {
            return StreamHandler(stream);
        }
        private static MP3Info StreamHandler(Stream stream)
        {
            byte[] b = new byte[128];
            MP3Info mp3struct = new MP3Info();
            stream.Seek(-128, SeekOrigin.End);
            stream.Read(b, 0, 128);
            string MP3Flag = System.Text.Encoding.Default.GetString(b, 0, 3);
            if (MP3Flag == "TAG")
            {
                mp3struct.Title = System.Text.Encoding.Default.GetString(b, 3, 30);
                mp3struct.Singer = System.Text.Encoding.Default.GetString(b, 33, 30);
                mp3struct.Album = System.Text.Encoding.Default.GetString(b, 63, 30);
                mp3struct.Year = System.Text.Encoding.Default.GetString(b, 93, 4);
                mp3struct.Comment = System.Text.Encoding.Default.GetString(b, 97, 30);
            }
            return mp3struct;
        }
    }
}

2019年6月25日 更新 :
打包后漢字亂碼有效解決方案見(jiàn):Unity發(fā)布讀取中文數(shù)據(jù)亂碼 - 成膜的博客 - CSDN博客

使用方法:

  1. 使用 mp3 路徑(這種方式要注意訪問(wèn)MP3文件的先后順序(亦即是先獲取文件信息再播放它),避免占用問(wèn)題)
        string path = "d://xxx/xx/my.mp3"
        MP3Info info = MP3Helper.ReadMP3Info(path);
        if (!string.IsNullOrEmpty(info.Title))
        {
            Debug.Log(info.ToString());
        }
  1. 使用 mp3 數(shù)據(jù)流
        //此處數(shù)據(jù)流多為為網(wǎng)絡(luò)加載的情景
        MP3Info info = MP3Helper.ReadMP3Info(stream);
        if (!string.IsNullOrEmpty(info.Title))
        {
            Debug.Log(info.ToString());
        }

Debug效果:

Tips : 筆者在測(cè)試時(shí)發(fā)現(xiàn)如果出版日期留空就只能得到歌曲名稱(chēng)。所以還有優(yōu)化空間,有興趣的可以提交修改版,超級(jí)歡迎。

擴(kuò)展閱讀:

C#顯示MP3的標(biāo)簽信息 - benben - CSDN博客
[Unity 3D]跨平臺(tái)的Mp3加載方案 - 簡(jiǎn)書(shū)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容