Hurriyet Spor

Bumerang - Yazarkafe

Android Metin Okuma TTS Örnek Kod

Android uygulamamızda metinleri sesli olarak seslendirebilmek için Google TTS motorundan faydanalabiliriz. Android cihazlarda yüklü farklı dil paketlerinin olduğu desteklenen dillerden birini seçebilirsiniz. Aşağıda dil olarak Türkçe seçilmiştir.

       


package com.konusma;



import android.app.Activity;




import android.os.Bundle;

import android.util.Log;
import android.view.Menu;
import android.view.View;

import android.widget.Button;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import android.speech.tts.TextToSpeech;


public class MainActivity extends Activity implements
TextToSpeech.OnInitListener { 
   
    Button bCevir;
  
    TextToSpeech tts;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        tts = new TextToSpeech(this, this);
        speakOut();
        bCevir = (Button) findViewById(R.id.bCevir);
        
        
        bCevir.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
              speakOut();
            }
        });
 
        
    }
 
    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    
    
    public void onInit(int status) {
 
        if (status == TextToSpeech.SUCCESS) {
 
        Locale locale = new Locale("tr", "TR");
          int result = tts.setLanguage(Locale.getDefault());
   
          result = tts.setLanguage(locale);
        Log.i("-------------",Arrays.toString(locale.getISOCountries()));
          
          
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
             
                speakOut();
            }
 
        } else {
            Log.e("TTS", "Initilization Failed!");
        }
 
    }
    
    
    private void speakOut() {
     
  
      
   try {
    tts.speak( URLDecoder.decode("Merhaba Kullanıcı", "utf-8"), TextToSpeech.QUEUE_FLUSH, null);
   } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  
        
    }
  
 

 
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}


       
 

Hiç yorum yok:

Yorum Gönder