繁体   English   中英

使用Android模拟器录制声音

[英]Recording sound using android emulator

我正在尝试通过创建android应用程序来录制声音。 这是代码:

这是助手类

package com.recorder;

import java.io.File;
import java.io.IOException;
import android.media.MediaRecorder;
import android.os.Environment;

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.getDataDirectory().getAbsolutePath() + path;
  }

  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.THREE_GPP);
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();
}
 }

这是我的活动:

package com.recorder;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class RecorderActivity extends Activity {
    MediaPlayer mp=new MediaPlayer();
    ImageButton mic;
    private AudioRecorder recorder;

    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.main);

     recorder = new AudioRecorder("/Mysounds1");//here is the path
    //to sdcard where we will save the recording
    }
    // Action listener for a button which record sound   
         public void mic(View view){
        Toast.makeText(this,"Recording started...",Toast.LENGTH_LONG).show();
        try {
        recorder.start();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }

当我运行代码时,出现以下异常:

java.io.IOException:未安装SD卡。 它已被删除。

我怎么解决这个问题?

您还需要模拟sdcard。 在SD卡插槽中转到AVD->编辑->,输入任意数字以将该内存量模拟为sdcard。 完成

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM