将Android TextToSpeech发送到一个立体声频道

时间:2014-11-14 23:01:11

标签: android asynchronous io callback text-to-speech

在Android上,我想通过一个声道播放TextToSpeech输出(想想肩膀天使)。为此,我目前正在使用tts.synthesizeToFile(),然后使用MediaPlayer播放动态创建的文件。我使用mediaPlayer.setVolume(0.0f, 1.0f)只通过一个频道播放音频。

我的工作代码如下。

我的问题是:是否有更直接的方式通过单一频道播放TTS输出?

使用TextToSpeech合成文件非常耗时,并且使用MediaPlayer播放它会使用比严格必要的资源更多的资源。我希望这能够响应并在低端设备上工作,因此对CPU很友好很重要。


MainActivity.java

package com.example.pantts;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.speech.tts.TextToSpeech;
import android.os.Bundle;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Locale;


public class MainActivity extends Activity implements TextToSpeech.OnInitListener {

    private TextToSpeech tts;
    private String toSpeak = "Hello, right ear!";
    private static final String FILE_ID = "file";
    private HashMap<String, String> hashMap = new HashMap<String, String>();

    private String filename;
    private TextToSpeech tts;
    private MediaPlayer mediaPlayer;


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

        filename = getFilesDir() + "/" + "tts.wav";
        Log.d("LOG", "file: " + filename);
        // /data/data/com.example.pantts/files/tts.wav

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        tts = new TextToSpeech(this, this);
        tts.setOnUtteranceProgressListener(mProgressListener);
    }

    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            tts.setLanguage(Locale.UK);
            hashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, FILE_ID);

            // Using deprecated call for API 20 and earlier
            tts.synthesizeToFile(toSpeak, hashMap, filename);
            Log.d("LOG", "synthesizeToFile queued");
        }
    }

    private UtteranceProgressListener mProgressListener = 
            new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {
            Log.d("LOG", "synthesizeToFile onStart " + utteranceId);
        }

        @Override
        public void onError(String utteranceId) {
            Log.d("LOG", "synthesizeToFile onError " + utteranceId);
        }

        @Override
        public void onDone(String utteranceId) {
            if (utteranceId.equals(FILE_ID)) { // Thanks to Hoan Nguyen for correcting this
                Log.d("LOG", "synthesizeToFile onDone " + utteranceId);

                try {
                    File ttsFile = new File(filename);
                    FileInputStream inputStream = new FileInputStream(ttsFile);
                    FileDescriptor fileDescriptor = inputStream.getFD();
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(fileDescriptor);
                    inputStream.close();

                    mediaPlayer.prepare();
                    mediaPlayer.setVolume(0.0f, 1.0f); // right channel only
                    mediaPlayer.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };
}

1 个答案:

答案 0 :(得分:1)

合成没有任何问题,这是错误的比较。它应该是

if (utteranceId.equals(FILE_ID))