Android mMediaRecorder.stop() 报错, 你蛋疼了吗?

本文介绍了一个使用MediaRecorder进行录音时遇到的问题及解决方案。通过采用单例模式,有效地避免了因重复调用导致的IllegalStateException异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MediaRecorder stop 方法报错,抛出 IllegalStateException,

使用单例完美解决, 是多么痛的领悟啊。 蛋疼了一整天了 ~~~!!!!


import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;

public class Recorder {
	MediaRecorder mMediaRecorder;
	MediaPlayer mediaPlayer;
	Context context;
	File mRecAudioFile;
	File mRecAudioPath;
	String strTempFile = "recaudio_";
	private static MediaRecorder m=null;
	public Recorder(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
		mediaPlayer = new MediaPlayer();
	}
	
	private MediaRecorder getInstance(){
		if (m==null) {
			m=new MediaRecorder();
		}
		return m;
	}

	public void startRecord() {
		try {
			if (!initRecAudioPath()) {
				return;
			}

			if (mMediaRecorder != null) {
				return;
			}

			mMediaRecorder = getInstance();
			mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// …Ë÷√¬ÛøÀ∑Á
			mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
			mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
			try {
				mRecAudioFile = File.createTempFile(strTempFile, ".amr",
						mRecAudioPath);

			} catch (Exception e) {
				e.printStackTrace();
			}
			mMediaRecorder.setOutputFile(mRecAudioFile.getAbsolutePath());
			mMediaRecorder.prepare();
			mMediaRecorder.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void stopRecord() {
		if (mRecAudioFile != null && mMediaRecorder != null) {

			try {
				mMediaRecorder.setOnErrorListener(null);
				mMediaRecorder.setPreviewDisplay(null);
				mMediaRecorder.stop();
				mMediaRecorder.release();
				mMediaRecorder = null;
			} catch (Exception e) {
				// TODO: handle exception
			}

		}
	}

	public void playMusic() {
		/*
		 * Intent intent = new Intent();
		 * intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		 * intent.setAction(android.content.Intent.ACTION_VIEW);
		 * intent.setDataAndType(Uri.fromFile(mRecAudioFile), "audio");
		 * context.startActivity(intent);
		 */

		if (mMediaRecorder != null) {
			return;
		}

		try {

			mediaPlayer.reset();
			mediaPlayer.setDataSource(mRecAudioFile.getAbsolutePath());
			mediaPlayer.prepare();
			mediaPlayer.start();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	private boolean initRecAudioPath() {
		if (sdcardIsValid()) {
			String path = Environment.getExternalStorageDirectory().toString()
					+ File.separator + "record";// µ√µΩSDø®µ√¬∑æ∂
			mRecAudioPath = new File(path);
			if (!mRecAudioPath.exists()) {
				mRecAudioPath.mkdirs();
			}
		} else {
			mRecAudioPath = null;
		}
		return mRecAudioPath != null;
	}

	private boolean sdcardIsValid() {
		if (Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
		}
		return false;
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值