您好我尝试使用AudioRecord类通过MIC录制音频并将其存储在2048大小的缓冲区中。
我尝试使用AudioTrack类从该缓冲区本身播放录制的音频,创建另一个相同大小的缓冲区。当我运行应用程序时,它显示我的main.xml文件,它显示“遗憾的应用已停止”。它没有记录任何东西。我只是想从录音缓冲区本身播放音频。谁能帮我吗?这是我的代码。
public class Main extends Activity {
class AudRecord
{
public AudioRecord recorder;
public boolean isRecording;
public int SAMPLERATE;
public int CHANNELS;
public int AUDIO_FORMAT;
public int buffer;
public Thread recordingThread;
public AudRecord()
{
recorder = null;
isRecording = false;
SAMPLERATE = 44100;
CHANNELS = AudioFormat.CHANNEL_IN_MONO;
AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
recordingThread = null;
buffer = 2048;
}
public void StartRecording() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLERATE,
CHANNELS, AUDIO_FORMAT, buffer);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable()
{
public void run() {
writeAudioData();
}
});
recordingThread.start();
}
void writeAudioData() {
byte data[] = new byte[2048];
while (isRecording) {
recorder.read(data, 0, buffer);
send(data);
}
}
public void send(byte[] data) {
int buffers = 2048;
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffers,
AudioTrack.MODE_STREAM);
at.play();
at.write(data, 0, buffers);
at.stop();
at.release();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AudRecord rec= new AudRecord();
rec.StartRecording();
}
}
答案 0 :(得分:0)
遗憾的是,应用已停止这意味着您的XML文件存在问题。
AudioRecording
使用MediaRecorder
public class MainActivity extends Activity {
MediaRecorder recorder = new MediaRecorder();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.btnStart)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
start();
}
});
((Button) findViewById(R.id.btnStop)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stop();
}
});
}
public void start()
{
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"yourfilename.3gpp");
recorder.prepare();
recorder.start();
}
public void stop()
{
recorder.stop();
recorder.reset();
recorder.release();
}
}