質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

2回答

1815閲覧

unityでの音声認識した文を表示したいのですが…

koppen54

総合スコア15

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2019/03/03 11:37

編集2019/03/04 09:09

前提・実現したいこと

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/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Bongo

2019/03/04 10:57

コンソール上のエラーメッセージを選択すると下部に全文が表示されるかと思いますが、ご提示いただいた「VoiceInputTest:OnRecognition(RecognitionData) (at Assets/Scripts/VoiceInputTest.cs:47)」の先には続きがあるでしょうか? もしありましたら、それもご提示いただくともしかしたら手がかりになるかもしれません。
koppen54

2019/03/04 11:22

HandlerDIR:OnRecognition(IntPtr)と書かれてありました
guest

回答2

0

get_isPlaying can only be called from the main thread.

google翻訳
get_isPlayingはメインスレッドからしか呼び出せません。

投稿2019/03/03 11:39

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fiveHundred

2019/03/04 09:59

なぜか低評価が付いておりますが、Unityではマルチスレッドはあまり考慮されておらず、メインスレッド以外では多くのUnityの関数が使えなくなるので、この回答は間違っていないと思います。
guest

0

ベストアンサー

コードが似ていましたのでUnityで手っ取り早くマイクの音声を文字列にする - Qiitaを参考に実験してみましたところ、確かにy_waiwaiさん、fiveHundredさんのご指摘のようにOnRecognitionは別スレッドで動作しているようでした。
OnRecognition内では取得した文字列を記録しておくだけにして、メインスレッド...例えばUpdate内で取り出すといった処置が必要かと思います。

C#

1using System.Collections; 2using System.Collections.Concurrent; 3using System.Collections.Generic; 4using System.Threading; 5using UnityEngine; 6using UnityEngine.UI; 7 8public class VoiceInputTest : MonoBehaviour 9{ 10 [SerializeField] InputField inputField; 11 12 private PXCMSession session; 13 private PXCMAudioSource source; 14 private PXCMSpeechRecognition sr; 15 private PXCMSpeechRecognition.Handler handler; 16 17 // 認識したテキストを格納するキューを追加 18 private readonly ConcurrentQueue<string> recognizedTexts = new ConcurrentQueue<string>(); 19 20 public void Start() 21 { 22 session = PXCMSession.CreateInstance(); 23 source = session.CreateAudioSource(); 24 25 PXCMAudioSource.DeviceInfo dinfo = null; 26 27 source.QueryDeviceInfo(0, out dinfo); 28 source.SetDevice(dinfo); 29 Debug.Log(dinfo.name); 30 31 session.CreateImpl<PXCMSpeechRecognition>(out sr); 32 33 PXCMSpeechRecognition.ProfileInfo pinfo; 34 sr.QueryProfile(out pinfo); 35 pinfo.language = PXCMSpeechRecognition.LanguageType.LANGUAGE_JP_JAPANESE; 36 sr.SetProfile(pinfo); 37 38 handler = new PXCMSpeechRecognition.Handler(); 39 handler.onRecognition = OnRecognition; 40 sr.SetDictation(); 41 sr.StartRec(source, handler); 42 43 Debug.Log($"Start[Thread {Thread.CurrentThread.ManagedThreadId}]"); 44 } 45 46 private void OnRecognition(PXCMSpeechRecognition.RecognitionData data) 47 { 48 if (string.IsNullOrEmpty(data.scores[0].sentence)) 49 { 50 Debug.Log($"OnRecognition[Thread {Thread.CurrentThread.ManagedThreadId}]: -----"); 51 } 52 else 53 { 54 string word = data.scores[0].sentence; 55 Debug.Log($"OnRecognition[Thread {Thread.CurrentThread.ManagedThreadId}]: {word}"); 56 57 // UIの操作はここでは行わず、テキストをキューに追加するだけにする 58 // inputField.text = word; 59 recognizedTexts.Enqueue(word); 60 } 61 } 62 63 private void Update() 64 { 65 // Update内でキューを確認し、中身を取り出してインプットフィールドに追記する 66 string word; 67 while (recognizedTexts.TryDequeue(out word)) 68 { 69 if (!string.IsNullOrEmpty(word)) 70 { 71 Debug.Log($"Update[Thread {Thread.CurrentThread.ManagedThreadId}]: {word}"); 72 inputField.text += word; 73 } 74 } 75 } 76 77 void OnDisable() 78 { 79 if (sr != null) 80 { 81 sr.StopRec(); 82 sr.Dispose(); 83 } 84 85 if (session != null) 86 { 87 session.Dispose(); 88 } 89 } 90}

結果

投稿2019/03/04 21:10

Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問