unity3d 直接播放mp3

  • A+
所属分类:资源教程
摘要

unity3d 直接播放mp3 显示封面
点击这里下载项目文件

unity3d 直接播放mp3 显示封面

[csharp] view plain copy

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using Id3Lib;  
  4. using NAudio.Wave;  
  5. using System.IO;  
  6. using System;  
  7. using System.Text;  
  8. using System.Runtime.InteropServices;  
  9. public class MusicPlayer : MonoBehaviour  
  10. {   
  11.   
  12.     private IWavePlayer iwavePlayer;  
  13.     private WaveStream waveStream;  
  14.     private WaveChannel32 waveChannel32;  
  15.     Texture2D t;  
  16.     string path="";   
  17.     // Use this for initialization  
  18.     void Start()  
  19.     {  
  20.           
  21.     }  
  22.   
  23.     void LoadMusic(string path)  
  24.     {   
  25.         byte[] imageData;  
  26.         using (FileStream fsRead = new FileStream(path, FileMode.Open))  
  27.         {  
  28.             int fsLen = (int)fsRead.Length;  
  29.             imageData = new byte[fsLen];  
  30.             fsRead.Read(imageData, 0, imageData.Length);  
  31.         }  
  32.         LoadAudioFromData(imageData);  
  33.         t = GetAlbumCover(path);  
  34.     }  
  35.   
  36.     private bool LoadAudioFromData(byte[] data)  
  37.     {  
  38.         try  
  39.         {  
  40.             MemoryStream tmpStr = new MemoryStream(data);  
  41.             waveStream = new Mp3FileReader(tmpStr);   
  42.             waveChannel32 = new WaveChannel32(waveStream);  
  43.   
  44.             iwavePlayer = new WaveOut();  
  45.             iwavePlayer.Init(waveChannel32);  
  46.             return true;  
  47.         }  
  48.         catch (System.Exception ex)  
  49.         {  
  50.             Debug.LogWarning("Error! " + ex.Message);  
  51.         }  
  52.         return false;  
  53.     }  
  54.   
  55.     // Update is called once per frame  
  56.     void Update()  
  57.     {  
  58.   
  59.     }  
  60.   
  61.   
  62.     void OnGUI()  
  63.     {  
  64.         if (GUI.Button(new Rect(0, 0, 120, 30), "选择音乐"))  
  65.         {  
  66.             OpenFileName ofn = new OpenFileName();  
  67.   
  68.             ofn.structSize = Marshal.SizeOf(ofn);  
  69.                 
  70.             ofn.filter = "mp3(*.mp3)/0*.mp3";  
  71.   
  72.             ofn.file = new string(new char[256]);  
  73.   
  74.             ofn.maxFile = ofn.file.Length;  
  75.   
  76.             ofn.fileTitle = new string(new char[64]);  
  77.   
  78.             ofn.maxFileTitle = ofn.fileTitle.Length;  
  79.   
  80.             ofn.initialDir = UnityEngine.Application.dataPath;//默认路径    
  81.   
  82.             ofn.title = "选择音乐";  
  83.   
  84.             ofn.defExt = "mp3";//显示文件的类型    
  85.             //注意 一下项目不一定要全选 但是0x00000008项不要缺少    
  86.             ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR    
  87.   
  88.             if (WindowDll.GetOpenFileName(ofn))  
  89.             {  
  90.                 Debug.Log("Selected file with full path: {0}" + ofn.file);  
  91.                 LoadMusic(ofn.file);  
  92.                 path = ofn.file;  
  93.             }  
  94.             ofn = null;  
  95.             Resources.UnloadUnusedAssets();  
  96.         }  
  97.         if(GUI.Button(new Rect(0,30,120,30),"播放"))  
  98.         {  
  99.             iwavePlayer.Play();  
  100.         }  
  101.         if (GUI.Button(new Rect(0, 60, 120, 30), "暂停"))  
  102.         {  
  103.             iwavePlayer.Pause();  
  104.         }  
  105.         if (GUI.Button(new Rect(0, 90, 120, 30), "停止"))  
  106.         {  
  107.             iwavePlayer.Stop();   
  108.         }  
  109.         GUI.Label(new Rect(200, 170, 500, 30), path);  
  110.         if(t!=null)  
  111.         {  
  112.             GUI.DrawTexture(new Rect(200, 200, 200, 200), t);  
  113.         }  
  114.     }  
  115.   
  116.     void OnApplicationQuit()  
  117.     {  
  118.         if (iwavePlayer!=null)  
  119.         {  
  120.             iwavePlayer.Dispose();  
  121.         }  
  122.     }  
  123.     /// <summary>  
  124.     /// 获取音乐封面  
  125.     /// </summary>  
  126.     /// <param name="path"></param>  
  127.     /// <returns></returns>  
  128.     public Texture2D GetAlbumCover(string path)  
  129.     {  
  130.         Texture2D image = null;  
  131.         FileStream fs = new FileStream(path, FileMode.Open);  
  132.         try  
  133.         {  
  134.             byte[] header = new byte[10]; //标签头  
  135.             int offset = 0;  
  136.             bool haveAPIC = false;  
  137.             fs.Read(header, 0, 10);  
  138.             offset += 10;  
  139.             string head = Encoding.Default.GetString(header, 0, 3);  
  140.   
  141.             if (head.Equals("ID3"))  
  142.             {  
  143.                 int sizeAll = header[6] * 0x200000 //获取该标签的尺寸,不包括标签头  
  144.                 + header[7] * 0x4000  
  145.                 + header[8] * 0x80  
  146.                 + header[9];  
  147.                 int size = 0;  
  148.                 byte[] body = new byte[10]; //数据帧头,这里认为数据帧头不包括编码方式  
  149.                 fs.Read(body, 0, 10);  
  150.                 offset += 10;  
  151.                 head = Encoding.Default.GetString(body, 0, 4);  
  152.                 while (offset < sizeAll) //当数据帧不是图片的时候继续查找  
  153.                 {  
  154.                     if (("APIC".Equals(head))) { haveAPIC = truebreak; }  
  155.                     size = body[size + 4] * 0x1000000 //获取该数据帧的尺寸(不包括帧头)  
  156.                     + body[size + 5] * 0x10000  
  157.                     + body[size + 6] * 0x100  
  158.                     + body[size + 7];  
  159.                     body = new byte[size + 10];  
  160.                     fs.Read(body, 0, size + 10);  
  161.                     offset += size + 10;  
  162.                     head = Encoding.Default.GetString(body, size, 4);  
  163.                 }  
  164.                 if (haveAPIC)  
  165.                 {  
  166.                     size = body[size + 4] * 0x1000000  
  167.                     + body[size + 5] * 0x10000  
  168.                     + body[size + 6] * 0x100  
  169.                     + body[size + 7];  
  170.                     byte[] temp = new byte[9];  
  171.                     byte[] temptype = new byte[10];  
  172.                     fs.Seek(1, SeekOrigin.Current);  
  173.                     fs.Read(temp, 0, 9);  
  174.                     int i = 0;  
  175.                     switch (Encoding.Default.GetString(temp))  
  176.                     {  
  177.                         case "image/jpe":  
  178.   
  179.                             while (i < size) //jpeg开头0xFFD8  
  180.                             {  
  181.                                 if (temptype[0] == 0 && temptype[1] == 255 && temptype[2] == 216)  
  182.                                 {  
  183.                                     break;  
  184.                                 }  
  185.                                 fs.Seek(-2, SeekOrigin.Current);  
  186.                                 fs.Read(temptype, 0, 3);  
  187.                                 i++;  
  188.                             }  
  189.                             fs.Seek(-2, SeekOrigin.Current);  
  190.                             break;  
  191.                         case "image/jpg":  
  192.   
  193.                             while (i < size) //jpg开头0xFFD8  
  194.                             {  
  195.                                 if (temptype[0] == 0 && temptype[1] == 255 && temptype[2] == 216)  
  196.                                 {  
  197.                                     break;  
  198.                                 }  
  199.                                 fs.Seek(-2, SeekOrigin.Current);  
  200.                                 fs.Read(temptype, 0, 3);  
  201.                                 i++;  
  202.                             }  
  203.                             fs.Seek(-2, SeekOrigin.Current);  
  204.                             break;  
  205.                         case "image/gif":  
  206.   
  207.                             while (i < size) //gif开头474946  
  208.                             {  
  209.                                 if (temptype[0] == 71 && temptype[1] == 73 && temptype[2] == 70)  
  210.                                 {  
  211.                                     break;  
  212.                                 }  
  213.                                 fs.Seek(-2, SeekOrigin.Current);  
  214.                                 fs.Read(temptype, 0, 3);  
  215.                                 i++;  
  216.                             }  
  217.                             fs.Seek(-3, SeekOrigin.Current);  
  218.                             break;  
  219.                         case "image/bmp":  
  220.   
  221.                             while (i < size) //bmp开头424d  
  222.                             {  
  223.                                 if (temptype[0] == 66 && temptype[1] == 77)  
  224.                                 {  
  225.                                     break;  
  226.                                 }  
  227.                                 fs.Seek(-1, SeekOrigin.Current);  
  228.                                 fs.Read(temptype, 0, 2);  
  229.                                 i++;  
  230.                             }  
  231.                             fs.Seek(-2, SeekOrigin.Current);  
  232.                             break;  
  233.                         case "image/png":  
  234.                             while (i < size) //png开头89 50 4e 47 0d 0a 1a 0a  
  235.                             {  
  236.                                 if (temptype[0] == 137 && temptype[1] == 80 && temptype[2] == 78 && temptype[3] == 71 && temptype[4] == 13)  
  237.                                 {  
  238.                                     break;  
  239.                                 }  
  240.                                 fs.Seek(-9, SeekOrigin.Current);  
  241.                                 fs.Read(temptype, 0, 10);  
  242.                                 i++;  
  243.                             }  
  244.                             fs.Seek(-10, SeekOrigin.Current);  
  245.                             break;  
  246.                         default://FFFB为音频的开头  
  247.                             break;  
  248.                     }  
  249.   
  250.                     byte[] imageBytes = new byte[size];  
  251.                     fs.Read(imageBytes, 0, size);  
  252.                     image = new Texture2D(128, 128);  
  253.                     image.LoadImage(imageBytes);  
  254.                 }  
  255.                 else  
  256.                 {  
  257.                     image = null;  
  258.                 }  
  259.             }  
  260.             else  
  261.             {  
  262.                 image = null;  
  263.             }  
  264.         }  
  265.         catch (Exception ex)  
  266.         {  
  267.             Debug.LogWarning(ex.Message);  
  268.         }  
  269.         finally  
  270.         {  
  271.             fs.Close();  
  272.         }  
  273.         return image;  
  274.     }  
  275. }  

unity3d 直接播放mp3

点击这里下载项目文件

  • 领支付宝红包
  • 扫一扫领红包
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
admin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: