unity使用NAudio/NVorbis加载外部音频文件
首先说一下起因,我这里有需求将音频文件加密,所以一般的组件和www加载都不能使用,于是我需要一种新的方案来加载音频文件。于是我找到了一个开源的音频处理类库NAudio来处理MP3和wav的音频文件,同时我还需要使用到ogg的音频格式,后面还找到了NVorbis,我这里也是初步探索,下面就是我在unity中使用NAudio/NVorbis加载外部音频文件的方法。
一、资料准备
1.NAudio的链接:http://naudio.codeplex.com/,里面有相关的介绍和详细的文档,对unity3d还有一个插件,https://www.assetstore.unity3d.com/en/#!/content/32034 ,我并没有机会下载下来学习一下,只是简单使用了NAudio的几个接口,后面有兴趣的可以和我一起研究一下NAudio。
2.NVorbis的链接:http://nvorbis.codeplex.com/ ,这里是支持ogg需要下载的类库,后面我实用到相关的接口,我再给出详细的文档链接。
3.从NAudio和NVorbis下载发布的dll文件放在unity中,NVorbis还需要导入NVorbis.NAudioSupport.dll后面需要使用NAudio来播放ogg,记得将unity的Api Compatibility Level 改为.NET 2.0。
二、在unity中播放外部的MP3音频文件
1.文档链接http://naudio.codeplex.com/wikipage?title=MP3
2.使用命名空间
1 2 | using NAudio; using NAudio.Wave; |
3.申请一个播放组件和一个音频文件读取
1 2 | IWavePlayer waveOutDevice; AudioFileReader audioFileReader; |
4.开始读取文件和播放音乐
1 2 3 4 5 6 | void Start (){ waveOutDevice = new WaveOut(); audioFileReader = new AudioFileReader( @"D:影音文件音乐BEYOND黄家驹 - 总有爱.mp3" ); waveOutDevice.Init(audioFileReader); waveOutDevice.Play(); } |
5.注意在程序中退出的关闭音乐播放组件,因为他并不是使用unity在播放声音,而且使用自带的组件播放的了
1 2 3 4 5 6 7 8 9 10 11 12 | void OnApplicationQuit() { if (waveOutDevice != null ) { waveOutDevice.Stop(); } if (waveOutDevice != null ) { waveOutDevice.Dispose(); waveOutDevice = null ; } } |
6. 还有这里加载wav也是同样可以播放的,同时应该也可以试试其他格式的音频比如aiff,但是不支持ogg,下面我会单独再来使用ogg。
三、在unity播放外部的ogg音频文件
1.文档链接:http://nvorbis.codeplex.com/documentation ,我这里还是会使用NAudio来播放2.使用命名空间
1 2 | using NVorbis; using NVorbis.NAudioSupport; |
1 2 3 4 5 6 | void Start (){ waveOutDevice = new WaveOut(); VorbisWaveReader oggReader = new VorbisWaveReader( @"D:侧田 - 感动.ogg" ); waveOutDevice.Init(oggReader); waveOutDevice.Play(); } |
四、将外部的音频文件转为unity的AudioClip
1.按着以上的方法来播放音乐是否感觉特别简单了,但是在实际使用中,如果是加载外部音频文件,我知道肯定有人还是觉得使用www来加载感觉更方便一些。于是我决定再来啰嗦一下,如果使用www外部加载时不能加载MP3格式的音频文件,但是目前MP3格式的音频文件确实最多的,还有一个重要因素是,我目前解密出来的音频文件是一堆字节数组,而且我也不知道不同音频应该怎样去解码,如果我再把字节数组去写成一个音频文件再用www来读取,那确实有点闲得蛋疼的感觉了。于是下面就是来实现怎样利用NAudio和NVorbis把一堆字节数组转成unity的AudioClip,使用unity的AudioSource来播放。
2. mp3/wav字节数组转AudioClip,具体给出代码来了,参考链接:http://gamedev.stackexchange.com/questions/114885/how-do-i-play-mp3-files-in-unity-standalone
3.自定义Wav格式,这里修复了RightChannel不为null时的最大输入判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | public class WAV { // convert two bytes to one float in the range -1 to 1 static 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) 1 return 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; } // properties public 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 stereo ChannelCount = wav[22]; // Forget byte 23 as 99.999% of WAVs are 1 or 2 channels // Get the frequency Frequency = 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 |
1 2 3 4 5 6 7 | public static AudioClip FromWavData( byte [] data) { WAV wav = new WAV(data); AudioClip audioClip = AudioClip.Create( "wavclip" , wav.SampleCount, 1, wav.Frequency, false ); audioClip.SetData(wav.LeftChannel, 0); return audioClip; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public static AudioClip FromMp3Data( byte [] data) { // Load the data into a stream MemoryStream mp3stream = new MemoryStream(data); // Convert the data in the stream to WAV format Mp3FileReader mp3audio = new Mp3FileReader(mp3stream); WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(mp3audio); // Convert to WAV data WAV wav = new WAV(AudioMemStream(waveStream).ToArray()); //Debug.Log(wav); AudioClip audioClip = AudioClip.Create( "testSound" , wav.SampleCount, 1, wav.Frequency, false ); audioClip.SetData(wav.LeftChannel, 0); // Return the clip return audioClip; } private static 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; } } |
|