- Home /
Question by
Tempest74 · May 28, 2019 at 02:54 PM ·
microphone
Problem requesting permission
Short story long. I try to move a player with my microphone. The problems appear when I request the permission (I am on unity remote 5). I use the scripts from: https://docs.unity3d.com/Manual/android-RequestingPermissions.html but it does not solve my problem. My microphone still does not record. Here I my scripts: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MicrophoneController : MonoBehaviour
{
public float sensitivity = 100;
public float loudness = 0;
AudioSource _audio;
// Start is called before the first frame update
void Start()
{
_audio = GetComponent<AudioSource>();
_audio.clip = Microphone.Start(null, true, 10, 44100);
_audio.loop = true;
_audio.mute = true;
while(!(Microphone.GetPosition(null) > 0))
{
}
_audio.Play();
}
// Update is called once per frame
void Update()
{
loudness = GetAvarageVolume() * sensitivity;
if(loudness > 8)
{
GetComponent<Rigidbody2D>().velocity = new Vector3(loudness, GetComponent<Rigidbody2D>().velocity.y);
}
}
private float GetAvarageVolume()
{
float[] datas = new float[256];
float a = 0;
_audio.GetOutputData(datas, 0);
foreach(float data in datas)
{
a += Mathf.Abs(data);
}
return a / 256;
}
}
using UnityEngine;
#if PLATFORM_ANDROID
using UnityEngine.Android;
#endif
public class AskForPermission : MonoBehaviour
{
GameObject dialog = null;
void Start ()
{
Debug.Log("Here");
#if PLATFORM_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
Permission.RequestUserPermission(Permission.Microphone);
dialog = new GameObject();
}
Debug.Log("Here_2");
#endif
}
void OnGUI ()
{
#if PLATFORM_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
// The user denied permission to use the microphone.
// Display a message explaining why you need it with Yes/No buttons.
// If the user says yes then present the request again
// Display a dialog here.
dialog.AddComponent<PermissionsRationaleDialog>();
return;
}
else if (dialog != null)
{
Destroy(dialog);
}
#endif
// Now you can do things with the microphone
}
}
using UnityEngine;
#if PLATFORM_ANDROID
using UnityEngine.Android;
#endif
public class PermissionsRationaleDialog : MonoBehaviour
{
const int kDialogWidth = 300;
const int kDialogHeight = 100;
private bool windowOpen = true;
void DoMyWindow(int windowID)
{
GUI.Label(new Rect(10, 20, kDialogWidth - 20, kDialogHeight - 50), "Please let me use the microphone.");
GUI.Button(new Rect(10, kDialogHeight - 30, 100, 20), "No");
if (GUI.Button(new Rect(kDialogWidth - 110, kDialogHeight - 30, 100, 20), "Yes"))
{
#if PLATFORM_ANDROID
Permission.RequestUserPermission(Permission.Microphone);
#endif
windowOpen = false;
}
}
void OnGUI ()
{
if (windowOpen)
{
Rect rect = new Rect((Screen.width / 2) - (kDialogWidth / 2), (Screen.height / 2) - (kDialogHeight / 2), kDialogWidth, kDialogHeight);
GUI.ModalWindow(0, rect, DoMyWindow, "Permissions Request Dialog");
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Microphone.Start() stops audio from playing on ios 2 Answers
Clear microphone data 3 Answers
How to check input and output of microphone? 1 Answer
Record while button pressed microphone 0 Answers
How to record last 30 seconds of audio? 0 Answers