Gapless looping MP3 music in Unity 3D
关于unity下 播放无缝循环的音乐 我们会发现一个问题,就是音乐制作团队提供的文件,使用PC或是MAC的一些特殊的第三方播放器 可以听到 该文件是完美无缝循环的音乐,但是放到unity里时,却发现结尾处会卡停一下,或是播放不完整。下面的内容就是解释这是怎么回事,要如何解决呢?
一般游戏开发团队 最常用的格式MP3。他体积小 兼容IOS 或是 安卓又非常好,但是他有个致命的BUG,就是音乐不能完美循环或是完整播放。下面先说说为什么MP3有这样的BUG。
Gapless looping MP3 music in Unity 3D
Recently I have been struggling with my background music. As it appears MP3 music does not loop very well. Natively MP3 sounds have a little space at the start and end of the track.I thought that when you add a MP3 sound to your game, the new Unity setting called 'Gapless looping' would solve that for you automatically.
The problem in my case was that the property was disabled.
After searching for answers on Unity questions and answers, Google etc I have found the solution myself!
The trick is to export your music as WAV. Meaning that it will be added to Unity as an uncompressed audio file. Next, select the audio file and watch the properties in the inspector. As you can see the drop down shows WAV and most of the other options are grayed out. Now, select MPEG from the drop down. By doing that we are telling Unity to compress the WAV file.
Watch what happens with the other properties: they become enabled!
Next, select Gapless looping and the other settings. If you are using the file as background music you might want to set priority to 0 for instance to prevent the sound from being swapped out. Also it probably doesn't make sense to have 3D background music as the volume and panning does not apply to the position of your listener.
Finally click Apply and Unity will start creating the MP3 file for you. Add an empty game object to your game, add an Audio Source component to it and select the Audio clip. Make sure looping is enabled and start your game!
The music should loop perfectly. So to summarize: don't import MP3 files into Unity but instead import WAV and let Unity create the MP3 for you. That way you can create nice looping background music.
The problem (I think) is, that mp3 has "header" data that it stores at the beginning and the end of the file because it is a compressed data format. This header data is represented as a silent gap in the audio file, usually at the beginning or end of the file.
(本文是跟Channel游戏音乐 美国总监 讨论的结果,所以该篇文章英文占了很大的内容)我用中文简短概述上面的问题。这个问题出在:mp3在开始和结束播放时都会储存一些标头数据("header" data),因为MP3格式是一个压缩数据格式。标头数据显示为一段无声的空隙,一般出现在文件起始或结尾处。所以再unity下 会出现循环时卡壳的情况。
Yi (奇异)should not be importing MP3 into Unity as the MP3 files already have this header data embedded into the file and it will not loop properly.
WAV audio, however, does not have this header data because it is uncompressed. According to the link above, the audio should be imported into Unity as WAV and then converted within Unity to MP3 upon import. There is also a "gapless looping" checkbox on this importer that will remove the header data from the MP3 file that Unity created. This way, when Unity imports uncompressed WAV audio, it will convert the audio to MP3 and also remove the header data from the file so that the file will then loop seamlessly.
所以不建议直接把MP3导入到Unity里,因为mp3文件已经产生了这些标头数据,无法正确循环播放,但是WAV文件就没有这种标头数据,因为WAV格式未经压缩。应使用WAV格式音频文件导入到Unity,再在Unity内部转换成MP3导入软件上有一个“无缝循环”的选项,可以去除Unity在MP3文件里产生的标头数据,这样就可以通过用Unity导入未压缩的WAV文件,并转为MP3、去除MP3里的标头数据,即可实现无缝循环。
If Yi is importing MP3 directly into Unity then it is importing an MP3 file that already has header data in it, and possibly redundantly converting it to MP3 again and adding additional header data. We should be importing uncompressed WAV files into Unity and letting Unity handle the MP3 conversion and the gapless looping feature, rather than importing MP3 directly to Unity.
如果直接把MP3导入Unity就无法避免产生标头数据,并且可能Unity还会再把它转成MP3,在此过程中又产生新的标头数据。所以应先把未压缩的WAV文件导入Unity,再让Unity将之转化为MP3,并实现无缝循环,而非直接导入MP3文件。当然音乐的部分,如果选择wav格式,最终untiy打包也会把wav的音乐一起打包,从而增大了容量。经过测试使用OGG作为源文件,导入untiy后,untiy自处理压缩后的得到的 无论是mpeg 还是 ogg ,完美无缝的循环表现都很不错,跟wav的表现几乎完全一样。音效部分, wav在untiy中是无解压过程的,所以音效的部分我们选择wav源文件没问题, 如果是音乐或是音效 要使用无缝LOOP的时候,建议使用OGG作为源文件 。并采用硬件播放方式从而减少内存的损耗。
最后补充:格式的选择 只是平台和引擎下的表现能力。但是要完美无缝循环 还是需要先得到完美无缝循环的音乐音效文件,Channel游戏音乐欢迎您 到我们的音频团队 来制作您最想要的声音体验。