« Android7 の x86版 を仮想環境にインストール | メイン | エンターでデフォルトアクションを指定する方法 »

Android

Android音声合成の基本形

 android.speech.tts.TextToSpeech でテキストを読み上げる最小限サンプル。
Githubのソースは AndroidStudio2.3.3 Basic Activityテンプレートが元。
app/build.gradle

compile 'com.android.support:appcompat-v7:24.+'
compile 'com.android.support:design:24.+'

としてAndroid2.3 以上に対応。ポイントは...▼
全ソースGithub
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import java.util.Locale;
import android.widget.Toast;
import android.content.Context;
import android.speech.tts.TextToSpeech;

public class MainActivity extends AppCompatActivity{
    private TextToSpeech textToSpeech;

    class Text2SpeechListener implements TextToSpeech.OnInitListener{
        @Override
        public void onInit( int status ){
            Context context = getApplicationContext();
            if( TextToSpeech.SUCCESS==status ){
                Locale locale = Locale.getDefault();
                if( textToSpeech.isLanguageAvailable( locale ) >= TextToSpeech.LANG_AVAILABLE ){
                    textToSpeech.setLanguage( locale );
                }else{
                    Toast.makeText( context, "need speech data " + locale.getDisplayName(), Toast.LENGTH_SHORT ).show();
                }
            }else{
                Toast.makeText( context, "need TextToSpeech Engine", Toast.LENGTH_SHORT ).show();
            }
        }
    }

    public void onClickSpeek( View v ){
        if( textToSpeech.isSpeaking() ){
            textToSpeech.stop();
        }
        textToSpeech.speak( "pen pineapple apple pen", TextToSpeech.QUEUE_FLUSH, null );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ){
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar );
        setSupportActionBar( toolbar );

        textToSpeech = new TextToSpeech( this, new Text2SpeechListener() );//★
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if( null != textToSpeech ){
            textToSpeech.shutdown();
        }
    }
}
ここまででスポーツウォッチなどが可能。
さらに応用アプリを考えると、発話完了のタイミングが必要。
全ソースGithub
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import java.util.Locale;
import android.widget.Toast;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity{
    private TextToSpeech textToSpeech;

    class Text2SpeechListener implements TextToSpeech.OnInitListener{
        @Override
        public void onInit( int status ){
            Context context = getApplicationContext();
            if( TextToSpeech.SUCCESS==status ){
                Locale locale = Locale.getDefault();
                if( textToSpeech.isLanguageAvailable( locale ) >= TextToSpeech.LANG_AVAILABLE ){
                    textToSpeech.setLanguage( locale );
                }else{
                    Toast.makeText( context, "need speech data " + locale.getDisplayName(), Toast.LENGTH_SHORT ).show();
                }
            }else{
                Toast.makeText( context, "need TextToSpeech Engine", Toast.LENGTH_SHORT ).show();
            }
            Toast.makeText( context, "Ready TextToSpeech Engine", Toast.LENGTH_SHORT ).show();
            //★音声合成の準備ができてから発話完了リスナーをセット
            textToSpeech.setOnUtteranceCompletedListener( new Text2SpeechOnUtteranceCompletedListener() );
        }
    }

    class Text2SpeechOnUtteranceCompletedListener implements TextToSpeech.OnUtteranceCompletedListener{
        @Override
        public void onUtteranceCompleted( final String utteranceId ){
            runOnUiThread( new Runnable(){
                @Override
                public void run(){
                    Toast.makeText( getApplicationContext(), "finish speech " + utteranceId , Toast.LENGTH_SHORT ).show();
                }
            });
        }
    }

    public void onClickSpeek( View v ){
        if( textToSpeech.isSpeaking() ){
            textToSpeech.stop();
        }
        HashMap hashMap = new HashMap();
        hashMap.put( TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "KEY_PARAM_UTTERANCE_ID" );
        textToSpeech.speak( "pen pineapple apple pen", TextToSpeech.QUEUE_FLUSH, hashMap );//★第3引数
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ){
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar );
        setSupportActionBar( toolbar );

        textToSpeech = new TextToSpeech( this, new Text2SpeechListener() );
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if( null != textToSpeech ){
            textToSpeech.shutdown();
        }
    }
}
これでニュース読み上げ君などが可能。

トラックバック

このエントリーのトラックバックURL:
https://www.remix.asia/cgi/mt/mt-tb.cgi/7659

コメントを投稿

(いままで、ここでコメントしたことがないときは、コメントを表示する前にこのブログのオーナーの承認が必要になることがあります。承認されるまではコメントは表示されません。そのときはしばらく待ってください。)