如何将音频文件作为输入谷歌语音到文本API?

时间:2014-09-23 11:39:33

标签: android audio speech-recognition speech-to-text

目前我制作了一个语音文字'应用程序使用谷歌语音文本api。

它将我在麦克风附近说的任何内容作为输入,并通过将其发送到谷歌并从中获取结果来显示它的文本形式。

嗯,我想把一个音频文件作为输入(我之前使用'录音机'应用程序记录)给我的#34;语音文本"而不是说话。应用程序。

此音频文件存在于手机的SD卡中。

Google语音文字api应选择此音频文件作为输入,然后将其文本格式作为输出。

我已经粘贴了我的" 100%工作" xml的代码和引用的java文件。

我的xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_above="@+id/textView1"
    android:layout_toLeftOf="@+id/textView1"
    android:background="@drawable/superim"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:text="@string/tap_on_the_button_to_speak_"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff" />

    <ImageButton
        android:id="@+id/btnSpeak"
        android:layout_width="253dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:contentDescription="@string/speak"
        android:src="@android:drawable/ic_btn_speak_now" />

    <TextView
        android:id="@+id/txtText"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="0.38"
        android:textAppearance="?android:attr/textAppearanceLarge"
         android:textColor="#ffffff"
          />

</LinearLayout>

我的java文件:

package com.prann.speechtotextdemo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
//import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    protected static final int RESULT_SPEECH = 1;

    private ImageButton btnSpeak;
    private TextView txtText;

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

        txtText = (TextView) findViewById(R.id.txtText);

        btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(
                        RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

                try {
                    startActivityForResult(intent, RESULT_SPEECH);
                    txtText.setText("");
                } catch (ActivityNotFoundException a) {
                    Toast t = Toast.makeText(getApplicationContext(),
                            "YOUR DEVICE DOESNT SUPPORT SPEECH TO TEXT \n install google vocie search ",
                            Toast.LENGTH_SHORT);
                    t.show();
                }
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case RESULT_SPEECH: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> text = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                txtText.setText(text.get(0));
            }
            break;
        }

        }
    }
}

有可能做我想做的事吗?如果是这样,怎么办??????

非常感谢详细解释。

提前谢谢你。 :-)

0 个答案:

没有答案
相关问题