2017-02-28 5 views
-2

アンドロイドアプリケーションでオーディオファイルを再生したいときのように、メディアプレーヤーを使用して生のフォルダからオーディオファイルを取得し、単に再生します。私がしたいことは、私が文字列データからオーディオを再生するようなものです。例: 文字列値= "Hello sir";文字列データを再生します。 (オーディオとして再生文字列の値)

私はこの文字列 "hello sir"をオーディオとして再生したいと思います。

+0

検索 "テキスト読み上げ" –

+0

@SergeyGlotovのおかげ –

答えて

0

を使用できTextToSpeech

public class MainActivity extends AppCompatActivity { 

    private TextToSpeech mTextToSpeech; 

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

     mTextToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { 
      @Override 
      public void onInit(int status) { 
       if (status != TextToSpeech.ERROR) { 
        mTextToSpeech.setLanguage(Locale.UK); 
        speak("Hello sir"); 
       } 
      } 
     }); 
    } 

    private void speak(String text) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      textToSpeechGreater21(text); 
     } else { 
      textToSpeechUnder20(text); 
     } 
    } 

    @SuppressWarnings("deprecation") 
    private void textToSpeechUnder20(String text) { 
     HashMap<String, String> map = new HashMap<>(); 
     map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId"); 
     mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, map); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    private void textToSpeechGreater21(String text) { 
     String utteranceId = this.hashCode() + ""; 
     mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId); 
    } 
} 
関連する問題