如何在外部存储中将文本到语音文件保存为.wav/.mp3格式

Aer*_*row 5 android text-to-speech

我正在做一个示例文本到语音应用程序,我的要求是使用EditText从用户获取文本并将输入文本保存为.wav/.mp3格式并存储在外部存储中.我在这个过程中使用了以下代码,但我不会成功.

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save in Sdcard" />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play the text file" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

TTS_AudioActivity.java

public class TTS_AudioActivity extends Activity {
    /** Called when the activity is first created. */
    Button store, play;
    EditText input;
    String speakTextTxt;
    private TextToSpeech mTts;

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

        store = (Button) findViewById(R.id.button1);
        play = (Button) findViewById(R.id.button2);
        input = (EditText) findViewById(R.id.editText1);
        store.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                speakTextTxt = input.getText().toString();
                HashMap<String, String> myHashRender = new HashMap<String, String>();
                myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
                        speakTextTxt);

                String exStoragePath = Environment
                        .getExternalStorageDirectory().getAbsolutePath();
                File appTmpPath = new File(exStoragePath + "/sounds/");
                appTmpPath.mkdirs();
                String tempFilename = "tmpaudio.wav";
                String tempDestFile = appTmpPath.getAbsolutePath() + "/"
                        + tempFilename;

                mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile);

            }
        });

        play.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final MediaPlayer mMediaPlayer = new MediaPlayer();
                try {
                    mMediaPlayer.setDataSource("/sdcard/sounds/tmpaudio.wav");
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mMediaPlayer.start();
                mMediaPlayer
                        .setOnCompletionListener(new OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                mMediaPlayer.stop();
                            }
                        });

            }
        });

    }
}
Run Code Online (Sandbox Code Playgroud)

在此,当我单击Store按钮时,输入文本必须以.wav格式存储,为此我使用了代码片段表单开发人员指南.成功保存后,当我单击"播放"按钮时,必须播放保存文件.我怎样才能做到这一点.提前致谢.

Pel*_*ler 0

使用

MediaScannerConnection.scanFile((Context) (this), new String[] {destFileName}, null, null);
Run Code Online (Sandbox Code Playgroud)

将 USB 重新连接到您的设备。我的直到我显示 .wav 文件才显示。或者,使用 ASTRO File 来查看它们。