Android:录制音频应用程序崩溃

时间:2016-09-04 08:50:39

标签: java android audio

每个人,

我正在尝试进行简单的录音。

单击录制按钮时,音频会完美录制。

我尝试录制另一个音频并播放。当我尝试播放第二个音频时,它会崩溃。

你可以帮我修复这个应用程序,这样我就可以多次录制音频而不会破坏应用程序。

这是代码。

  stop.setEnabled(false);
  play.setEnabled(false);
  outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";;

  myAudioRecorder=new MediaRecorder();
  myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
  myAudioRecorder.setOutputFile(outputFile);

  record.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        try {
           myAudioRecorder.prepare();
           myAudioRecorder.start();
        }


        catch (IllegalStateException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }

        catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }

        record.setEnabled(false);
        stop.setEnabled(true);

        Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
     }
  });

  stop.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        myAudioRecorder.stop();
        myAudioRecorder.reset();

        record.setEnabled(true);
        stop.setEnabled(false);
        play.setEnabled(true);

        Toast.makeText(getApplicationContext(), "Audio recorded successfully",Toast.LENGTH_LONG).show();
     }
  });

 play.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) throws IllegalArgumentException,SecurityException,IllegalStateException {
        MediaPlayer m = new MediaPlayer();

        try {
           m.setDataSource(outputFile);
        }

        catch (IOException e) {
           e.printStackTrace();
        }

        try {
           m.prepare();
        }

        catch (IOException e) {
           e.printStackTrace();
        }

        m.start();
        Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
     }
  });

谢谢!

2 个答案:

答案 0 :(得分:1)

单击停止按钮时,您似乎无法启用录制按钮。写

record.setEnabled(true);

在停止按钮的onClick()方法实现中。

另一种情况是,当点击停止按钮时,您正在设置myAudioRecorder = null。如果单击此记录按钮后,您将在此语句中获得NullPointerException

myAudioRecorder.prepare() //exception

解决方法是在stop的onClick()实现中删除将myAudioRecorder设置为null的语句。

另一个问题是因为这句话myAudioRecorder.release()。释放资源后,您无法取回上一个MediaRecorder实例。每次录制时重新初始化myAudioRecorder,或者在单击停止后不释放资源。要查看MediaRecorder生命周期see this.

以下是您可以查看和理解的参考代码。请在需要时捕获例外情况。

//package name
//imports
public class RecordPlayActivity extends AppCompatActivity implements View.OnClickListener {
// declare buttons here
private MediaRecorder myAudioRecorder;
private String recordOutputFile;
private MediaPlayer mediaPlayer;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(null);
    setContentView(R.layout.layout_name);
    //find buttons view by Id here
    record.setOnClickListener(this);
    stop.setOnClickListener(this);
    play.setOnClickListener(this);
    recordOutputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
    myAudioRecorder = new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(recordOutputFile);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(recordOutputFile);
    //Initial condition
    //stop.setEnabled(false);   //not required as stop can be made always enabled
    play.setEnabled(false);
}

@Override
public void onClick(View view){
    switch(view.getId()){
        case R.id.idForRecord:
            myAudioRecorder.prepare();
            myAudioRecorder.start();
            //Recording started
            record.setEnabled(false);
            // don't make play enabled cause you dont want to play
            // and record at same time without stopping record.
            play.setEnabled(false); //required because play can get enabled from stop but it should not remain when recording
            break;
        case R.id.idForStop:
            //if clicked after record
            myAudioRecorder.stop();
            myAudioRecorder.reset();
            //if clicked after play
            if(mediaPlayer.isLooping()) {
                mediaPlayer.stop();
            }
            //recording stopped and saved;
            record.setEnabled(true);
            play.setEnabled(true);
            break;
        case R.id.idForPlay:
            mediaPlayer.prepare();
            mediaPlayer.start();
            //playing
            record.setEnabled(false); // you dont wanna play and record at same time
            break;
        default:
    }
}

}`

希望这些都解决了你的问题。

答案 1 :(得分:0)

它崩溃是因为outputFile = Environment.getExternalStorageDirectory()。getAbsolutePath()+“ /recording.3gp”;定向到Android OS的顶级目录...

如果使用此路径Environment.getExternalStorageDirectory()+ File.separator +“ sounds”“ / recording.3gp”,则您的录音将进入下一级目录“ sounds”进入目录声音。

如果您的录音位于顶层路径下方的文件夹中,则应用不会崩溃...