unity播放加密的音频文件
来源:互联网 发布:新日铁软件 张涛 编辑:程序博客网 时间:2024/06/05 01:03
首先说一下起因,我这里有需求读书软件要求播放加密的音频文件,所以一般的组件和www加载都不能使用,于是我需要一种新的方案来加载音频文件。于是我找到了一个开源的音频处理类库NAudio来处理MP3和wav的音频文件,同时我还需要使用到ogg的音频格式,后面还找到了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。
4.在unity3d中播放MP3,文档链接http://naudio.codeplex.com/wikipage?title=MP3
4.1 使用命名空间
1
2
using
NAudio;
using
NAudio.Wave;
1
2
IWavePlayer waveOutDevice;
AudioFileReader audioFileReader;
1
2
3
4
5
6
void
Start (){
waveOutDevice =
new
WaveOut();
audioFileReader =
new
AudioFileReader(
@"D:\影音文件\音乐\BEYOND\黄家驹 - 总有爱.mp3"
);
waveOutDevice.Init(audioFileReader);
waveOutDevice.Play();
}
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
;
}
}
5.在unity3d中播放ogg,文档链接:http://nvorbis.codeplex.com/documentation ,我这里还是会使用NAudio来播放
5.1使用命名空间
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();
}
于是我决定再来啰嗦一下,如果使用www外部加载时不能加载MP3格式的音频文件,但是目前MP3格式的音频文件确实最多的,还有一个重要因素是,我目前解密出来的音频文件是一堆字节数组,而且我也不知道不同音频应该怎样去解码,如果我再把字节数组去写成一个音频文件再用www来读取,那确实有点闲得蛋疼的感觉了。
于是下面就是来实现怎样利用NAudio和NVorbis把一堆字节数组转成unity的AudioClip,使用unity的AudioSource来播放。
6.1 mp3/wav字节数组转AudioClip,具体给出代码来了,参考链接:http://gamedev.stackexchange.com/questions/114885/how-do-i-play-mp3-files-in-unity-standalone
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
public
static
AudioClip FromWavData(
byte
[] data)
{
// Load the data into a stream
MemoryStream wavstream =
new
MemoryStream(data);
WaveFileReader wavaudio =
new
WaveFileReader(wavstream);
WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(wavaudio);
// Convert to WAV data
WAV wav =
new
WAV(AudioMemStream(waveStream).ToArray());
Debug.Log(wav);
AudioClip audioClip = AudioClip.Create(
"clip"
, wav.SampleCount, 1, wav.Frequency,
false
);
audioClip.SetData(wav.LeftChannel, 0);
// Return the clip
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(
"clip"
, 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;
while
(pos < wav.Length)
{
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);
}
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
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);
// 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(0);
//Only used to rewind the stream, we won't be seeking
}
0 0
- unity播放加密的音频文件
- java实现音频文件的播放
- 播放SDcard上面的音频文件
- 音频文件播放时间的获取
- java 实现音频文件的播放
- 播放音频文件
- 播放音频文件
- android实现音频文件播放的方法
- Anroid开发 之 音频文件的循环播放
- 在浏览器中播放音频文件的兼容性问题
- 在浏览器中播放音频文件的兼容性问题
- 【C#获取音频文件的播放时间】
- php判断一个音频文件的播放时间
- AVAudioPlayer 播放音频文件无声音的问题
- html/Javascript播放WAV音频文件的方法
- 得到音频文件的播放时间---GetAmrDuration
- MediaPlayer播放assert目录下的音频文件
- MediaPlayer播放不同来源的音频文件
- C++ __abstract
- Gradle配置3
- 【Eclipse】更改部署位置
- 海康相机RTSP连接代码分析
- 夕拾算法初级篇:1)1001. 害死人不偿命的(3n+1)猜想
- unity播放加密的音频文件
- 学习bootstrap 总结(1)
- copy构造函数调用时机4,函数返回值是匿名对象
- AngularJS启动过程
- [leetcode]303. Range Sum Query - Immutable
- 单向链表的建议搭建
- 进程的组成部分
- Python将多个list合并为1个list
- hdu2036 计算坐标内任意多边形的面积 含凹凸