-1

我从这里得到 Mediarecorder 的示例源代码

但它不运行。调试时显示强制关闭。

谢谢

4

1 回答 1

1
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startBtn = (Button) findViewById(R.id.bgnBtn);
Button endBtn = (Button) findViewById(R.id.stpBtn);
Button playRecordingBtn = (Button) findViewById(R.id.playRecordingBtn);
Button stpPlayingRecordingBtn = (Button) findViewById(R.id.stpPlayingRecordingBtn);
startBtn.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View view) 
    {
        try 
        {
            beginRecording();
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
});

endBtn.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View view) 
    {
        try 
        {
            stopRecording();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
       }
   });

playRecordingBtn.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View view) 
    {
        try 
        {
            playRecording();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
});

stpPlayingRecordingBtn.setOnClickListener(new OnClickListener() 
{
@Override
    public void onClick(View view) 
    {
        try 
        {
            stopPlayingRecording();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        }
 });
   }

private void beginRecording() throws Exception 
{
    killMediaRecorder();
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    if (file == null) 
    {
        File rootDir = Environment.getExternalStorageDirectory();
        file = File.createTempFile(PREFIX, EXTENSION, rootDir);
    }
    recorder.setOutputFile(file.getAbsolutePath());
    recorder.prepare();
    recorder.start();
    isRecording = true;
}

private void stopRecording() throws Exception 
{
    if (recorder != null)
    {
        isRecording = false;
        recordAmplitude.cancel(true);
        recorder.stop();
    }
}

private void killMediaRecorder()
{
    if (recorder != null) 
    {
        recorder.release();
    }
}

private void killMediaPlayer() 
{
    if (mediaPlayer != null) 
    {
        try 
        {
            mediaPlayer.release();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}

private void playRecording() throws Exception 
{
    killMediaPlayer();
    mediaPlayer = new MediaPlayer();
    // from sdcard
    if (file.exists()) 
    {   
        mediaPlayer.setDataSource(file.getAbsolutePath());
    }
    mediaPlayer.prepare();
    mediaPlayer.start();
}

private void stopPlayingRecording() throws Exception 
{
    if(mediaPlayer!=null)
    {
        mediaPlayer.stop();
    }
}

@Override
protected void onDestroy() 
{
    super.onDestroy();
    killMediaRecorder();
    killMediaPlayer();
}
}
于 2012-06-19T07:34:49.497 回答