1
我是java/Android的新手,一直在使用简单的录音应用程序时遇到问题。 我一直在关注本教程here,虽然我认为自己在最后,但我的应用程序无法正常工作。这里是我的.java代码:Android录音应用程序,录音不开始
public class MainActivity extends Activity {
private MediaRecorder audioRecorder;
private String outFile = null;
private Button record, stop, play;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
record = (Button)findViewById(R.id.recordButton);
stop = (Button)findViewById(R.id.stopButton);
play = (Button)findViewById(R.id.playButton);
stop.setEnabled(false);
play.setEnabled(false);
outFile = Environment.getExternalStorageDirectory().getAbsolutePath()+"/myRec.3gp";
audioRecorder = new MediaRecorder();
audioRecorder.reset();
audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
audioRecorder.setOutputFile(outFile);
}
public void record(View view){
try {
audioRecorder.prepare();
audioRecorder.start();
}
catch(IllegalStateException ise){
ise.printStackTrace();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
record.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording...", Toast.LENGTH_SHORT).show();
}
public void stop(View view){
audioRecorder.stop();
audioRecorder.release();
audioRecorder = null;
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recorded successfully.", Toast.LENGTH_SHORT).show();
}
public void play(View view) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(outFile);
mp.prepare();
mp.start();
Toast.makeText(getApplicationContext(), "Playing...", Toast.LENGTH_SHORT).show();
}
}
我已经作出了一定要包括正确使用的权限在外部存储和录制音频我的XML文件。
当我运行应用程序,我没有得到任何警告,但我按下记录按钮,什么都没有发生。任何人都可以帮我看到我忽视的任何明显的东西吗?非常感谢!
我正在使用win7与Android Studio和测试摩托罗拉Moto G W/Android 4.4。
具有u加入<用途的许可安卓:名称= “android.permission.WRITE_EXTERNAL_STORAGE”/> <使用许可权的android:NAME = “android.permission.RECORD_AUDIO”/> – 2014-11-08 07:20:25
你设置录音键调用你的'记录'方法? – immibis 2014-11-08 07:25:43
@NaveenTamrakar是的,我抓住了那一个。 – jdf 2014-11-08 07:33:35