- Home /
Question by
alexanderlaiman · May 13, 2020 at 08:44 AM ·
androidaudiojavanestedandroidjavaobject
How to access methods from nested static java classes
Hi there, Im trying to build in an android audio controller into my unity application by accessing the audiostreams on the device and then changing their volume levels. However I am really struggling with interfacing with the android native code through Unity. I seem unable to call the methods in the nested static class AudioAttributes$Builder. I'm able to assign the builder to an AndroidJavaObject but then the script never proceeds when trying to call the methods on the Builder. Here's the code. I'm unable to read errors from the device due to the profiler not connecting or finding the device (Pico g2)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AndroidVolume : MonoBehaviour {
[SerializeField] int desiredVolume;
private AndroidJavaObject activity;
private AndroidJavaObject audioManager;
private AndroidJavaObject audioAttributes;
private AndroidJavaObject builderClass;
private AndroidJavaClass unityPlayer;
private AndroidJavaObject builder;
int controlStream;
int maxVolume;
private const int USAGE = 1;
private const int CONTENT_TYPE = 2;
void Start () {
unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
Debug.LogFormat("JClass:{0}", unityPlayer);
audioManager = new AndroidJavaObject("android.media.AudioManager");
audioAttributes = new AndroidJavaObject("android.media.AudioAttributes");
Debug.LogFormat("audiomanager:{0}", audioManager);
Debug.LogFormat("audioAttributes :{0}", audioAttributes );
activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
Debug.LogFormat("activity:{0}", activity);
audioManager = activity.Call<AndroidJavaObject>("getSystemService", new AndroidJavaObject("java.lang.String", "audio"));
Debug.Log("audioManager:{0}", audioManager);
Debug.Log("creating builder...");
builder = new AndroidJavaObject("android.media.AudioAttributes$Builder");
Debug.Log("Builder ready");
//Program fails to continue here while built to android.
builderClass.CallStatic("setUsage", USAGE);
Debug.Log("Set usage");
builder.Call("setContentType", CONTENT_TYPE);
Debug.Log("Set content type");
audioAttributes = builder.Call<AndroidJavaObject>("build");
Debug.Log("Build complete");
Debug.LogFormat("audioAttributes:{0}", audioAttributes);
}
}
Comment