- Home /
Android Native Audio Adapter - Load file to MediaPlayer
Hi. I'm trying to create simple AndroidNativeAudioAdapter
script to workaround Unity audio latency on Android. I just want to be able create MediaPlayer
instance and use start()
method. Unfortunetly, I don't know how to set Unity asset as MediaPlayer
's datasource. My file "song.mp3" is located in Assets/StreamingAssets/Songs folder and here is my actual constructor (doesn't work):
public AndroidNativeAudioAdapter(string songName)
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaClass mediaPlayerClass = new AndroidJavaClass("android.media.MediaPlayer");
AndroidJavaObject currentActivity;
AndroidJavaObject uri;
AndroidJavaObject mediaPlayer;
currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
uri = uriClass.CallStatic<AndroidJavaObject>("parse",
"jar:file://" + Application.dataPath + "!/assets/Songs/"+songName+".mp3");
mediaPlayer = mediaPlayerClass.CallStatic<AndroidJavaObject>
("create", currentActivity, uri);
mediaPlayer.Call("Start");
}
App should work on Android 4.0 (API level 14). Thank you in advance!
Have you found a solution to your problem? If so, I would be really interested in how you've done it as I'm coping myself with this annoying audio delay... I've even noticed a delay on iOS but it's not as bad as with Android. Thanks!
After a week I just give up and bought this asset (well documented): https://www.assetstore.unity3d.com/en/#!/content/35295 On some devices delay disappeared, on others is ~50% smaller, but still exists. I think that audio delay is unavoidable on some devices, because every app on my smartphone have that issue. But I'm still interested how to do it by myself if someone can explain how it works.
Thanks. That asset would have been my last resort for the android side too. I noticed a slightly better performance when building with IL2CPP for Android, but the delay is still distinct. Nevertheless some improvement. I need to look further into this issue and if I find some proper solution I will share it with you.
Your answer
Follow this Question
Related Questions
How to pass ArrayList from Java to Unity C# 1 Answer
Disable/Enable Bluetooth on Android (NATIVE) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to enable location dialog? 0 Answers