- Home /
How to play mp3, mp4 files on the local disk
I use the www, but it only supports ogg format, How to play mp3, mp4 files on the local disk
O$$anonymous$$, this is driving me nuts! I have been trying for days to get a unity game to load a .ogg or .mp3 file from the strea$$anonymous$$g assets folder using WWW. It works great on the standalone PC build and while in the editor. However on android I just get the error message "strea$$anonymous$$g ".ogg" not supported on this platform". I've been searching for days now and this seems like it should be a straightforward thing to do- load an external audio file...how common and mundane of a task to be so damned difficult (if not impossible).
Why in the name of all that is holy does Unity not document how to do this task, which seems many people are struggling with?
Answer by save · Aug 19, 2013 at 10:28 AM
The short answer is, you cannot play certain media files with Unity without a proper plugin. You need a license.
You can however play ogg- and wav-files, as a small bandaid to this void of an answer, importing them at runtime could look like this:
#pragma strict
import System.Collections.Generic;
import System.IO;
static var path : String = "./"; // Is equal to where you have your executable
static var fileTypes : String[] = ["ogg", "wav"]; // Valid file types
static var files : FileInfo[];
static var audioSource : AudioSource;
static var audioClips : List.<AudioClip> = new List.<AudioClip>();
function Start () {
// If in editor the path is in Assets folder
if (Application.isEditor)
path = "Assets/";
// Set an AudioSource to this object
audioSource = audio;
if(audioSource == null)
audioSource = gameObject.AddComponent(AudioSource);
// Find files in directory
yield GetFilesInDirectory();
// Play a clip found in directory
if (audioClips.Count>0) {
audioSource.clip = audioClips[0];
audioSource.Play();
}
}
function GetFilesInDirectory () {
var info : DirectoryInfo = new DirectoryInfo(path);
files = info.GetFiles();
for (var file : FileInfo in files) {
var extension : String = Path.GetExtension(file.FullName);
if (ValidType(extension))
yield LoadFile(file.FullName);
}
}
function ValidType (extension : String) : boolean {
for (var validExtension : String in fileTypes)
if (extension.IndexOf(validExtension) > -1)
return true;
return false;
}
function LoadFile (path : String) {
var www : WWW = new WWW("file://"+path);
var clip : AudioClip = www.audioClip;
while (!clip.isReadyToPlay)
yield;
clip = www.GetAudioClip(false);
var parts : String[] = path.Split("\\"[0]);
clip.name = parts[parts.Length-1];
audioClips.Add(clip);
}
Answer by Benproductions1 · Aug 19, 2013 at 08:20 AM
http://lmgtfy.com?q=Unity+Play+Audio+from+File
@Benproductions1, you missed his point. the OP doesnt want to play files already imported into Unity. He wants to play "external" files. The primary way to do that is using WWW, but remember that plays Ogg files only, not $$anonymous$$P3. Hence, his question.
Its actually a legitimate question. For reference, see this related question.
I did not miss the point. The whole point was to show that currently there is no way of doing what he wants, as seen in one of the search results.
The point of my answer was to show that his question had already been answered before, specifically the "related question" you linked to, which answeres the question here.
If the OP would have simply done a google search he would not have had to post the question :)
Ben, its always easier to find out if something works, rather than if it doesn't work. Why? Because if something works, you find the solution via Google. If it doesnt, you will find many people saying it can't be done, but you can't get the feeling out of your $$anonymous$$d that perhaps there is someone out there who "can" get it done.
So my logic is simply this: If its already answered, and if it can be done, point to the relevant answer. If it "can't" be done, then repeat the answer again + point to older answer.
Your answer
Follow this Question
Related Questions
Streaming MP3s in WebGL Editor 0 Answers
Load mp3 from Harddrive on PC (again) 1 Answer
WWW.getAudioClip() returning wrong length from one server but not another 0 Answers
WWW load mp3 GetAudioClip without stream,play only a part,then no sound. 0 Answers
What video formats can I use loading from the streaming assets folder? 2 Answers