Plugin To Receive Audio File
Solution 1:
What you can do is stick the audio file in the streaming assets folder of your project, and then use the Media Player class to access and play the file.
However, two things I would point out. a. Usually, using MediaPlayer will require you to run on the UI thread. This means that the game will be paused. You'll need to see if there's a workaround
b. Unity's audio solution is not the best, granted. But there's plenty of plugins available that are pretty amazing on the Asset store.
Solution 2:
So, I still wasn't able to forward the actual file (wav) then try to convert it to SoundPool. What I did was something different:
I put the audio files into the StreamingAssets folder so that when the project is packaged, I still have access to these raw files (although in Android, their compressed into jar files). (Just like Venkat at Axiom Studios said, I wish I saw that answer sooner though, spent some time discovering what StreamingAssets were.)
Using WWW, I was able to retrieve the raw files from the jar files. This is from "Application.streamingAssetsPath".
With these files, I wrote them into a new directory that can be easily accessible by the plugin using File.WriteAllBytes. I used the "Application.persistentDataPath" directory.
In the plugin side, SoundPool is able to get the resource from a provided URL, which is the destination directory + file name.
With this process, I was able to ultimately accomplish what I was going for, and the improvement in performance is drastic. And I'm pretty happy with it.
*Note: This however, makes copies of the files from inside the package unto the install folder (/data/data/com.example.app/files for example). I'm sure you can opt to provide a check before writing if the files exists already just so you can leave the files there for future use, or just delete them when the app closes as clean up.
Solution 3:
I just made Native Audio asset store plugins which can make a native call to both iOS and Android's fastest native way from one central interface. https://www.assetstore.unity3d.com/#!/content/107067 (It uses AudioTrack
instead of SoundPool
though, since from my test it is faster)
Both of the platform receives an audio file via StreamingAssets
special folder of Unity. At Java side, here's how you can refer to the file.
In Unity : Assets/StreamingAssets/Assist.wav
In Java :
Context context = UnityPlayer.currentActivity;
AssetManager assetManager = context.getAssets();
try {
AssetFileDescriptor descriptor = assetManager.openFd(audioPath);
byte[] music = newbyte[(int) descriptor.getLength()];
InputStream is = assetManager.open(audioPath);
is.read(music);
is.close();
//Do whatever you want with "music"
} catch (IOException e) {
Log("Error reading audio file " + audioPath);
e.printStackTrace();
return-1;
}
You now have a byte[]
array of an audio. audioPath
here would be just "Assist.wav". I guess you could put that to SoundPool
to instantiate it.
Post a Comment for "Plugin To Receive Audio File"