Media Recorder记录应用程序音频

时间:2013-10-06 06:22:15

标签: android audio record soundpool mediarecorder

我有一个应用程序,其中有几个按钮有声音(soundPool)我需要将它们写在闪存卡上我通过MediaRecorder但是当我运行应用程序然后打开录音播放并点击停止录制应用程序崩溃你不告诉我这是什么问题? 我期望在方法recordStop

public class MainActivity extends Activity {


    int kickSound;
    SoundPool mSoundPool;
    AssetManager assets;

  private MediaRecorder mediaRecorder;
  private MediaPlayer mediaPlayer;
  private String fileName;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
    assets = getAssets();

    kickSound = loadSound("snare_trap.ogg");

    ImageButton kick = (ImageButton)this.findViewById(R.id.imageButton1);
    kick.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                playSound(kickSound);
            }
            return false;
        }
    });


    fileName = Environment.getExternalStorageDirectory() + "/record.3gpp";
  }

  protected void playSound(int sound) {

        if (sound > 0)
          mSoundPool.play(sound, 1, 1, 1, 0, 1);
      }


      private int loadSound(String fileName) {
            AssetFileDescriptor afd = null;
            try {
              afd = assets.openFd(fileName);
            } catch (IOException e) {
              e.printStackTrace();
              Toast.makeText(this, "Couldn't load file '" + fileName + "'", Toast.LENGTH_SHORT).show();
              return -1;
            }
            return mSoundPool.load(afd, 1);
          }


  public void recordStart(View v) {
    try {
      releaseRecorder();

      File outFile = new File(fileName);
      if (outFile.exists()) {
        outFile.delete();
      }

      mediaRecorder = new MediaRecorder();
      mediaRecorder.setAudioSource(AudioManager.STREAM_MUSIC);
      mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      mediaRecorder.setOutputFile(fileName);
      mediaRecorder.prepare();
      mediaRecorder.start();
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  public void recordStop(View v) {
      mediaRecorder.stop();
    }


  public void playStart(View v) {
    try {
      releasePlayer();
      mediaPlayer = new MediaPlayer();
      mediaPlayer.setDataSource(fileName);
      mediaPlayer.prepare();
      mediaPlayer.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void playStop(View v) {
    if (mediaPlayer != null) {
      mediaPlayer.stop();
    }
  }

  private void releaseRecorder() {
    if (mediaRecorder != null) {
      mediaRecorder.release();
      mediaRecorder = null;
    }
  }

  private void releasePlayer() {

      mediaPlayer.release();
      mediaPlayer = null;
    }


  @Override
  protected void onDestroy() {
    super.onDestroy();
    releasePlayer();
    releaseRecorder();
  }

}

1 个答案:

答案 0 :(得分:0)

你为什么要开始录音?只有在您不想再访问录像机时才应使用释放。尝试将版本移至最后。