2011-05-12 56 views
4

我想在android 3.0中创建一个录音机应用程序。该场景是当我点击record按钮它必须开始记录,当我点击stop按钮它必须停止记录。android录音机应用程序

录制语音后,我想要播放录制的声音时,我点击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(); 
       } 

      } 
     }) ; 

    } 
} 

回答

2

您可以使用setOutputFormat (int output_format)

设置在录制过程中产生的输出文件的格式。在setAudioSource()/ setVideoSource()之后但在prepare()之前调用它。 使用H.263视频编码器和AMR音频编码器时,建议始终使用3GP格式。使用MPEG-4容器格式可能会混淆某些桌面播放器。


Audalyzer音频分析器为Android

这是一个简单的音频分析器,用于机器人。它将麦克风的声音读数显示为波形显示,频谱和dB计。 dB级别是相对于您的设备的最大输入级别。


Android MediaRecorder

使用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 
2

android.permission.RECORD_AUDIO

android.permission.WRITE_EXTERNAL_STORAGE 

清单中...