public static AudioClip GetAudioClipFromMP3ByteArray(byte[] in_aMP3Data) { AudioClip l_oAudioClip = null; Stream l_oByteStream = new MemoryStream(in_aMP3Data); MP3Sharp.MP3Stream l_oMP3Stream = new MP3Sharp.MP3Stream(l_oByteStream); try { //Get the converted stream data MemoryStream l_oConvertedAudioData = new MemoryStream(); byte[] l_aBuffer = new byte[2048]; int l_nBytesReturned = -1; int l_nTotalBytesReturned = 0; while (l_nBytesReturned != 0) { l_nBytesReturned = l_oMP3Stream.Read(l_aBuffer, 0, l_aBuffer.Length); l_oConvertedAudioData.Write(l_aBuffer, 0, l_nBytesReturned); l_nTotalBytesReturned += l_nBytesReturned; } //Debug.Log("MP3 file has " + l_oMP3Stream.ChannelCount + " channels with a frequency of " + l_oMP3Stream.Frequency); byte[] l_aConvertedAudioData = l_oConvertedAudioData.ToArray(); Debug.Log("Converted Data has " + l_aConvertedAudioData.Length + " bytes of data"); //Convert the byte converted byte data into float form in the range of 0.0-1.0 float[] l_aFloatArray = new float[l_aConvertedAudioData.Length / 2]; for (int i = 0; i < l_aFloatArray.Length; i++) { if (BitConverter.IsLittleEndian) { //Evaluate earlier when pulling from server and/or local filesystem - not needed here //Array.Reverse( l_aConvertedAudioData, i * 2, 2 ); } //Yikes, remember that it is SIGNED Int16, not unsigned (spent a bit of time before realizing I screwed this up...) l_aFloatArray[i] = (float)(BitConverter.ToInt16(l_aConvertedAudioData, i * 2) / 32768.0f); } //For some reason the MP3 header is readin as single channel despite it containing 2 channels of data (investigate later) if (l_oMP3Stream.ChannelCount == 1) { l_oAudioClip = AudioClip.Create("MySound", l_aFloatArray.Length / 2, 2, l_oMP3Stream.Frequency, false); } else { l_oAudioClip = AudioClip.Create("MySound", l_aFloatArray.Length, 2, l_oMP3Stream.Frequency, false); } l_oAudioClip.SetData(l_aFloatArray, 0); } catch (Exception ex) { Debug.Log(ex.ToString()); } return(l_oAudioClip); }
static void ExampleReadEntireMP3File() { MP3Stream stream = new MP3Stream("@sample.mp3"); // Create the buffer const int NUMBER_OF_PCM_BYTES_TO_READ_PER_CHUNK = 4096; byte[] buffer = new byte[NUMBER_OF_PCM_BYTES_TO_READ_PER_CHUNK]; int bytesReturned = -1; int totalBytes = 0; while (bytesReturned != 0) { bytesReturned = stream.Read(buffer, 0, buffer.Length); totalBytes += bytesReturned; } Console.WriteLine("Read a total of " + totalBytes + " bytes."); stream.Close(); stream = null; }
/// <summary> /// 将mp3格式的字节数组转换为audioclip /// </summary> /// <param name="data"></param> /// <returns></returns> public static AudioClip GetAudioClipFromMP3ByteArray(byte[] mp3Data) { var mp3MemoryStream = new MemoryStream(mp3Data); MP3Sharp.MP3Stream mp3Stream = new MP3Sharp.MP3Stream(mp3MemoryStream); //Get the converted stream data MemoryStream convertedAudioStream = new MemoryStream(); byte[] buffer = new byte[2048]; int bytesReturned = -1; int totalBytesReturned = 0; while (bytesReturned != 0) { bytesReturned = mp3Stream.Read(buffer, 0, buffer.Length); convertedAudioStream.Write(buffer, 0, bytesReturned); totalBytesReturned += bytesReturned; } //Debug.Log("MP3 file has " + mp3Stream.ChannelCount + " channels with a frequency of " + //mp3Stream.Frequency); byte[] convertedAudioData = convertedAudioStream.ToArray(); //bug of mp3sharp that audio with 1 channel has right channel data, to skip them byte[] data = new byte[convertedAudioData.Length / 2]; for (int i = 0; i < data.Length; i += 2) { data[i] = convertedAudioData[2 * i]; data[i + 1] = convertedAudioData[2 * i + 1]; } Wav wav = new Wav(data, mp3Stream.ChannelCount, mp3Stream.Frequency); AudioClip audioClip = AudioClip.Create("testSound", wav.SampleCount, 1, wav.Frequency, false); audioClip.SetData(wav.LeftChannel, 0); return(audioClip); }