如何在 Android 上将 TTS 输出保存为音频文件?

发布于 2025-01-04 00:28:22 字数 118 浏览 0 评论 0原文

这是我在这里发表的第一篇文章。我是 Android 编程新手。我想创建一个应用程序,可以将文本到语音的输出保存到我​​的数据库的音频文件中。我听说过 synthesizeToFile() 但事实并非如此。

this is my first post here. I'm new in Android Programming. I want to create an app where I can save the output of the text to speech into an audio file to my database. I've heard about synthesizeToFile() but It's not it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清欢 2025-01-11 00:28:22

SynthesizeToFile() 应该创建一个 wav (您可以解码并发送到您的数据库或另存为文件或您正在使用它执行的任何操作),并且您可以使用 Nitesh 的代码播放它。

来自http://android-developers。 blogspot.fi/2009/09/introduction-to-text-to-speech-in.html

HashMap<String, String> myHashRender = new HashMap();
String wakeUpText = "Are you up yet?";
String destFileName = "/sdcard/myAppCache/wakeUp.wav";
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, wakeUpText);
mTts.synthesizeToFile(wakeUpText, myHashRender, destFileName);

一旦收到合成完成的通知,您就可以播放
输出文件就像任何其他音频资源一样
android.media.MediaPlayer。

synthesizeToFile() should create a wav (which you can decode and send to your db or save as a file or whatever you're doing with it), and you can play it back using Nitesh's code.

From http://android-developers.blogspot.fi/2009/09/introduction-to-text-to-speech-in.html:

HashMap<String, String> myHashRender = new HashMap();
String wakeUpText = "Are you up yet?";
String destFileName = "/sdcard/myAppCache/wakeUp.wav";
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, wakeUpText);
mTts.synthesizeToFile(wakeUpText, myHashRender, destFileName);

Once you are notified of the synthesis completion, you can play the
output file just like any other audio resource with
android.media.MediaPlayer.

骑趴 2025-01-11 00:28:22

使用此代码并从资产文件夹中获取 mp3 文件访问权限并尝试此代码。

mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this,R.raw.button);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {                   
    @Override
    public void onCompletion(MediaPlayer mp) {
        mMediaPlayer.stop();
    }
});

Use this code and get the mp3 file acess from the assets folder and try this code.

mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this,R.raw.button);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {                   
    @Override
    public void onCompletion(MediaPlayer mp) {
        mMediaPlayer.stop();
    }
});
任谁 2025-01-11 00:28:22

您应该保存在 tts 文件资源文件夹中。

You should be saved in the tts file assets folder.

棒棒糖 2025-01-11 00:28:22

mTTS =new TextToSpeech(this, new TextToSpeech.OnInitListener()

        private String mAudioFilename = "";
        private final String mUtteranceID = "totts";
        @Override
        public void onInit(int status) {
            bsave.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    saveToAudioFile(mEditText.getText().toString().trim());
                }
            });

            CreateFile();
        }



        private void CreateFile() {
                // Perform the dynamic permission request
                if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);

                // Create audio file location
                File sddir = new File(Environment.getExternalStorageDirectory() + "/My File/");
                sddir.mkdir();


                mAudioFilename = sddir.getAbsolutePath() + "/" + mUtteranceID + ".wav";


            }



        private void saveToAudioFile(String text) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mTTS.synthesizeToFile(text, null, new File(mAudioFilename), mUtteranceID);
                Toast.makeText(MainActivity.this, "Saved to " + mAudioFilename, Toast.LENGTH_LONG).show();
            } else {
                HashMap<String, String> hm = new HashMap();
                hm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,mUtteranceID);
                mTTS.synthesizeToFile(text, hm, mAudioFilename);
                Toast.makeText(MainActivity.this, "Saved to " + mAudioFilename, Toast.LENGTH_LONG).show();
            }



        }
    });

mTTS =new TextToSpeech(this, new TextToSpeech.OnInitListener()

        private String mAudioFilename = "";
        private final String mUtteranceID = "totts";
        @Override
        public void onInit(int status) {
            bsave.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    saveToAudioFile(mEditText.getText().toString().trim());
                }
            });

            CreateFile();
        }



        private void CreateFile() {
                // Perform the dynamic permission request
                if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);

                // Create audio file location
                File sddir = new File(Environment.getExternalStorageDirectory() + "/My File/");
                sddir.mkdir();


                mAudioFilename = sddir.getAbsolutePath() + "/" + mUtteranceID + ".wav";


            }



        private void saveToAudioFile(String text) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mTTS.synthesizeToFile(text, null, new File(mAudioFilename), mUtteranceID);
                Toast.makeText(MainActivity.this, "Saved to " + mAudioFilename, Toast.LENGTH_LONG).show();
            } else {
                HashMap<String, String> hm = new HashMap();
                hm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,mUtteranceID);
                mTTS.synthesizeToFile(text, hm, mAudioFilename);
                Toast.makeText(MainActivity.this, "Saved to " + mAudioFilename, Toast.LENGTH_LONG).show();
            }



        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文