博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PC平台在Unity3D中播放硬盘ogg,mp3,wav文件
阅读量:6081 次
发布时间:2019-06-20

本文共 6454 字,大约阅读时间需要 21 分钟。

Unity3D PC平台本身是支持直接用www读取本地ogg,wav的,但是并不能读取byte[],字节数组格式,这对用习惯了bass,fmod的人来说有点不方便。

搜了一圈发现了一个C#的音频库叫NAudio,开源并且免费。

简单粗暴的搞一份二进制release,然后nuget一下ogg扩展

Install-Package NAudio.Vorbis  

使用方面参考了下面3个地址

简单来说新建一个NAudioPlayer.cs文件,内容如下

using UnityEngine;using System.IO;using System;using NAudio;using NAudio.Wave;public static class NAudioPlayer{    static NVorbis.VorbisReader vorbis;    public static AudioClip FromOggData(byte[] data)    {        // Load the data into a stream        MemoryStream oggstream = new MemoryStream(data);        vorbis = new NVorbis.VorbisReader(oggstream, false);        int samplecount = (int)(vorbis.SampleRate * vorbis.TotalTime.TotalSeconds);        //  AudioClip audioClip = AudioClip.Create("clip", samplecount, vorbis.Channels, vorbis.SampleRate, false, true, OnAudioRead, OnAudioSetPosition);        AudioClip audioClip = AudioClip.Create("ogg clip", samplecount, vorbis.Channels, vorbis.SampleRate, false, OnAudioRead);        // Return the clip        return audioClip;    }    static void OnAudioRead(float[] data)    {        var f = new float[data.Length];        vorbis.ReadSamples(f, 0, data.Length);        for (int i = 0; i < data.Length; i++)        {            data[i] = f[i];        }    }    static void OnAudioSetPosition(int newPosition)    {        vorbis.DecodedTime = new TimeSpan(newPosition); //Only used to rewind the stream, we won't be seeking    }    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;    }    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("mp3Clip", 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;    }}/* 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 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 < 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);    }}

再挂个AudioSource,简单粗暴读取文件,解码变为byte[]填充进AudioClip,播放即可

void PlayOGG(){    string path = Application.dataPath + "/../Audio/Music/a.ogg";    byte[] data = File.ReadAllBytes(path);    audioSource.clip = NAudioPlayer.FromOggData(data);    audioSource.Play();}void PlayMP3(){    string path = Application.dataPath + "/../Audio/Music/a.mp3";    byte[] data = File.ReadAllBytes(path);    audioSource.clip = NAudioPlayer.FromMp3Data(data);    audioSource.Play();}void PlayWAV(){    string path = Application.dataPath + "/../Audio/Music/a.wav";    byte[] data = File.ReadAllBytes(path);    audioSource.clip = NAudioPlayer.FromWavData(data);    audioSource.Play();}

 

转载于:https://www.cnblogs.com/kileyi/p/10792893.html

你可能感兴趣的文章
【BZOJ】2243 [SDOI2011]染色
查看>>
springboot集成springsession利用redis来实现session共享
查看>>
文件上传与下载总结
查看>>
【测试基础】测试用例的设计方法
查看>>
MySQL优化-》执行计划和常见索引
查看>>
ThinkPHP中通过URL重写隐藏应用的入口文件index.php的相关服务器的配置
查看>>
18、图片 & 多媒体
查看>>
第七周进度总结
查看>>
Android任务栈的运行规律
查看>>
批处理通用测试代码
查看>>
uva 10594 Data Flow
查看>>
POJ 3592 Instantaneous Transference
查看>>
redis数据类型(字符串)
查看>>
解决火狐浏览器安装不上Selenium IDE插件“此附加组件无法安装”
查看>>
How PD works in Agile team
查看>>
存储过程 触发器,视图
查看>>
Dockerfile详解
查看>>
Archlinux添加MP3播放器
查看>>
django连接Oracle过程中出现的问题
查看>>
第七周作业——基础
查看>>