태그 보관물: android

android

Android 휴대폰에서 성가신 대화 상자없이 음성 인식을 사용하려면 어떻게해야합니까?

Android API를 수정하지 않고도 가능합니까? 이에 대한 기사를 찾았습니다. Android API를 수정해야한다는 의견이 하나 있습니다. 그러나 수정 방법은 언급하지 않았습니다. 아무도 그렇게하는 방법에 대한 제안을 할 수 있습니까? 감사!


이 기사를 찾았습니다.
SpeechRecognizer
그의 요구는 저와 거의 같습니다. 저에게 좋은 참고 자료입니다!


이 문제는 완전히 해결되었습니다. 이 중국 웹 사이트에서
사용할 수있는 샘플 코드 를 검색했습니다.
여기에 내 소스 코드가 있습니다.

package voice.recognition.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import android.util.Log;



public class voiceRecognitionTest extends Activity implements OnClickListener 
{

   private TextView mText;
   private SpeechRecognizer sr;
   private static final String TAG = "MyStt3Activity";
   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button speakButton = (Button) findViewById(R.id.btn_speak);     
            mText = (TextView) findViewById(R.id.textView1);     
            speakButton.setOnClickListener(this);
            sr = SpeechRecognizer.createSpeechRecognizer(this);       
            sr.setRecognitionListener(new listener());        
   }

   class listener implements RecognitionListener          
   {
            public void onReadyForSpeech(Bundle params)
            {
                     Log.d(TAG, "onReadyForSpeech");
            }
            public void onBeginningOfSpeech()
            {
                     Log.d(TAG, "onBeginningOfSpeech");
            }
            public void onRmsChanged(float rmsdB)
            {
                     Log.d(TAG, "onRmsChanged");
            }
            public void onBufferReceived(byte[] buffer)
            {
                     Log.d(TAG, "onBufferReceived");
            }
            public void onEndOfSpeech()
            {
                     Log.d(TAG, "onEndofSpeech");
            }
            public void onError(int error)
            {
                     Log.d(TAG,  "error " +  error);
                     mText.setText("error " + error);
            }
            public void onResults(Bundle results)                   
            {
                     String str = new String();
                     Log.d(TAG, "onResults " + results);
                     ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                     for (int i = 0; i < data.size(); i++)
                     {
                               Log.d(TAG, "result " + data.get(i));
                               str += data.get(i);
                     }
                     mText.setText("results: "+String.valueOf(data.size()));        
            }
            public void onPartialResults(Bundle partialResults)
            {
                     Log.d(TAG, "onPartialResults");
            }
            public void onEvent(int eventType, Bundle params)
            {
                     Log.d(TAG, "onEvent " + eventType);
            }
   }
   public void onClick(View v) {
            if (v.getId() == R.id.btn_speak) 
            {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");

                intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
                     sr.startListening(intent);
                     Log.i("111111","11111111");
            }
   }
}

디버깅 후 성가신 로그를 삭제하십시오!



답변

SpeechRecognizer 인터페이스를 사용하세요 . 앱에 RECORD_AUDIO 권한이 있어야하며, 그런 다음 SpeechRecognizer를 만들고 RecognitionListener를 부여한 다음 해당 startListening메서드 를 호출 할 수 있습니다. 음성 인식기가 음성 청취를 시작할 준비가되었을 때 그리고 음성을 수신하여 텍스트로 변환 할 때 청취자에게 콜백을 받게됩니다.


답변

GAST 에는 SpeechRecognizer아주 적은 새로운 코드로 클래스 를 사용하는 데 사용할 수있는 편리한 추상 클래스가 있습니다 . 을 실행하는 예도 있습니다 SpeechRecognizer사용하여 배경 서비스로


답변

게시 해 주셔서 감사합니다! oncreate에서 onclick 리스너를 정의하는 것이 유용하다는 것을 알았습니다.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mText = (TextView) findViewById(R.id.textView1);     
    MyRecognitionListener listener = new MyRecognitionListener();
    sr = SpeechRecognizer.createSpeechRecognizer(this);       
    sr.setRecognitionListener(listener);

    findViewById(R.id.button1).setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);    
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
                intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1); 
                intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
                sr.startListening(intent);
        }
    });     
}


답변

성가신 대화없이 텍스트를 음성으로, 음성을 텍스트로 변환하는 Github 프로젝트를 만들었습니다.

https://github.com/hiteshsahu/Android-TTS-STT/tree/master/app/src/main/java/com/hiteshsahu/stt_tts/translation_engine

 //SPEECH TO TEXT DEMO
    speechToText.setOnClickListener({ view ->

        Snackbar.make(view, "Speak now, App is listening", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()

        TranslatorFactory
                .instance
                .with(TranslatorFactory.TRANSLATORS.SPEECH_TO_TEXT,
                        object : ConversionCallback {
                            override fun onSuccess(result: String) {
                                sttOutput.text = result
                            }

                            override fun onCompletion() {
                            }

                            override fun onErrorOccurred(errorMessage: String) {
                                erroConsole.text = "Speech2Text Error: $errorMessage"
                            }

                        }).initialize("Speak Now !!", this@HomeActivity)

    })


    //TEXT TO SPEECH DEMO
    textToSpeech.setOnClickListener({ view ->

        val stringToSpeak :String = ttsInput.text.toString()

        if (null!=stringToSpeak &&  stringToSpeak.isNotEmpty()) {

            TranslatorFactory
                    .instance
                    .with(TranslatorFactory.TRANSLATORS.TEXT_TO_SPEECH,
                            object : ConversionCallback {
                                override fun onSuccess(result: String) {
                                }

                                override fun onCompletion() {
                                }

                                override fun onErrorOccurred(errorMessage: String) {
                                    erroConsole.text = "Text2Speech Error: $errorMessage"
                                }

                            })
                    .initialize(stringToSpeak, this)

        } else {
            ttsInput.setText("Invalid input")
            Snackbar.make(view, "Please enter some text to speak", Snackbar.LENGTH_LONG).show()
        }

    })

여기에 이미지 설명 입력


답변