2017-03-23 7 views
2

해당 클래스의 speak 함수를 호출하면 UtteranceProgressListener이 호출되지만 수신기의 메서드는 onStart(), onDone()onError()이 호출되지 않습니다.왜 UtteranceProgressListener의 함수가 호출되지 않습니까?

궁극적으로 우리는 텍스트를 음성으로 일시 중지하고 재생 일시 중지 단추를 사용하여 같은 문장에서 다시 시작할 수있는 코드를 작성하려고합니다. speak()를 호출하기 전에

package com.example.android.m_tour; 

import android.content.Intent; 
import android.speech.tts.TextToSpeech; 
import android.speech.tts.UtteranceProgressListener; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 


import com.wnafee.vector.MorphButton; 

import java.util.HashMap; 
import java.util.Locale; 

import static com.example.android.m_tour.R.id.mb; 


public class guide extends AppCompatActivity implements MorphButton.OnStateChangedListener { 

    private Button settingsButton; 
    private String data, s; 
    private TextView textView; 
    private static TextToSpeech textToSpeech; 

    //qr code scanner object 
    private MainActivity mainActivity; 
    private TTSsettings ttSsettings; 
    private MorphButton control; 
    private Locale loc; 
    private String pr[]; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_guide); 
     control = (MorphButton) findViewById(mb); 
     mainActivity = new MainActivity(); 
     textView = (TextView) findViewById(R.id.textview); 
     textView.setText(mainActivity.getData()); 
     settingsButton = (Button) findViewById(R.id.settings); 
     ttSsettings = new TTSsettings(); 
     loc = new Locale("en_IN"); 

     control.setOnStateChangedListener(this); 
     settingsButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       Intent intent = new Intent(guide.this, TTSsettings.class); 
       startActivity(intent); 
       /*textToSpeech.stop(); 
       Intent intent = new Intent(); 
       intent.setAction("com.android.settings"); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(intent);*/ 
      } 
     }); 
     data = mainActivity.getData(); 
     onStart(); 

     textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() { 
      @Override 
      public void onInit(int i) { 
       if (i == TextToSpeech.SUCCESS) { 
        //set language Locale to US 
        int result = textToSpeech.setLanguage(loc); 
        //check that is language locale available on device or supported 
        if (result == TextToSpeech.LANG_MISSING_DATA 
          || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
         Toast.makeText(guide.this, "Leanguage not present", Toast.LENGTH_SHORT).show(); 
        } else { 
         speakOut(); 
        } 

       } else { 
        //show toast if initialization failed 
        Toast.makeText(getBaseContext(), "TTS Engine Initilization Failed!", Toast.LENGTH_SHORT).show(); 
       } 

       textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() { 
        @Override 
        public void onStart(String s) { 
         //if ("id".equals(s) 
         Toast.makeText(guide.this, "onStart works", Toast.LENGTH_SHORT).show(); 

        } 

        @Override 
        public void onDone(String s) { 
         //if ("id".equals(s)) 
          Toast.makeText(guide.this, "onDone works", Toast.LENGTH_SHORT).show(); 
        } 

        @Override 
        public void onError(String s) { 
         //if ("id".equals(s)) 
          Toast.makeText(guide.this, "OnError works", Toast.LENGTH_SHORT).show(); 
        } 
       }); 

      } 
     }); 
    } 

    @Override 
    public void onDestroy() { 
     // Don't forget to stop and shutdown text to speech engine! 
     if (textToSpeech != null) { 
      textToSpeech.stop(); 
      textToSpeech.shutdown(); 
     } 
     super.onDestroy(); 
    } 

    @Override 
    public void onStateChanged(MorphButton.MorphState changedTo, boolean isAnimating) { 
     if (changedTo == MorphButton.MorphState.END) { 
      textToSpeech.stop(); 
     } else { 
      speakOut(); 

     } 
    } 

    private void speakOut() { 
     HashMap<String, String> params = new HashMap<String, String>(); 
     params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "id"); 
     textToSpeech.setPitch((float) ttSsettings.getPitchRate()); 
     textToSpeech.setSpeechRate((float) ttSsettings.getSpeechRate()); 
     textToSpeech.speak(pr[1], TextToSpeech.QUEUE_FLUSH, params); 
     } 


    //getters 
    public String getData() { 
     return data; 
    } 

    public static TextToSpeech getTextToSpeech() { 
     return textToSpeech; 
    } 
} 

답변

2

setOnUtteranceProgressListener() 메서드를 호출 할 필요가 있습니다. 이제 speakOut() 메서드를 먼저 호출하고 이후에야 수신기를 설정합니다. 다른 방향으로 회전해야합니다.

textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() { 
    @Override 
    public void onInit(int i) { 
     if (i == TextToSpeech.SUCCESS) { 
      //set language Locale to US 
      int result = textToSpeech.setLanguage(loc); 
      //check that is language locale available on device or supported 
      if (result == TextToSpeech.LANG_MISSING_DATA 
        || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
       Toast.makeText(guide.this, "Leanguage not present", Toast.LENGTH_SHORT).show(); 
      } else { 
       textToSpeech.setOnUtteranceProgressListener(
        new UtteranceProgressListener() { 
        @Override 
        public void onStart(String s) { 
         //if ("id".equals(s) 
         Toast.makeText(guide.this, "onStart works", Toast.LENGTH_SHORT).show(); 
        } 

        @Override 
        public void onDone(String s) { 
         //if ("id".equals(s)) 
         Toast.makeText(guide.this, "onDone works", Toast.LENGTH_SHORT).show(); 
        } 

        @Override 
        public void onError(String s) { 
         //if ("id".equals(s)) 
         Toast.makeText(guide.this, "OnError works", Toast.LENGTH_SHORT).show(); 
        } 
       });        

       speakOut(); 
      } 
     } else { 
      //show toast if initialization failed 
      Toast.makeText(getBaseContext(), "TTS Engine Initilization Failed!", Toast.LENGTH_SHORT).show(); 
     } 
    } 
}); 
관련 문제