Android mMediaRecorder.stop() 报错, 你蛋疼了吗?
2015-11-27 17:59
495 查看
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; } }
相关文章推荐
- MediaPlayer 在线播放器代码
- jquery中animate的stop()方法作用实例分析
- 深入Android MediaPlayer的使用方法详解
- Android提高之MediaPlayer播放网络视频的实现方法
- Android开发之MediaPlayer多媒体(音频,视频)播放工具类
- Android提高之MediaPlayer播放网络音频的实现方法
- Android提高之MediaPlayer音视频播放
- 网络播放 MediaPlayer: Prepare failed status 0x1
- 问题解决了
- jQuery stop()浅析
- MediaPlayer
- 相对完美的后台Service实现播放音乐功能
- 使用Vitamio播放rtsp流媒体并实现动态截图
- android-----简单的音乐播放器
- Android的VideoView如何循环播放
- Android_视频播放的简单使用方法
- android语音聊天
- mediaplayer stop called in state 1
- android app小白试水4-添加mediaplayer
- IOS获取本地音乐