LoginSignup
1
0

More than 5 years have passed since last update.

Android studio 音声認識で得た言葉を効果音に変換

Posted at

前提・実現したいこと

音声認識を利用した言葉を効果音に変換して再生するアプリを考えています。
(例)「おはよう」というと効果音でこけこっこーと流れる。

音声認識→テキスト化→効果音ファイルロード→テキストから言葉の抜出→効果音再生

発生している問題・エラーメッセージ

端末での実行をすると、エラーは出ないが、音声認識をして終わってしまう。

該当のソースコード


package com.example.sushilab.test_f;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private SoundPool soundPool;    // 効果音を鳴らす本体(コンポ)
    private TextView textDisplay;

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

        textDisplay = (TextView) findViewById(R.id.text1_view);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
        } else {
            AudioAttributes attr = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_MEDIA)
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .build();
            soundPool = new SoundPool.Builder()
                    .setAudioAttributes(attr)
                    .setMaxStreams(5)
                    .build();
        }
    }

    public void onClick(View v) {
        //音声入力がサポートされているか?
        if (getPackageManager().queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0).size() == 0) {
            Log.d("tag", "Voice Input is not supported.");
            return;  //サポートされてないとき無視
        }

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        startActivityForResult(intent, 0);  //0:requestCode
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0 && resultCode == RESULT_OK) {
            if (data.hasExtra(RecognizerIntent.EXTRA_RESULTS)) {
                //ここで認識された文字列を取得
                List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                if (results.size() > 0) {
                    String str = "";
                    for (String s : results) {
                        str += s + "\n";
                    }
                    textDisplay.setText(str);
                    int raw = 0;
                    if (str.contains("こんばんは")){
                        raw = R.raw.one;
                    }
                    else if( str.contains("さようなら")){
                        raw = R.raw.two;
                    }
                    else{
                        return;
                    }

                    int mp3 = soundPool.load(this, raw, 1);
                    soundPool.play(mp3,1f , 1f, 0, 0, 1f);
                }
            }
        }
    }

}

試したこと

効果音を変えたりしました。音声認識と、ボタンを押して効果音再生は別々では問題なくできました。

補足情報(FW/ツールのバージョンなど)

Android studio 3.0.1
rawは作成済みです。中にone.mp3とtwo.mp3が入っています。
とにかく動かしたいです。出来たら、言葉の部分と効果音をもっと増やしていく予定です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0