我想在android 3.0中创建一个录音机应用程序。情况是,当我点击record
按钮时,它必须开始记录,当我点击stop
按钮时,它必须停止录制。
录制语音后,我想点击play
按钮时播放录制的声音。当我点击stop playing
按钮时,它必须停止播放。我曾尝试过录制语音的代码。但它以3gpp
格式存储文件。所以它不是在ma设备上播放。所以我试图将outputformat更改为'AMR_NB'。但是当我点击停止按钮时它会崩溃。任何人都可以提供代码,否则PLZ帮助我通过编辑我的代码使其工作。
这是我的助手班......
public class AudioRecorder {
final MediaRecorder recorder = new MediaRecorder();
final String path;
/**
* Creates a new audio recording at the given path (relative to root of SD card).
*/
public AudioRecorder(String path) {
this.path = sanitizePath(path);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
/**
* Starts a new recording.
*/
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
}
这是我的活动课
public class audiorecording extends Activity {
private Button start;
private Button stop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
final AudioRecorder recorder = new AudioRecorder("/audiometer/temp");
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
recorder.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}) ;
//….wait a while
stop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
recorder.stop();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}) ;
}
}
答案 0 :(得分:2)
您可以使用setOutputFormat (int output_format)
设置录制期间生成的输出文件的格式。在setAudioSource()/ setVideoSource()之后但在prepare()之前调用它。 建议在使用H.263视频编码器和AMR音频编码器时始终使用3GP格式。使用MPEG-4容器格式可能会使一些桌面播放器感到困惑。
<小时/> Audalyzer适用于Android的音频分析器
这是一款适用于Android的简单音频分析器。它将来自麦克风的声音读数显示为波形显示,频谱和dB仪表。 dB级别相对于设备的最大输入级别。
使用MediaRecorder录制音频的常见情况如下:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
recorder.start(); // Recording is now started
...
recorder.stop();
recorder.reset(); // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused
答案 1 :(得分:2)
放android.permission.RECORD_AUDIO
android.permission.WRITE_EXTERNAL_STORAGE
清单中的?...