android 终止方法,如何在android中停止录音?

if(null != recorder){

recorder.stop();

recorder.reset();

recorder.release();

recorder = null;

}

为什么会强制关闭应用程序?..

提前感谢!

这是我VoiceRecording2.java。它有没有按钮,启动按钮和停止按钮,并选择格式..

package com.example.voicexml;

import java.io.File;

import java.io.IOException;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.media.MediaRecorder;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.widget.Button;

public class VoiceRecording2 extends Activity {

private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";

private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";

private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";

private MediaRecorder recorder = null;

private int currentFormat = 0;

private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP };

private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP };

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.voice);

setButtonHandlers();

enableButtons(false);

setFormatButtonCaption();

}

private void setButtonHandlers() {

((Button)findViewById(R.id.btnStart)).setOnClickListener(btnClick);

((Button)findViewById(R.id.btnStop)).setOnClickListener(btnClick);

((Button)findViewById(R.id.btnFormat)).setOnClickListener(btnClick);

}

private void enableButton(int id,boolean isEnable){

((Button)findViewById(id)).setEnabled(isEnable);

}

private void enableButtons(boolean isRecording) {

enableButton(R.id.btnStart,!isRecording);

enableButton(R.id.btnFormat,!isRecording);

enableButton(R.id.btnStop,isRecording);

}

private void setFormatButtonCaption(){

((Button)findViewById(R.id.btnFormat)).setText(getString(R.string.audio_format) + " (" + file_exts[currentFormat] + ")");

}

private String getFilename(){

String filepath = Environment.getExternalStorageDirectory().getPath();

File file = new File(filepath,AUDIO_RECORDER_FOLDER);

if(!file.exists()){

file.mkdirs();

}

return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);

}

private void startRecording(){

recorder = new MediaRecorder();

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

recorder.setOutputFormat(output_formats[currentFormat]);

recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

recorder.setOutputFile(getFilename());

recorder.setOnErrorListener(errorListener);

recorder.setOnInfoListener(infoListener);

try {

recorder.prepare();

recorder.start();

} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

private void stopRecording(){

}

private void displayFormatDialog(){

AlertDialog.Builder builder = new AlertDialog.Builder(this);

String formats[] = {"MPEG 4", "3GPP"};

builder.setTitle(getString(R.string.choose_format_title))

.setSingleChoiceItems(formats, currentFormat, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

currentFormat = which;

setFormatButtonCaption();

dialog.dismiss();

}

})

.show();

}

private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {

public void onError(MediaRecorder mr, int what, int extra) {

AppLog.logString("Error: " + what + ", " + extra);

}

};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {

public void onInfo(MediaRecorder mr, int what, int extra) {

AppLog.logString("Warning: " + what + ", " + extra);

}

};

private View.OnClickListener btnClick = new View.OnClickListener() {

public void onClick(View v) {

switch(v.getId()){

case R.id.btnStart:{

AppLog.logString("Start Recording");

enableButtons(true);

startRecording();

break;

}

case R.id.btnStop:{

if(null != recorder){

//recorder.stop();

//recorder.reset();

//recorder.release();

System.out.println("Churva");

//recorder = null;

}

AppLog.logString("Start Recording");

enableButtons(false);

//stopRecording();

break;

}

case R.id.btnFormat:{

displayFormatDialog();

break;

}

}

}

};

}

它简单porgram,在你的Android录制语音设备..

+3

请张贴logcat的输出 –

+0

请分享一些更多的代码和类的实例是这个记录吗? –

+0

我发布了整个VoiceRecording2.java,它也有AppLog.java,并且布局.. –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值