-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathSampleVoice.cs
More file actions
107 lines (92 loc) · 3.44 KB
/
Copy pathSampleVoice.cs
File metadata and controls
107 lines (92 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Meta.XR.Samples;
using System.Collections;
using Oculus.Voice;
using TMPro;
using UnityEngine;
using UnityEngine.Android;
namespace TheWorldBeyond.SampleScenes
{
[MetaCodeSample("TheWorldBeyond")]
public class SampleVoice : MonoBehaviour
{
[SerializeField]
private AppVoiceExperience m_voiceExperience;
public TextMeshProUGUI ThoughtText;
public GameObject[] DemoObjects;
public GameObject NotifScreen;
private const string DEFAULT_MESSAGE = "(press to begin transcribing)";
private void OnEnable()
{
m_voiceExperience.VoiceEvents.OnStartListening.AddListener(StartListening);
m_voiceExperience.VoiceEvents.OnStoppedListening.AddListener(StopListening);
m_voiceExperience.VoiceEvents.OnPartialTranscription.AddListener(LiveTranscriptionHandler);
ThoughtText.text = DEFAULT_MESSAGE;
// ensure the experience is hidden by default, until user has agreed to the notif
foreach (var obj in DemoObjects)
{
obj.SetActive(false);
}
}
private void OnDisable()
{
m_voiceExperience.VoiceEvents.OnStartListening.RemoveListener(StartListening);
m_voiceExperience.VoiceEvents.OnStoppedListening.RemoveListener(StopListening);
m_voiceExperience.VoiceEvents.OnPartialTranscription.RemoveListener(LiveTranscriptionHandler);
}
public void BeginTranscription()
{
m_voiceExperience.Activate();
}
private void StartListening()
{
ThoughtText.text = "(begin talking)";
}
private void StopListening()
{
_ = StartCoroutine(ClearText(3));
}
private void LiveTranscriptionHandler(string content)
{
ThoughtText.text = content;
}
private IEnumerator ClearText(float countdown)
{
yield return new WaitForSeconds(countdown);
ThoughtText.text = DEFAULT_MESSAGE;
}
// this is only called from the UI button
public void CheckPermissionsAndContinue(GameObject notifScreen)
{
// user should have already seen permission screen upon initial run
// pop it up again if permission's still not granted
if (Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
StartExperience(Permission.Microphone);
}
else
{
// display the Android permission screen, with a callback
// if user denies permission, our notif screen is still visible
// if you want to provide more user feedback, add PermissionDenied callback here
var callbacks = new PermissionCallbacks();
callbacks.PermissionGranted += StartExperience;
Permission.RequestUserPermission(Permission.Microphone, callbacks);
}
}
private void StartExperience(string permissionName)
{
if (permissionName == Permission.Microphone)
{
foreach (var obj in DemoObjects)
{
obj.SetActive(true);
}
if (NotifScreen)
{
NotifScreen.SetActive(false);
}
}
}
}
}