gpt4 book ai didi

java - 通过用户输入保存音频文件android

转载 作者:行者123 更新时间:2023-12-02 03:43:01 25 4
gpt4 key购买 nike

我试图通过用户输入保存音频文件名,但是当我使用名称(test1)保存文件号1时,没有保存任何内容,当我使用名称(test2)再次尝试时,第二个文件使用第一个名称保存( test1),第三个是第二个名字(test2)

 private Button play, stop, record;
private MediaRecorder myAudioRecorder;
private String outputFile = "";
final String format = ".3gp";
private String inputText = "";
//make dir
boolean exists = (new File("/storage/emulated/0/PonezRecorder/")).exists();
if (!exists){new File("/storage/emulated/0/PonezRecorder").mkdirs();}

//user input name
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
outputFile = input.getText().toString();
// Record button click listener
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile("/storage/emulated/0/PonezRecorder/"+ outputFile + format );
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException ise) {
// make something ...
} catch (IOException ioe) {
// make something
}
// stop click listener
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;

最佳答案

请尝试这个;

String audioName;    
public void recordAndSaveAudioFile(String customizedName) {
saved_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Voice Recorder";
File destinationDirectory = new File(saved_path);
if (!destinationDirectory.exists()) {
destinationDirectory.mkdirs();
}
this.audioName = customizedName;;
destinationFile = new File(destinationDirectory, audioName);

try {
FileOutputStream outputStream = new FileOutputStream(destinationFile);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
setupMediaRecorder(destinationFile);
try {
mediaRecorder.prepare();
mediaRecorder.start();

} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, "Recording...", Toast.LENGTH_SHORT).show();
}
private void setupMediaRecorder(File destination) {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(Uri.parse(String.valueOf(destination)).toString());
}

关于java - 通过用户输入保存音频文件android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56823930/

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