Skip to content

Commit 7e9180a

Browse files
committed
renamed MainActivity.java class to SpeechRecognizerFragment, now it works as a Fragment not like a UnityPlayerActivity
1 parent b2d91b0 commit 7e9180a

File tree

1 file changed

+69
-49
lines changed

1 file changed

+69
-49
lines changed

UnitySpeechRecognizerPlugin/SpeechRecognizer/src/main/java/com/example/eric/unityspeechrecognizerplugin/MainActivity.java renamed to UnitySpeechRecognizerPlugin/SpeechRecognizer/src/main/java/com/example/eric/unityspeechrecognizerplugin/SpeechRecognizerFragment.java

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,57 @@
11
package com.example.eric.unityspeechrecognizerplugin;
22

33
import android.Manifest;
4+
import android.app.Fragment;
45
import android.content.ActivityNotFoundException;
56
import android.content.Intent;
67
import android.content.pm.PackageManager;
78
import android.os.Bundle;
89
import android.speech.RecognitionListener;
910
import android.speech.RecognizerIntent;
1011
import android.speech.SpeechRecognizer;
11-
import androidx.core.content.ContextCompat;
12-
import androidx.core.app.ActivityCompat;
1312
import android.util.Log;
13+
1414
import com.unity3d.player.UnityPlayer;
15-
import com.unity3d.player.UnityPlayerActivity;
15+
1616
import java.util.ArrayList;
1717
import java.util.Locale;
1818

19+
import androidx.annotation.Nullable;
20+
import androidx.core.app.ActivityCompat;
21+
import androidx.core.content.ContextCompat;
22+
1923
/**
2024
* Created by Eric on 27/03/2020.
2125
*/
2226

23-
public class MainActivity extends UnityPlayerActivity
27+
public class SpeechRecognizerFragment extends Fragment
2428
{
29+
//Tag to follow logcats
2530
private static final String TAG = "SpeechRecognizer";
31+
// Singleton instance.
32+
public static SpeechRecognizerFragment instance;
33+
34+
//UNITY CONTEXT
35+
String gameObjectName;
36+
String resultCallbackName = "OnResult";
37+
public static void SendUnityResults(String results)
38+
{
39+
UnityPlayer.UnitySendMessage(instance.gameObjectName, instance.resultCallbackName, results);
40+
Log.d(TAG, results);
41+
}
42+
public static void SetUp(String gameObjectName)
43+
{
44+
instance = new SpeechRecognizerFragment();
45+
instance.gameObjectName = gameObjectName; // Store 'GameObject' reference
46+
UnityPlayer.currentActivity.getFragmentManager().beginTransaction().add(instance, SpeechRecognizerFragment.TAG).commit();
47+
}
2648

27-
//ANDROID BRIDGE
28-
public static String androidBridgeGameObjectName = "AndroidBridge";
49+
@Override
50+
public void onCreate(@Nullable Bundle savedInstanceState) {
51+
super.onCreate(savedInstanceState);
52+
setRetainInstance(true);
53+
RequestRecordAudioPermission();
54+
}
2955

3056
//SPEECH RECOGNIZER
3157
public SpeechRecognizer sr;
@@ -36,21 +62,14 @@ public class MainActivity extends UnityPlayerActivity
3662
private static String glanguage = "en-US";
3763
private static int gMaxResults = 10;
3864

39-
40-
@Override
41-
protected void onCreate(Bundle savedInstanceState)
42-
{
43-
super.onCreate(savedInstanceState);
44-
RequestRecordAudioPermission();
45-
}
4665
private void RequestRecordAudioPermission()
4766
{
4867
// Here, thisActivity is the current activity
49-
if (ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)
68+
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)
5069
{
5170
// Permission is not granted
5271
// Should we show an explanation?
53-
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECORD_AUDIO))
72+
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.RECORD_AUDIO))
5473
{
5574
// Show an explanation to the user *asynchronously* -- don't block
5675
// this thread waiting for the user's response! After the user
@@ -59,7 +78,7 @@ private void RequestRecordAudioPermission()
5978
else
6079
{
6180
// No explanation needed; request the permission
62-
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},20);
81+
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.RECORD_AUDIO},20);
6382
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
6483
// app-defined int constant. The callback method gets the
6584
// result of the request.
@@ -72,42 +91,40 @@ private void RequestRecordAudioPermission()
7291
}
7392
}
7493

75-
//ANDROID BRIDGE
76-
public static void SendUnityResults(String results)
94+
public void StartListening()
7795
{
78-
UnityPlayer.UnitySendMessage(androidBridgeGameObjectName, "OnResult", results);
79-
Log.d(TAG, results);
96+
getActivity().runOnUiThread(new Runnable() {
97+
@Override
98+
public void run() {
99+
Log.d(TAG, "StartListeningCalled");
100+
sr = SpeechRecognizer.createSpeechRecognizer(getActivity());
101+
sr.setRecognitionListener(speechListener);
102+
103+
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
104+
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
105+
if (languageNotSet)
106+
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
107+
else
108+
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, glanguage);
109+
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, gMaxResults);
110+
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, gQuestion);
111+
112+
try
113+
{
114+
sr.startListening(intent);
115+
}
116+
catch (ActivityNotFoundException a)
117+
{
118+
Log.d(TAG, a.toString());
119+
}
120+
}
121+
});
80122
}
81-
82-
//SPEECH RECOGNIZER
83123
public void StopListening()
84124
{
85125
sr.destroy();
86126
sr = null;
87127
}
88-
public void StartListening()
89-
{
90-
sr = SpeechRecognizer.createSpeechRecognizer(this);
91-
sr.setRecognitionListener(speechListener);
92-
93-
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
94-
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
95-
if (languageNotSet)
96-
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
97-
else
98-
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, glanguage);
99-
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, gMaxResults);
100-
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, gQuestion);
101-
102-
try
103-
{
104-
sr.startListening(intent);
105-
}
106-
catch (ActivityNotFoundException a)
107-
{
108-
Log.d(TAG, a.toString());
109-
}
110-
}
111128
public void RestartListening()
112129
{
113130
StopListening();
@@ -118,6 +135,7 @@ public void SetContinuousListening(boolean isContinuous)
118135
speechListener.continuousListening = isContinuous;
119136
}
120137

138+
//SPEECH RECOGNIZER_LISTENER
121139
class SpeechRecognitionListener implements RecognitionListener
122140
{
123141
private ArrayList<String> resultData = new ArrayList<>();
@@ -127,10 +145,12 @@ public void onReadyForSpeech(Bundle params) {
127145
Log.d(TAG, "onReadyForSpeech");
128146
}
129147

130-
public void onBeginningOfSpeech() {Log.d(TAG, "onBeginningOfSpeech");}
148+
public void onBeginningOfSpeech() {
149+
Log.d(TAG, "onBeginningOfSpeech");
150+
}
131151

132152
public void onRmsChanged(float rmsdB) {
133-
//Log.d(TAG, "onRmsChanged");
153+
/*Log.d(TAG, "onRmsChanged");*/
134154
}
135155

136156
public void onBufferReceived(byte[] buffer) {
@@ -159,6 +179,7 @@ public void onResults(Bundle results)
159179
str.append("~").append(resultData.get(i));
160180
}
161181
}
182+
162183
SendUnityResults(str.toString());
163184
if(continuousListening)
164185
RestartListening();
@@ -172,5 +193,4 @@ public void onEvent(int eventType, Bundle params) {
172193
Log.d(TAG, "onEvent " + eventType);
173194
}
174195
}
175-
176-
}
196+
}

0 commit comments

Comments
 (0)