Android - 如何录制简单的声音片段?

Posted

技术标签:

【中文标题】Android - 如何录制简单的声音片段?【英文标题】:Android - How to record simple soundbite? 【发布时间】:2012-02-21 22:18:54 【问题描述】:

编辑:当我第二次单击录制按钮(停止录制)时,它会强制关闭应用程序。该事件的具体代码:

else if(isrec) 
                recorder.stop();
                recorder.reset();
                recorder.release();
                recorder = null;
                isrec = false;
                Toast.makeText( getApplicationContext(),"No longer recording!",Toast.LENGTH_SHORT).show();
            

(原始问题): 我在尝试让应用程序在单击按钮时录制声音时遇到问题。我已经包含了代码...这是我的 Toasts 告诉我的:

setAudiosource 之后 setOutputFile 之后 isrec 不正确 正在尝试... 捕获 IO 异常...

非常感谢任何帮助。

private OnClickListener micListener = new OnClickListener() 
    boolean isrec = false;
    public void onClick(View v) 
        MediaRecorder recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        Toast.makeText( getApplicationContext(),"after setAudioSource",Toast.LENGTH_SHORT).show();
        recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
        File outputFile = null;
        outputFile = getFileStreamPath("output.amr");
        recorder.setOutputFile(outputFile.getAbsolutePath());

        Toast.makeText( getApplicationContext(),"after setOutputFile",Toast.LENGTH_SHORT).show();
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        if(!isrec) 
            Toast.makeText( getApplicationContext(),"isrec is not true",Toast.LENGTH_SHORT).show();
            try 
                Toast.makeText( getApplicationContext(),"trying...",Toast.LENGTH_SHORT).show();
                recorder.prepare();
                recorder.start();
                isrec = true;
                Toast.makeText( getApplicationContext(),"Recording!",Toast.LENGTH_SHORT).show();
             catch (IllegalStateException e) 
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText( getApplicationContext(),"caught IllegalState Exception...",Toast.LENGTH_SHORT).show();
             catch (IOException e) 
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText( getApplicationContext(),"caught IO Exception...",Toast.LENGTH_SHORT).show();
            
         else if(isrec) 
            recorder.stop();
            recorder.reset();
            recorder.release();
            recorder = null;
            isrec = false;
            Toast.makeText( getApplicationContext(),"No longer recording!",Toast.LENGTH_SHORT).show();
        
    
;

【问题讨论】:

【参考方案1】:

首先,您应该为isrec 使用布尔,而不是字符串

那个:

 FileDescriptor fileName = null;
  recorder.setOutputFile(fileName); 

这意味着您要记录在一个为空的文件上

如果您创建一个文件,并将其作为记录器的参数传递,它应该可以工作。

编辑:有时当您想停止记录器时,释放不起作用。 所以,请保留stop()reset()

【讨论】:

谢谢!我创建了一个文件,它不再给出 IO 异常,这很好,但是现在当我第二次单击该按钮时,我会强制关闭。我已经更新了我原来帖子中的代码。 不要发布。我有同样的问题。尝试删除版本并保持停止并重置【参考方案2】:

为主要活动尝试以下代码:

public class AudioRecordTestActivity extends Activity

private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;

private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;

private PlayButton   mPlayButton = null;
private MediaPlayer   mPlayer = null;

private void onRecord(boolean start) 
    if (start) 
        startRecording();
     else 
        stopRecording();
    


private void onPlay(boolean start) 
    if (start) 
        startPlaying();
     else 
        stopPlaying();
    


private void startPlaying() 
    mPlayer = new MediaPlayer();
    try 
        mPlayer.setDataSource(mFileName);
        mPlayer.prepare();
        mPlayer.start();
     catch (IOException e) 
        Log.e(LOG_TAG, "prepare() failed");
    


private void stopPlaying() 
    mPlayer.release();
    mPlayer = null;


private void startRecording() 
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try 
        mRecorder.prepare();
     catch (IOException e) 
        Log.e(LOG_TAG, "prepare() failed");
    

    mRecorder.start();


private void stopRecording() 
    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;


class RecordButton extends Button 
    boolean mStartRecording = true;

    OnClickListener clicker = new OnClickListener() 
        public void onClick(View v) 
            onRecord(mStartRecording);
            if (mStartRecording) 
                setText("Stop recording");
             else 
                setText("Start recording");
            
            mStartRecording = !mStartRecording;
        
    ;

    public RecordButton(Context ctx) 
        super(ctx);
        setText("Start recording");
        setOnClickListener(clicker);
    


class PlayButton extends Button 
    boolean mStartPlaying = true;

    OnClickListener clicker = new OnClickListener() 
        public void onClick(View v) 
            onPlay(mStartPlaying);
            if (mStartPlaying) 
                setText("Stop playing");
             else 
                setText("Start playing");
            
            mStartPlaying = !mStartPlaying;
        
    ;

    public PlayButton(Context ctx) 
        super(ctx);
        setText("Start playing");
        setOnClickListener(clicker);
    


public AudioRecordTestActivity() 
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/audiorecordtest.3gp";


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

    LinearLayout ll = new LinearLayout(this);
    mRecordButton = new RecordButton(this);
    ll.addView(mRecordButton,
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0));
    mPlayButton = new PlayButton(this);
    ll.addView(mPlayButton,
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0));
    setContentView(ll);


@Override
public void onPause() 
    super.onPause();
    if (mRecorder != null) 
        mRecorder.release();
        mRecorder = null;
    

    if (mPlayer != null) 
        mPlayer.release();
        mPlayer = null;
    

并在 ma​​nifest 文件中指定以下权限

 <uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

【讨论】:

【参考方案3】:

也许this 会有所帮助?本文将向您展示如何解决两个问题:

    显示录制 WAV 文件的方法。 尽可能以最佳质量录制音频。

【讨论】:

以上是关于Android - 如何录制简单的声音片段?的主要内容,如果未能解决你的问题,请参考以下文章

在android中将录制的声音调制成不同的效果?

如何在 Android 中以更好的质量录制声音并降低噪音

Android 4.4中AudioRecord用例 - 录制系统内置声音

录制使用 javascript 播放的声音

在 Java 中使用声音计将声音从麦克风录制到文件

如何在 Android 上生成复杂的声音?