前提・実現したいこと
unityで音声認識した文をinputfieldに表示したいのですがエラーが出てきました。どうしたら良いのでしょうか。もし実現できる方法があるならそれも教えてほしいです。
発生している問題・エラーメッセージ
get_isPlaying can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. UnityEngine.UI.InputField:set_text(String) VoiceInputTest:OnRecognition(RecognitionData) (at Assets/Scripts/VoiceInputTest.cs:47)
該当のソースコード
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class VoiceInputTest : MonoBehaviour 7{ 8 [SerializeField] InputField inputField; 9 10 private PXCMSession session; 11 private PXCMAudioSource source; 12 private PXCMSpeechRecognition sr; 13 private PXCMSpeechRecognition.Handler handler; 14 15 public void Start() 16 { 17 session = PXCMSession.CreateInstance(); 18 source = session.CreateAudioSource(); 19 20 PXCMAudioSource.DeviceInfo dinfo = null; 21 22 source.QueryDeviceInfo(0, out dinfo); 23 source.SetDevice(dinfo); 24 Debug.Log(dinfo.name); 25 26 session.CreateImpl<PXCMSpeechRecognition>(out sr); 27 28 PXCMSpeechRecognition.ProfileInfo pinfo; 29 sr.QueryProfile(out pinfo); 30 pinfo.language = PXCMSpeechRecognition.LanguageType.LANGUAGE_JP_JAPANESE; 31 sr.SetProfile(pinfo); 32 33 handler = new PXCMSpeechRecognition.Handler(); 34 handler.onRecognition = OnRecognition; 35 sr.SetDictation(); 36 sr.StartRec(source, handler); 37 } 38 39 private void OnRecognition(PXCMSpeechRecognition.RecognitionData data) 40 { 41 if(string.IsNullOrEmpty(data.scores[0].sentence)) 42 { 43 Debug.Log("-----"); 44 }else{ 45 string word = data.scores[0].sentence; 46 Debug.Log(word); 47 inputField.text = word; 48 } 49 } 50 51 void OnDisable() 52 { 53 if (sr != null) 54 { 55 sr.StopRec(); 56 sr.Dispose(); 57 } 58 59 if (session != null) 60 session.Dispose(); 61 } 62 63} 64
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー