Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

TTS text to speech Android example


AD MOB

Android development example source code


// import
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
// you have to add implementation
public class Main extends Activity implements TextToSpeech.OnInitListener {
        private int _langTTSavailable = -1; // set up in onInit method
        // declaration
	private TextToSpeech mTts;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
// assigned handle - initialisation
			mTts = new TextToSpeech(this,
			        (OnInitListener) this  // TextToSpeech.OnInitListener
			        );
}

   // Implements TextToSpeech.OnInitListener.
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            // Set preferred language to US english.
             _langTTSavailable = mTts.setLanguage(Locale.US); // Locale.FRANCE etc.
            if (_langTTSavailable == TextToSpeech.LANG_MISSING_DATA ||
            	_langTTSavailable == TextToSpeech.LANG_NOT_SUPPORTED) {
             } else if ( _langTTSavailable >= 0) {
			 mTts.speak("Good morning",
            TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
            null);
            }
        } else {
            // Initialization failed.
        }
    }

    @Override
    public void onDestroy() {
        // TTS shutdown!
        if (mTts != null) {
            mTts.stop();
            mTts.shutdown();
        }

        super.onDestroy();
    }

}










Get supported language:


private TextToSpeech mTts;
// public void onInit(int status){
int result;
String s;
result = mTts.setLanguage(	Locale.	CANADA	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	CANADA	 not supported<br>"	;}else{s+="	CANADA	 supported<br>";}
result = mTts.setLanguage(	Locale.	CANADA_FRENCH	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	CANADA_FRENCH	 not supported<br>"	;}else{s+="	CANADA_FRENCH	 supported<br>";}
result = mTts.setLanguage(	Locale.	CHINA	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	CHINA	 not supported<br>"	;}else{s+="	CHINA	 supported<br>";}
result = mTts.setLanguage(	Locale.	CHINESE	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	CHINESE	 not supported<br>"	;}else{s+="	CHINESE	 supported<br>";}
result = mTts.setLanguage(	Locale.	ENGLISH	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	ENGLISH	 not supported<br>"	;}else{s+="	ENGLISH	 supported<br>";}
result = mTts.setLanguage(	Locale.	FRANCE	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	FRANCE	 not supported<br>"	;}else{s+="	FRANCE	 supported<br>";}
result = mTts.setLanguage(	Locale.	FRENCH	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	FRENCH	 not supported<br>"	;}else{s+="	FRENCH	 supported<br>";}
result = mTts.setLanguage(	Locale.	GERMAN	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	GERMAN	 not supported<br>"	;}else{s+="	GERMAN	 supported<br>";}
result = mTts.setLanguage(	Locale.	GERMANY	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	GERMANY	 not supported<br>"	;}else{s+="	GERMANY	 supported<br>";}
result = mTts.setLanguage(	Locale.	ITALIAN	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	ITALIAN	 not supported<br>"	;}else{s+="	ITALIAN	 supported<br>";}
result = mTts.setLanguage(	Locale.	ITALY	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	ITALY	 not supported<br>"	;}else{s+="	ITALY	 supported<br>";}
result = mTts.setLanguage(	Locale.	JAPAN	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	JAPAN	 not supported<br>"	;}else{s+="	JAPAN	 supported<br>";}
result = mTts.setLanguage(	Locale.	JAPANESE	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	JAPANESE	 not supported<br>"	;}else{s+="	JAPANESE	 supported<br>";}
result = mTts.setLanguage(	Locale.	KOREA	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	KOREA	 not supported<br>"	;}else{s+="	KOREA	 supported<br>";}
result = mTts.setLanguage(	Locale.	KOREAN	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	KOREAN	 not supported<br>"	;}else{s+="	KOREAN	 supported<br>";}
result = mTts.setLanguage(	Locale.	PRC	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	PRC	 not supported<br>"	;}else{s+="	PRC	 supported<br>";}
result = mTts.setLanguage(	Locale.	ROOT	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	ROOT	 not supported<br>"	;}else{s+="	ROOT	 supported<br>";}
result = mTts.setLanguage(	Locale.	SIMPLIFIED_CHINESE	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	SIMPLIFIED_CHINESE	 not supported<br>"	;}else{s+="	SIMPLIFIED_CHINESE	 supported<br>";}
result = mTts.setLanguage(	Locale.	TAIWAN	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	TAIWAN	 not supported<br>"	;}else{s+="	TAIWAN	 supported<br>";}
result = mTts.setLanguage(	Locale.	TRADITIONAL_CHINESE	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	TRADITIONAL_CHINESE	 not supported<br>"	;}else{s+="	TRADITIONAL_CHINESE	 supported<br>";}
result = mTts.setLanguage(	Locale.	UK	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	UK	 not supported<br>"	;}else{s+="	UK	 supported<br>";}
result = mTts.setLanguage(	Locale.	US	);	if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s +=	 "	US	 not supported<br>"	;}else{s+="	US	 supported<br>";}


397LW NO topic_id




AD

Další témata ....(Topics)


307

Updating Android SDK failed: "A folder failed to be moved.” | updating-android-sdk-%e2%80%9ca-folder-failed-to-be-moved-%e2%80%9d


Try:
- close Eclipse and continue updating
or
- change permission of folder
or
- disable antivirus
or
- copy manually from zip packages from temp
c:\Program Files\android\sdk\temp\
c:\Program Files\android\sdk\temp\platform-tools_r19.0.1-windows.zip
c:\Program Files\android\sdk\temp\tools_r22.6.2-windows.zip
into propriety folders
or
- download all SDK package and replace old
https://developer.android.com/sdk/index.html?hl=sk
311

findViewById in onCreate returns NULL when using Fragment | findviewbyid-in-oncreate-returns-null-when-using-fragment


Call findViewById from onCreateView

public class MainActivity extends ActionBarActivity {

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

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.containerMoje, new PlaceholderFragment()).commit();
		}
		
	}


	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {
		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			if (container == null) {
		        return null;
		    }


			 TextView mText;
			mText = (TextView)rootView.findViewById(R.id.idText);
			mText.setText("Hello from fragment_main");

			return rootView;
		}
	}
}




res\layout
activity_main.xml

<FrameLayout xmlns:android="//schemas.android.com/apk/res/android"
    xmlns:tools="//schemas.android.com/tools"
    android:id="@+id/containerMoje"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cz.okhelp.autoskola.MainActivity"
    tools:ignore="MergeRootFrame" />



fragment_main.xml

<RelativeLayout xmlns:android="//schemas.android.com/apk/res/android"
    xmlns:tools="//schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cz.okhelp.autoskola.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/idText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
149

Convert a Bitmap to Gray Scale Android example | convert-a-bitmap-to-gray-scale-android-example


Black and white bitmap image pictures, gray scale colormatrix Android example.

public class ApokusActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {

		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);

		}

		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();

			canvas.drawColor(Color.YELLOW);


			// you need to insert a image flower_blue into res/drawable folder

			paint.setFilterBitmap(true);
			Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
					R.drawable.flower_blue);
			canvas.drawBitmap(bitmapOrg, 10, 10, paint);
			int width, height;

		    ColorMatrix cm = new ColorMatrix(
		    		new float[]{
		    		0.5f,0.5f,0.5f,0,0,
		            0.5f,0.5f,0.5f,0,0,
		            0.5f,0.5f,0.5f,0,0,
		            0,0,0,1,0,0,
		            0,0,0,0,1,0
		            });
		    cm.setSaturation(0);
		    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
		    paint.setColorFilter(f);
		    int h = bitmapOrg.getHeight();
			//canvas.drawBitmap(bitmapOrg, 10, 10, paint);
			canvas.drawBitmap(bitmapOrg, 10, 10 + h + 10, paint);

		}

	}
}




gry scale bitmap android
242

Replace graphic accent diacritic Java Function | replace-graphic-accent-java-function


Replace diacritic marks: Á Č Ď É Ě Í Ň Ó Ř Š Ť Ú Ů Ý Ž
á č ď é ě í ň ó ř š ť ú ů ý ž


public String replaceDiacritic (String inputStr) {
	
	Map<String, String> replacements = new LinkedHashMap<String,String>() {{
		//Velká
		put("Á","A");
		put("Č","C");
		put("Ď","D");
		put("É","E");
		put("Ě","E");
		put("Í","I");
		put("Ň","N");
		put("Ó","O");
		put("Ř","R");
		put("Š","S");
		put("Ť","T");
		put("Ú","U");
		put("Ů","U");
		put("Ý","Y");
		put("Ž","Z");
		//Malá  "," ");
		put("á","a");
		put("č","c");
		put("ď","d");
		put("é","e");
		put("ě","e");
		put("í","i");
		put("ň","n");
		put("ó","o");
		put("ř","r");
		put("š","s");
		put("ť","t");
		put("ú","u");
		put("ů","u");
		put("ý","y");
		put("ž","z");
		}
	};
	for(Map.Entry<String, String> entry : replacements.entrySet()) {
          inputStr =  inputStr.replaceAll(entry.getKey(), entry.getValue());
         }	
	return inputStr;
}
	
244

Include xml into another xml Android Example | include-xml-into-another-xml-android-example


How include layout defined in other xml file into another xml file example:
res/layout/my_layout.xml into
main.xml

<include  layout="@layout/my_layout" android:id="@+id/idMyLayout" />


Editace: 2011-10-28 09:38:58
Počet článků v kategorii: 397
Url:tts-text-to-speech

AD
11 Share
Share
Tweet