آموزش تبدیل متن به صدا در اندروید استودیو به صورت ساده و روان

آموزش تبدیل متن به صدا در اندروید استودیو به صورت ساده و روان :

Android به شما امکان می دهد متن خود را به صدا تبدیل کنید. نه تنها شما می توانید آن را تبدیل کنید بلکه به شما امکان می دهد متن را با زبان های مختلف صحبت کنید.
Android برای این منظور کلاس TextToSpeech را ارائه می دهد. برای استفاده از این کلاس ، باید یک شیء از این کلاس را فوری کنید و همچنین initListener را مشخص کنید. نحو آن در زیر آورده شده است –
private EditText write;ttobj=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {   @Override   public void onInit(int status) {   }});

در این شنونده ، شما باید خصوصیات شیء TextToSpeech مانند زبان آن ، pt e.t.c. را مشخص کنید. زبان را می توان با فراخوانی متد setLanguage () تنظیم کرد. نحو آن در زیر آورده شده است –
ttobj.setLanguage(Locale.UK);

پس از تنظیم زبان ، می توانید با روش صحبت کلاس تماس بگیرید تا متن را صحبت کنید. نحو آن در زیر آورده شده است –

ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);

کد های MainActivity.java.  برای تبدیل متن به صدا در اندروید
package com.example.sairamkrishna.myapplication;import android.app.Activity;import android.os.Bundle;import android.speech.tts.TextToSpeech;import android.view.View;import android.widget.Button;import android.widget.EditText;import java.util.Locale;import android.widget.Toast;public class MainActivity extends Activity {   TextToSpeech t1;   EditText ed1;   Button b1;   @Override   protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);      ed1=(EditText)findViewById(R.id.editText);      b1=(Button)findViewById(R.id.button);      t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {         @Override         public void onInit(int status) {            if(status != TextToSpeech.ERROR) {               t1.setLanguage(Locale.UK);            }         }      });      b1.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {            String toSpeak = ed1.getText().toString();            Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();            t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);         }      });   }   public void onPause(){      if(t1 !=null){         t1.stop();         t1.shutdown();      }      super.onPause();   }}

کد های activity_main.xml
<?xml version=”1.0″ encoding=”utf-8″?><RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”   xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent”   android:layout_height=”match_parent” android:paddingLeft=”@dimen/activity_horizontal_margin”   android:paddingRight=”@dimen/activity_horizontal_margin”   android:paddingTop=”@dimen/activity_vertical_margin”   android:paddingBottom=”@dimen/activity_vertical_margin”   tools:context=”.MainActivity”   android:transitionGroup=”true”>   <TextView android:text=”Text to Speech” android:layout_width=”wrap_content”      android:layout_height=”wrap_content”      android:id=”@+id/textview”      android:textSize=”35dp”      android:layout_alignParentTop=”true”      android:layout_centerHorizontal=”true” />   <TextView      android:layout_width=”wrap_content”      android:layout_height=”wrap_content”      android:text=”Tutorials point”      android:id=”@+id/textView”      android:layout_below=”@+id/textview”      android:layout_centerHorizontal=”true”      android:textColor=”#ff7aff24″      android:textSize=”35dp” />   <ImageView      android:layout_width=”wrap_content”      android:layout_height=”wrap_content”      android:id=”@+id/imageView”      android:src=”@drawable/abc”      android:layout_below=”@+id/textView”      android:layout_centerHorizontal=”true”      android:theme=”@style/Base.TextAppearance.AppCompat” />   <EditText      android:layout_width=”wrap_content”      android:layout_height=”wrap_content”      android:id=”@+id/editText”      android:layout_below=”@+id/imageView”      android:layout_marginTop=”46dp”      android:hint=”Enter Text”      android:layout_alignParentRight=”true”      android:layout_alignParentEnd=”true”      android:layout_alignParentLeft=”true”      android:layout_alignParentStart=”true”      android:textColor=”#ff7aff10″      android:textColorHint=”#ffff23d1″ />   <Button      android:layout_width=”wrap_content”      android:layout_height=”wrap_content”      android:text=”Text to Speech”      android:id=”@+id/button”      android:layout_below=”@+id/editText”      android:layout_centerHorizontal=”true”      android:layout_marginTop=”46dp” /></RelativeLayout>

کد های Strings.xml.
<resources>   <string name=”app_name”>My Application</string></resources>

کد های AndroidManifest.xml
<?xml version=”1.0″ encoding=”utf-8″?><manifest xmlns:android=”http://schemas.android.com/apk/res/android”   package=”com.example.sairamkrishna.myapplication” >   <application      android:allowBackup=”true”      android:icon=”@mipmap/ic_launcher”      android:label=”@string/app_name”      android:theme=”@style/AppTheme” >      <activity         android:name=”.MainActivity”         android:label=”@string/app_name” >         <intent-filter>            <action android:name=”android.intent.action.MAIN” >            <category android:name=”android.intent.category.LAUNCHER” />         </intent-filter>      </activity>   </application></manifest>

این را نیز بخوانید
بیایید سعی کنیم برنامه خود را اجرا کنیم. به نظر من دستگاه واقعی Android Mobile را با رایانه خود متصل کرده اید. برای اجرای برنامه از استودیوی Android ، یکی از پرونده های فعالیت پروژه خود را باز کرده و از نوار ابزار ، روی گزینه Run Eclipse Run Icon کلیک کنید. قبل از شروع برنامه ، استودیوی اندیشه پنجره زیر را نمایش می دهد تا گزینه ای را انتخاب کنید که می خواهید برنامه Android خود را اجرا کنید.

خب کار تمام تست الان باید همه چی خوب پیش بره ، برنامه رو نصب کنید و تست کنید مشکلی داشتید تو بخش دیدگاه بنویسید

 

مطلب مفیدی برای شما بود ؟ پس به اشتراک بگذارید برای دوستانتان

codesrcReza

109 مطلب منتشر شده

درباره این مطلب نظر دهید !