智能手機(jī)操作系統(tǒng)IOS與Android平分天下(PS:WP與其他的直接無(wú)視了),而Android的免費(fèi)招來(lái)了一大堆廠商分分向Android示好,故Android可能會(huì)有“較好”的前景。
Android實(shí)現(xiàn)簡(jiǎn)單的錄音、播音與視頻錄制功能。當(dāng)然了、無(wú)論是出場(chǎng)綁定的還是第三方都有很多很好的應(yīng)用。但是對(duì)于一名開(kāi)發(fā)人員來(lái)說(shuō)及時(shí)做的UI再丑、功能再爛那也是一種成就!
實(shí)現(xiàn)錄音、錄制視頻功能需要調(diào)用【MediaRecorder】類,播放音頻則需要【MediaPlayer】類。那簡(jiǎn)單以實(shí)現(xiàn)錄音、錄制視頻、播放音頻為例寫一個(gè)自己的SoundRecordingHelper類。
思路:Android已經(jīng)封裝好了這兩個(gè)類只需要進(jìn)行簡(jiǎn)單的設(shè)置就可以實(shí)現(xiàn)這三個(gè)功能了 1、需要在工程文件中添加需要的權(quán)限 2、設(shè)置布局文件 3、調(diào)用自己封裝的SoundRecordingHelper類。下面主要講SoundRecordingHelper類,實(shí)現(xiàn)功能直接調(diào)用此類中的Start與Stop方法即可。
package com.clown.tools;
import android.content.Context; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Environment; import android.view.SurfaceHolder; import android.view.SurfaceView;
/* * Android錄音輔助類 * 需要添加的權(quán)限: * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> * <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> * <uses-permission android:name="android.permission.RECORD_AUDIO" /> * 視頻錄制添加的權(quán)限: * <uses-permission android:name="android.permission.CAMERA" /> * <uses-permission android:name="android.permission.RECORD_AUDIO" /> * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> * 文件格式:Environment.getExternalStorageDirectory().getAbsolutePath() + "/TonFun.amr" */ public class SoundRecordingHelper implements SurfaceHolder.Callback { // 文件路徑 private String strFilePath = ""; // 數(shù)據(jù)上下文 private Context context = null; // 媒體播放對(duì)象 private MediaPlayer mPlayer = null; // 媒體錄音對(duì)象 private MediaRecorder mRecorder = null; // 用來(lái)顯示視頻的一個(gè)接口,我靠不用還不行,也就是說(shuō)用mediarecorder錄制視頻還得給個(gè)界面看 // 想偷偷錄視頻的同學(xué)可以考慮別的辦法。。嗯需要實(shí)現(xiàn)這個(gè)接口的Callback接口 private SurfaceHolder surfaceHolder;
public String getStrFilePath() { return strFilePath; }
public void setStrFilePath(String strFilePath) { this.strFilePath = strFilePath; }
/* * 音頻錄制的構(gòu)造函數(shù) */ public SoundRecordingHelper(Context context, String strFilePath) { this.context = context; this.strFilePath = strFilePath; }
/* * 視頻錄制的構(gòu)造函數(shù) */ @SuppressWarnings("deprecation") public SoundRecordingHelper(Context context, SurfaceView surfaceview, String strFilePath) { this.context = context; this.surfaceHolder = surfaceview.getHolder(); this.surfaceHolder.addCallback(this); // 加入回調(diào)接口 this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // 這個(gè)方法已經(jīng)過(guò)時(shí)了,但是為了兼容低版本必須設(shè)置 this.strFilePath = strFilePath; }
/* * 開(kāi)始錄音 */ public boolean startSoundRecording() { boolean bResult = false; mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 設(shè)置音頻來(lái)源(MIC表示麥克風(fēng)) mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // 設(shè)置音頻輸出格式 mRecorder.setOutputFile(strFilePath); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); // 設(shè)置音頻編碼 try { mRecorder.prepare(); mRecorder.start(); bResult = true; } catch (Exception ex) { System.out.println("Error:錄音失敗、" + ex.getMessage()); bResult = false; } return bResult; }
/* * 停止錄音 */ public boolean stopSoundRecording() { boolean bResult = false; if (mRecorder != null) { mRecorder.stop(); mRecorder.release(); mRecorder = null; bResult = true; } else { bResult = false; } return bResult; }
/* * 開(kāi)始播放音頻 */ public boolean startPlay() { boolean bResult = false; mPlayer = new MediaPlayer(); try { mPlayer.setDataSource(strFilePath); mPlayer.prepare(); mPlayer.start(); bResult = true; } catch (Exception ex) { System.out.println("Error:播放失敗、" + ex.getMessage()); bResult = false; } return bResult; }
/* * 停止播放音頻 */ public boolean stopPlay() { boolean bResult = false; if (mPlayer != null) { mPlayer.stop(); mPlayer.release(); mPlayer = null; bResult = true; } else { bResult = false; } return bResult; }
/* * 調(diào)用攝像頭進(jìn)行視頻錄制 nWidth:視頻的寬 * nHeight:視頻的高nRate:視頻幀數(shù)nType:視頻格式(MediaRecorder.OutputFormat.THREE_GPP) */ public boolean startVideoRecording(int nWidth, int nHeight, int nRate, int nType) { boolean bResult = false; mRecorder = new MediaRecorder(); mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // 設(shè)置視頻源為攝像頭 mRecorder.setOutputFormat(nType);// 設(shè)置錄制完成后視頻的封裝格式THREE_GPP為3gp.MPEG_4為mp4 mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); // 設(shè)置錄制的視頻編碼h263 // h264 mRecorder.setVideoSize(nWidth, nHeight);// 設(shè)置視頻錄制的分辨率。必須放在設(shè)置編碼和格式的后面,否則報(bào)錯(cuò) mRecorder.setVideoFrameRate(nRate);// 設(shè)置錄制的視頻幀率。必須放在設(shè)置編碼和格式的后面,否則報(bào)錯(cuò) mRecorder.setPreviewDisplay(surfaceHolder.getSurface()); // 設(shè)置顯示預(yù)覽 mRecorder.setOutputFile(strFilePath); // 設(shè)置輸出路徑 try { mRecorder.prepare(); mRecorder.start(); bResult = true; } catch (Exception ex) { System.out.print("Error:視頻錄制錯(cuò)誤、" + ex.getMessage()); bResult = false; } return bResult; }
/* * 停止視頻錄制 */ public boolean stopVideoRecording() { boolean bResult = false; if (mRecorder != null) { mRecorder.stop(); mRecorder.release(); mRecorder = null; bResult = true; } else { bResult = false; } return bResult; }
/* * SurfaceHolder.Callback接口實(shí)現(xiàn)方法 */ @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub System.out.println(holder.toString()); }
@Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub System.out.println(holder.toString()); }
@Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub if (context != null) { context = null; } if (mPlayer != null) { mPlayer = null; } if (mRecorder != null) { mRecorder = null; } } }
注:實(shí)現(xiàn)視頻錄制時(shí)需要在UI布局中添加 Surfaceview控件,用以實(shí)時(shí)預(yù)覽視頻。如分辨率過(guò)低是會(huì)導(dǎo)致視頻花屏! |