日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity mp3转wav

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity mp3转wav 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

介紹

unity的WWW或者UnityWebRequest無法加載MP3格式的,就需要我們將MP3轉成WAV或者其他可用的格式。這里使用了NAudio.dll 引用 using NAudio.Wave;

/// <summary>/// UnityWebRequest 加載MP3播放/// </summary>/// <param name="url"></param>/// <param name="audio"></param>/// <returns></returns>public IEnumerator OnMP3LoadAndPlay(string url, AudioSource audio){UnityWebRequest www = UnityWebRequest.Get(url);yield return www.SendWebRequest();audio.clip = FromMp3Data(www.downloadHandler.data);audio.Play();}/// <summary>/// mp3轉wav/// </summary>/// <param name="data">byte[]</param>/// <returns></returns>AudioClip FromMp3Data(byte[] data){//將數據加載到流中MemoryStream mp3stream = new MemoryStream(data);//將流中的數據轉換為WAV格式Mp3FileReader mp3audio = new Mp3FileReader(mp3stream);WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(mp3audio);//轉換為WAV數據WAV wav = new WAV(AudioMemStream(waveStream).ToArray());AudioClip audioClip = AudioClip.Create("testSound", wav.SampleCount, 1, wav.Frequency, false);audioClip.SetData(wav.LeftChannel, 0);return audioClip;}/// <summary>/// 內存流/// </summary>/// <param name="waveStream">byte[]</param>/// <returns></returns>MemoryStream AudioMemStream(WaveStream waveStream){MemoryStream outputStream = new MemoryStream();using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat)){byte[] bytes = new byte[waveStream.Length];waveStream.Position = 0;waveStream.Read(bytes, 0, Convert.ToInt32(waveStream.Length));waveFileWriter.Write(bytes, 0, bytes.Length);waveFileWriter.Flush();}return outputStream;}

WAV.cs

/* From http://answers.unity3d.com/questions/737002/wav-byte-to-audioclip.html */ public class WAV {// convert two bytes to one float in the range -1 to 1static float bytesToFloat(byte firstByte, byte secondByte){// convert two bytes to one short (little endian)short s = (short)((secondByte << 8) | firstByte);// convert to range from -1 to (just below) 1return s / 32768.0F;}static int bytesToInt(byte[] bytes, int offset = 0){int value = 0;for (int i = 0; i < 4; i++){value |= ((int)bytes[offset + i]) << (i * 8);}return value;}// propertiespublic float[] LeftChannel { get; internal set; }public float[] RightChannel { get; internal set; }public int ChannelCount { get; internal set; }public int SampleCount { get; internal set; }public int Frequency { get; internal set; }public WAV(byte[] wav){// Determine if mono or stereoChannelCount = wav[22]; // Forget byte 23 as 99.999% of WAVs are 1 or 2 channels// Get the frequencyFrequency = bytesToInt(wav, 24);// Get past all the other sub chunks to get to the data subchunk:int pos = 12; // First Subchunk ID from 12 to 16// Keep iterating until we find the data chunk (i.e. 64 61 74 61 ...... (i.e. 100 97 116 97 in decimal))while (!(wav[pos] == 100 && wav[pos + 1] == 97 && wav[pos + 2] == 116 && wav[pos + 3] == 97)){pos += 4;int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216;pos += 4 + chunkSize;}pos += 8;// Pos is now positioned to start of actual sound data.SampleCount = (wav.Length - pos) / 2; // 2 bytes per sample (16 bit sound mono)if (ChannelCount == 2) SampleCount /= 2; // 4 bytes per sample (16 bit stereo)// Allocate memory (right will be null if only mono sound)LeftChannel = new float[SampleCount];if (ChannelCount == 2) RightChannel = new float[SampleCount];else RightChannel = null;// Write to double array/s:int i = 0;int maxInput = wav.Length - (RightChannel == null ? 1 : 3);// while (pos < wav.Length)while ((i < SampleCount) && (pos < maxInput)){LeftChannel[i] = bytesToFloat(wav[pos], wav[pos + 1]);pos += 2;if (ChannelCount == 2){RightChannel[i] = bytesToFloat(wav[pos], wav[pos + 1]);pos += 2;}i++;}}public override string ToString(){return string.Format("[WAV: LeftChannel={0}, RightChannel={1}, ChannelCount={2}, SampleCount={3}, Frequency={4}]", LeftChannel, RightChannel, ChannelCount, SampleCount, Frequency);} }

總結

以上是生活随笔為你收集整理的Unity mp3转wav的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。