我正在制作一个Android应用来录制音频。我想要录制的音频的视觉表示,例如线性可视化器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我正在制作一个Android应用来录制音频。我想要录制的音频的视觉表示,例如线性可视化器相关的知识,希望对你有一定的参考价值。
我有一个android应用来录制音频。我想要像线可视化器那样以可视方式表示录制的音频。我尝试使用它,但失败了https://github.com/gauravk95/audio-visualizer-android
我是一个初学者,因此,感谢您的逐步帮助,谢谢。
这是我的主要活动的样子:
public class MainActivity extends AppCompatActivity {
private Button buttonStart, buttonStop, buttonPlayLastRecordAudio,
buttonStopPlayingRecording ;
private String AudiosavePathInDevice = null;
private MediaRecorder mediaRecorder ;
private Random random ;
private static final int RequestPermissionCode = 1;
private MediaPlayer mediaPlayer ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = findViewById(R.id.button);
buttonStop = findViewById(R.id.button2);
buttonPlayLastRecordAudio = findViewById(R.id.button3);
buttonStopPlayingRecording = findViewById(R.id.button4);
buttonStop.setEnabled(false);
buttonPlayLastRecordAudio.setEnabled(false);
buttonStopPlayingRecording.setEnabled(false);
random = new Random();
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(checkPermission()) {
AudioSavePathInDevice =
Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + CreateRandomAudioFileName() + "AudioRecording.3gp";
MediaRecorderReady();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
buttonStart.setEnabled(false);
buttonStop.setEnabled(true);
Toast.makeText(MainActivity.this, "Recording Started", LENGTH_LONG).show();
} else {
requestPermission();
}
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder.stop();
buttonStop.setEnabled(false);
buttonPlayLastRecordAudio.setEnabled(true);
buttonStart.setEnabled(true);
buttonStopPlayingRecording.setEnabled(false);
Toast.makeText(MainActivity.this, "Recording Completed", LENGTH_LONG).show();
}
});
buttonPlayLastRecordAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) throws IllegalArgumentException,
SecurityException, IllegalStateException {
buttonStop.setEnabled(false);
buttonStart.setEnabled(false);
buttonStopPlayingRecording.setEnabled(true);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(AudioSavePathInDevice);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
Toast.makeText(MainActivity.this, "Recording Playing", LENGTH_LONG).show();
}
});
buttonStopPlayingRecording.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonStop.setEnabled(false);
buttonStart.setEnabled(true);
buttonStopPlayingRecording.setEnabled(false);
buttonPlayLastRecordAudio.setEnabled(true);
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
MediaRecorderReady();
}
}
});
}
public void MediaRecorderReady(){
mediaRecorder=new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(AudioSavePathInDevice);
}
private String CreateRandomAudioFileName(){
StringBuilder stringBuilder = new StringBuilder(5);
int i = 0 ;
while(i < 5) {
String randomAudioFileName = "ABCDEFGHIJKLMNOP";
stringBuilder.append(randomAudioFileName.
charAt(random.nextInt(randomAudioFileName.length())));
i++ ;
}
return stringBuilder.toString();
}
private void requestPermission() {
ActivityCompat.requestPermissions(MainActivity.this, new
String[]{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO}, RequestPermissionCode);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == RequestPermissionCode) {
if (grantResults.length > 0) {
boolean StoragePermission = grantResults[0] ==
PackageManager.PERMISSION_GRANTED;
boolean RecordPermission = grantResults[1] ==
PackageManager.PERMISSION_GRANTED;
if (StoragePermission && RecordPermission) {
Toast.makeText(MainActivity.this, "Permission Granted",
LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Permission Denied", LENGTH_LONG).show();
}
}
}
}
public boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO);
return result == PackageManager.PERMISSION_GRANTED &&
result1 == PackageManager.PERMISSION_GRANTED;
}
}
答案
您只需要使用音频dsp库
检查此链接:https://developer.android.com/ndk/guides/audio
您也可以检查:https://github.com/james34602/JamesDSPManager
MWEngine音频引擎也可以执行此操作:“通过设备输入(例如麦克风)进行实时录制和处理”
https://github.com/igorski/MWEngine
经常考虑一下dsp,您会发现如何可视化音频频率
以上是关于我正在制作一个Android应用来录制音频。我想要录制的音频的视觉表示,例如线性可视化器的主要内容,如果未能解决你的问题,请参考以下文章