- Home /
can't load music (wav and mp3 ) on android but the code works fine in editor and window build
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using System;
public class WWWAudio : MonoBehaviour { private string Path; public AudioClip Clip;
//List of all valid directories
public List<string> Files = new List<string>();
//List of all AudioClips
public List<AudioClip> Clips = new List<AudioClip>();
private AudioManager CustomRadio = null;
void Awake()
{
CustomRadio = GetComponent<AudioManager>();
Path = string.Format("file:///storage/emulated/0/", Application.persistentDataPath);
if (Directory.Exists(Path ))
{
try
{
DirectoryInfo info = new DirectoryInfo(Path);
foreach (var item in info.GetFiles("*.wav",SearchOption.AllDirectories))
{
Files.Add(item.Name);
}
}
catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); }
}
if (Files.Count != 0)
{
StartCoroutine(LoadAudioFile());
}
else {
print("no music files found");
}
}
IEnumerator LoadAudioFile()
{
string[] files = Directory.GetFiles(Path,"*.wav",SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
if (files[i].EndsWith(".wav"))
{
Clip = new WWW(files[i]).GetAudioClip(true, true,AudioType.WAV);
if (Clip.loadState != AudioDataLoadState.Loaded)
{
yield return Clip;
}
else
{
Clip.name = Files[i];
Clips.Add(Clip);
CustomRadio.musicClips.Add(Clip);
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Which AudioClip formats will decompress using hardware? 0 Answers
Loading Audio Files at Runtime for Playback and Processing 1 Answer
Sound not playing on Galaxy S4 device 0 Answers
Mp3 Streaming on android 1 Answer
streaming to iPhone and Android 0 Answers