当前位置:   article > 正文

unity3d 直接播放mp3_unity3d 解析mp3

unity3d 解析mp3

unity3d 直接播放mp3 显示封面

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



点击这里下载项目文件

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/118431?site
推荐阅读
相关标签
  

闽ICP备14008679号