Unity 开发安卓动态加载外部音频文件

Unity2018.4.2ff 开发安卓加载外部音频文件开发说明
1、WWW方式,支持MP3和WAV音频文件格式
2、 UnityWebRequest方式,UnityWebRequestMultimedia.GetAudioClip(readPath, _audioType)不支持MP3格式,支持WAV格式;
3、WWW和UnityWebRequest都不能在安卓端使用Naudio.dll库文件创建新的wav文件。
(那位大佬如何知道方式,请在评论转告一下,谢谢!!!)
创建代码如下:

       string _tempURL = _url.Replace(".mp3", ".wav");

        Debug.Log("_tempURL:: " + _tempURL);
        if (!File.Exists(_tempURL))
        {
            FileStream _fileStream = File.Open(_tempURL, FileMode.Open);//安卓端报错
            Mp3FileReader _mp3FileReader = new Mp3FileReader(_fileStream);
            WaveFileWriter.CreateWaveFile(_tempURL, _mp3FileReader);
        }

正确加载音频文件的代码如下所示:

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class LoadAudioManager : MonoSingleton<LoadAudioManager>
{
    /// <summary>
    /// 获取AudioClip,并添加到挂载AudioSource组件的物体上
    /// </summary>
    /// <param name="uiRoot">UIPanel的父类</param>
    /// <param name="floderName">音频文件所在的文件夹名称</param>
    public void GetAudioClip(Transform uiRoot, string floderName)
    {
        //动态添加UI上音频文件
        for (int i = 0; i < uiRoot.childCount; i++)
        {
            AudioSource audioSource = uiRoot.GetChild(i).GetComponent<AudioSource>();
            string audioName = uiRoot.GetChild(i).name;
            StartCoroutine(LoadExternalAudioWWW(audioSource, floderName, audioName));
        }
    }

    /// <summary>
    /// 加载外部音频文件,并添加到挂载AudioSource组件的物体上
    /// </summary>
    /// <param name="audioSource">AudioSource组件</param>
    /// <param name="floderName">音频所在的文件夹名称</param>
    /// <param name="audioName">音频名称</param>
    /// <returns></returns>
    public IEnumerator LoadExternalAudioWWW(AudioSource audioSource, string floderName, string audioName)//成功的方法
    {
        string loadPath;//读取文件的路径
        if (Application.platform == RuntimePlatform.Android)
        {
            loadPath = "/storage/emulated/0/ConfigFiles/Audios/" + floderName + "/" + audioName + ".mp3";
            //Debug.LogError($"安卓端音频读取::{loadPath},");
        }
        else
        {
            loadPath = Application.streamingAssetsPath + "/ConfigFiles/Audios/" + floderName + "/" + audioName + ".mp3";
            //Debug.Log($"PC端音频读取::{loadPath},");
        }

        var www = new WWW("file://" + loadPath);
        yield return www;
        var clip = www.GetAudioClip();
        audioSource.clip = clip;
    }

    /// <summary>
    /// 加载外部音频文件,并添加到挂载AudioSource组件的物体上
    /// </summary>
    /// <param name="audioSource">AudioSource组件</param>
    /// <param name="floderName">音频所在的文件夹名称</param>
    /// <param name="audioName">音频名称</param>
    /// <param name="_audioType">音频格式</param>
    /// <returns></returns>
    public IEnumerator LoadExternalAudioWebRequest(AudioSource audioSource, string floderName, string audioName, AudioType _audioType = AudioType.WAV)
    {
        string readPath;//读取文件的路径
        if (Application.platform == RuntimePlatform.Android)
        {
            readPath = "file:///sdcard/ConfigFiles/Audios/" + floderName + "/" + audioName + ".wav";
            Debug.LogError($"安卓端音频读取::{readPath},");
        }
        else
        {
            readPath = Application.streamingAssetsPath + "/ConfigFiles/Audios/" + floderName + "/" + audioName + ".wav";
            Debug.LogError($"PC端音频读取::{readPath},");
        }

        yield return null;
        UnityWebRequest _unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(readPath, _audioType);
        yield return _unityWebRequest.SendWebRequest();

        if (_unityWebRequest.isHttpError || _unityWebRequest.isNetworkError)
        {
            Debug.Log(_unityWebRequest.error.ToString());
        }
        else
        {
            AudioClip _audioClip = DownloadHandlerAudioClip.GetContent(_unityWebRequest);
            audioSource.clip = _audioClip;
            //audioSource.loop = true;
            //audioSource.Play();
        }
    }
}
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值