gpt4 book ai didi

android - 在android中录制期间的声波

转载 作者:行者123 更新时间:2023-11-29 14:03:02 25 4
gpt4 key购买 nike

如何在android中实现录音时的声波??

在下面的代码中

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultView = (TextView) findViewById(R.id.output);

try {
// the soundfile
File storageDir = new File(Environment
.getExternalStorageDirectory(), "com.hascode.recorders");
storageDir.mkdir();
Log.d(APP_TAG, "Storage directory set to " + storageDir);
outfile = File.createTempFile("hascode", ".3gp", storageDir);

// init recorder
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outfile.getAbsolutePath());

// init player
player.setDataSource(outfile.getAbsolutePath());
} catch (IOException e) {
Log.w(APP_TAG, "File not accessible ", e);
} catch (IllegalArgumentException e) {
Log.w(APP_TAG, "Illegal argument ", e);
} catch (IllegalStateException e) {
Log.w(APP_TAG, "Illegal state, call reset/restore", e);
}

btRecord = (Button) findViewById(R.id.btRecord);
btRecord.setOnClickListener(handleRecordClick);

btPlay = (Button) findViewById(R.id.btPlay);
btPlay.setOnClickListener(handlePlayClick);

}

private final OnClickListener handleRecordClick = new OnClickListener() {
@Override
public void onClick(View view) {
if (!recording) {
startRecord();
} else {
stopRecord();
}
}
};

private final OnClickListener handlePlayClick = new OnClickListener() {
@Override
public void onClick(View view) {
if (!playing) {
startPlay();
} else {
stopPlay();
}
}
};

private void startRecord() {
Log.d(APP_TAG, "start recording..");
printResult("start recording..");
try {
recorder.prepare();
recorder.start();
recording = true;
} catch (IllegalStateException e) {
Log
.w(APP_TAG,
"Invalid recorder state .. reset/release should have been called");
} catch (IOException e) {
Log.w(APP_TAG, "Could not write to sd card");
}
}

private void stopRecord() {
Log.d(APP_TAG, "stop recording..");
printResult("stop recording..");
recorder.stop();
recorder.reset();
recorder.release();
recording = false;
}

private void startPlay() {
Log.d(APP_TAG, "starting playback..");
printResult("start playing..");
try {
playing = true;
player.prepare();
player.start();
} catch (IllegalStateException e) {
Log.w(APP_TAG, "illegal state .. player should be reset");
} catch (IOException e) {
Log.w(APP_TAG, "Could not write to sd card");
}
}

private void stopPlay() {
Log.d(APP_TAG, "stopping playback..");
printResult("stop playing..");
player.stop();
player.reset();
player.release();
playing = false;
}

private void printResult(String result) {
resultView.setText(result);
}

最佳答案

您想将录制的声音与预先录制的海浪声合并吗?

如果是这样,也许您可​​以尝试创建两个不同来源的声音对象。然后你也许可以间隔写入输出文件。一段为预录一段,一段为新录。

我什至不知道这是否是一种有效的方法。正是我的一般方法会一直面临这个问题。

关于android - 在android中录制期间的声波,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8572625/

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