2012-03-23 234 views
0

我试图通过创建一个android应用程序来录制声音。 这里是代码:使用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卡。它被删除。

我该如何解决这个问题?

回答

3

您还需要模拟SD卡。转到AVD - >编辑 - > SD卡插槽中,给任何数字来模拟SD卡的内存量。完成

+0

问题被解决..但现在我得到新的异常: java.io.FileNotFoundException/Mysounds1(权限被拒绝) – Developer 2012-03-23 17:00:32

+1

这意味着该文件不存在于您的模拟SD卡。把你的文件推入SD卡,它应该可以工作。同时检查DDMS的FIle EXplorer中的文件。 – Akhil 2012-03-23 17:24:03