gpt4 book ai didi

android - 如何在外部存储中将 Text to Speech 文件保存为 .wav/.mp3 格式

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:15 26 4
gpt4 key购买 nike

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

ma​​in.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>

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();
}
});

}
});

}
}

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

最佳答案

您是否尝试使用函数:

 public int synthesizeToFile (CharSequence text, Bundle params, File file, String utteranceId) 

API DOCUMENTATION

代码应该是这样的:

    private void speakText(String text) {
String state = Environment.getExternalStorageState();
boolean mExternalStorageWriteable = false;
boolean mExternalStorageAvailable = false;
if (Environment.MEDIA_MOUNTED.equals(state)) {
// Can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;

} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Can't read or write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/download");
dir.mkdirs();
File file = new File(dir, "myData.mp3");
int test = m_TTS.synthesizeToFile((CharSequence) text, null, file,
"tts");
}

如果一切正常,变量 test 必须为 0。

请记住为您的 list 添加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于android - 如何在外部存储中将 Text to Speech 文件保存为 .wav/.mp3 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9941781/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com