我一直试图在Android中录制和播放录制的音频。我已经使用了MediaRecorder和MediaPlayer类。
我的代码包含3个细分: 的startRecording() stoprecording() playaudio
但是我的代码片段中出现错误,每当我执行它时,startrecording()函数都会实现catch块。
package hare.krishna;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class Recordplay2Activity extends Activity
{
Button play1,record1,stop1;
TextView status1;
MediaRecorder m1=null;
MediaPlayer m2=null;
String path;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play1=(Button)findViewById(R.id.play);
record1=(Button)findViewById(R.id.record);
stop1=(Button)findViewById(R.id.stop);
status1=(TextView)findViewById(R.id.status);
OnClickListener recordListener = new OnClickListener()
{
public void onClick(View v)
{
startrecording();
}
};
OnClickListener stopListener = new OnClickListener()
{
public void onClick(View v)
{
stoprecording();
}
};
OnClickListener playListener = new OnClickListener()
{
public void onClick(View v)
{
playaudio();
}
};
record1.setOnClickListener(recordListener);
stop1.setOnClickListener(stopListener);
play1.setOnClickListener(playListener);
}
public void startrecording()
{
status1.setText("GET READY FOR RECORDING");
try
{
m1=new MediaRecorder();
m1.setAudioSource(MediaRecorder.AudioSource.MIC);
m1.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
m1.setOutputFile(path);
m1.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
m1.prepare();
m1.start();
}
catch(Exception e)
{
status1.setText("ERROR IN startrecording FUNCTION ");
}
}
public void stoprecording()
{
try
{
status1.setText("TIME TO STOP MEDIA");
m1.stop();
m1.reset();
m1.release();
}
catch(Exception e)
{
status1.setText("ERROR IN stoprecording FUNCTION");
}
}
public void playaudio()
{
status1.setText("PLAYING RECORDER AUDIO");
try
{
m2=new MediaPlayer();
m2.setDataSource("/sdcard/output.3gp");
m2.prepare();
m2.start();
}
catch(Exception e)
{
System.out.println("ERROR in playaudio FUNCTION");
}
}
}
答案 0 :(得分:1)
我解决了。我改变了AudioEncoder
:
m1.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
为:
m1.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT)