美文网首页
音频优化之代码

音频优化之代码

作者: APP4x | 来源:发表于2020-11-18 13:52 被阅读0次

    这是我在项目里用的音频格式化代码:

    public class SoundImportEditor : AssetPostprocessor
    {
        public override uint GetVersion()
        {
            return 1;
        }
    
        const string SYMBOL_ANDROID = "Android";
        const string SYMBOL_IPHONE = "iPhone";
        const string DIR_SOUND = "Assets/Sound/";
    
    
        //背景音乐:Compress In Memory + Vorbis/MP3
        //经常播放的短音效:Decompress On Load + PCM
        //一般的音效:Compressed In Memory + ADPCM
    
        List<string> bgSound = new List<string>()
        {
            "BgBattle",
            "BgMainUi"
        };
    
        List<string> btnSound = new List<string>()
        {
            "battle_open_btn",
            "normal_choose",
            "normal_click",
            "normal_close",
            "normal_layout",
            "normal_menu"
        };
    
        bool NameInList(List<string> list, string name)
        {
            foreach (var item in list)
            {
                if (name.Contains(item))
                    return true;
            }
            return false;
        }
    
        private void OnPostprocessAudio(AudioClip clip)
        {
            AudioImporter importer = assetImporter as AudioImporter;
            string versionStr = this.GetVersion().ToString();
            if (importer.userData == versionStr)
                return;
            string assetPath = importer.assetPath;
            if (!assetPath.Contains(DIR_SOUND))
                return;
    
            Debug.LogFormat("自动帮您导入音频 纠正格式 : {0}", assetPath);
    
            //ambisonic VR有用
            importer.ambisonic = false;
            importer.preloadAudioData = false;
            //channels 声道数
            importer.forceToMono = clip.channels <= 1;
            importer.userData = versionStr;
    
            AudioImporterSampleSettings setting = importer.defaultSampleSettings;
            setting.sampleRateSetting = AudioSampleRateSetting.OverrideSampleRate;
            setting.sampleRateOverride = 22050;
    
            if (NameInList(bgSound, assetPath))
            {
                importer.loadInBackground = true;
    
                setting.loadType = AudioClipLoadType.Streaming;
                setting.compressionFormat = AudioCompressionFormat.Vorbis;
                setting.quality = 0.5f;
            }
            else if (NameInList(btnSound, assetPath))
            {
                importer.loadInBackground = false;
    
                setting.loadType = AudioClipLoadType.DecompressOnLoad;
                setting.compressionFormat = AudioCompressionFormat.PCM;
            }
            else
            {
                importer.loadInBackground = false;
    
                setting.loadType = AudioClipLoadType.CompressedInMemory;
                setting.compressionFormat = AudioCompressionFormat.ADPCM;
            }
    
            importer.SetOverrideSampleSettings(SYMBOL_ANDROID, setting);
            importer.SetOverrideSampleSettings(SYMBOL_IPHONE, setting);
    
            importer.SaveAndReimport();
            EditorUtility.SetDirty(importer);
        }
    }
    

    相关文章

      网友评论

          本文标题:音频优化之代码

          本文链接:https://www.haomeiwen.com/subject/ykkciktx.html