gpt4 book ai didi

android - 如何从外部设备录制音频(麦克风)

转载 作者:行者123 更新时间:2023-12-03 01:52:05 32 4
gpt4 key购买 nike

我想使用媒体记录器从我的android应用程序记录音频。我已将音频源设置为麦克风,这使我可以从麦克风进行录制。但是我也很乐意检测是否连接了任何外部麦克风。如果是这样,那么它应该从中录制音频。
例如:如果将蓝牙扬声器/麦克风连接到电话,则它应从蓝牙麦克风捕获声音。
必须设置什么音频源?将其设置为默认帮助吗?

记录代码:

recorder = new MediaRecorder();
recorder.SetAudioSource(AudioSource.Mic);
recorder.SetOutputFormat(OutputFormat.Mpeg4);
recorder.SetAudioEncoder(AudioEncoder.Aac);
recorder.SetAudioSamplingRate(44100);
recorder.SetAudioEncodingBitRate(16 * 8000);
recorder.SetAudioChannels(2);
recorder.SetOutputFile(path);
recorder.Prepare();
recorder.Start();

最佳答案

Use  this Code to your manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>

java class`enter code here`
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class AudioOnTouchActivity extends Activity {
Button b1;
private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP };
private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
AppLog.logString("Start Recording");
startRecording();
break;
case MotionEvent.ACTION_UP:
AppLog.logString("stop Recording");
stopRecording();
break;
}
return false;
}
});
}

private String getFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);

if(!file.exists()){
file.mkdirs();
}

return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}

private void startRecording(){
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
AppLog.logString("Error: " + what + ", " + extra);
}
};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
AppLog.logString("Warning: " + what + ", " + extra);
}
};

private void stopRecording(){
if(null != recorder){
recorder.stop();
recorder.reset();
recorder.release();

recorder = null;
}
}
}

AppLog.java file is

import android.util.Log;

public class AppLog {
private static final String APP_TAG = "AudioRecorder";

public static int logString(String message){
return Log.i(APP_TAG,message);
}

the xml is

<?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"
>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>

</LinearLayout>

关于android - 如何从外部设备录制音频(麦克风),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38869568/

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