I downloaded the .mp3 file from the server and stored it in my android persistentDataPath. But I do not know how to connect .mp3 files to AudioClip. Please let me know if there is a workaround for this.
Asked
Active
Viewed 1,020 times
-1
-
sorry.. I found the answer. I did not know there was www.audioClip. I can use this. – Bert Hu Jul 06 '17 at 01:24
1 Answers
0
Update
As pointed out in the comments WWW
now supports .mp3 files making it the way to go.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public string url;
public AudioSource source;
void Start() {
WWW www = new WWW(url);
source = GetComponent<AudioSource>();
source.clip = www.audioClip;
}
void Update() {
if (!source.isPlaying && source.clip.isReadyToPlay)
source.Play();
}
}
Original answer:
Audio clip doesn't accept a file. It accepts an array of floats.
You can use NAudio to convert the compressed MP3 to a WAV file.
using NAudio;
using NAudio.Wave;
// ....
using (Mp3FileReader reader = new Mp3FileReader(mp3File))
{
WaveFileWriter.CreateWaveFile(outputFile, reader);
}
You will then need to read the wave file which will basically have a header followed by the chunk data. The chunk data can be converted to a float and sent to your audio clip.

Alexander Higgins
- 6,765
- 1
- 23
- 41
-
You can use WWW class as it is built in and this task does not require external libraries. – Suraj S Jul 06 '17 at 01:31
-
-
-
-
-
Added WWW as an answer but beware the documentation says the function is now `Obsolete public Object audioClip; ` https://docs.unity3d.com/ScriptReference/WWW-audioClip.html – Alexander Higgins Jul 06 '17 at 07:04
-