<!--音频录制权限--> <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> <!--视频录制权限--> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<pre name="code" class="java">package cn.media; import java.io.File; import java.io.IOException; import android.app.Activity; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; //视频录制:</pre><pre name="code" class="java">public class MediaConnection extends Activity { private File myRecAudioFile; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private Button buttonStart; private Button buttonStop; private File dir; private MediaRecorder recorder; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.video); mSurfaceView = (SurfaceView) findViewById(R.id.videoView); mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); buttonStart = (Button) findViewById(R.id.start); buttonStop = (Button) findViewById(R.id.stop); File defaultDir = Environment.getExternalStorageDirectory(); String path = defaultDir.getAbsolutePath() + File.separator + "V" + File.separator; // 创建文件夹存放视频 dir = new File(path); if (!dir.exists()) { dir.mkdir(); } recorder = new MediaRecorder(); buttonStart.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { recorder(); } }); buttonStop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { recorder.stop(); recorder.reset(); recorder.release(); recorder = null; } }); } public void recorder() { try { myRecAudioFile = File.createTempFile("video", ".3gp", dir);// 创建临时文件 recorder.setPreviewDisplay(mSurfaceHolder.getSurface());// 预览 recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);// 视频源 recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 录音源为麦克风 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // 输出格式为3gp recorder.setVideoSize(800, 480);// 视频尺寸 recorder.setVideoFrameRate(15);// 视频帧频率 recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);// 视频编码 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // 音频编码 recorder.setMaxDuration(10000);// 最大期限 recorder.setOutputFile(myRecAudioFile.getAbsolutePath()); // 保存路径 recorder.prepare(); recorder.start(); } catch (IOException e) { e.printStackTrace(); } } } </pre>
音频录制:
package cn.media; import java.io.File; import java.io.IOException; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Intent; import android.media.MediaRecorder; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; /** * 音频录制 * @author Administrator * */ public class MyMediaRecoder extends Activity { MediaRecorder mrec; File audiofile=null; private static final String TAG="SoundRecordingDemo"; protected void startRecording() throws IOException { // 设置音频源 mrec.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置文件输出格式 mrec.setOutputFormat(MediaRecorder.AudioEncoder.AMR_NB); if(audiofile==null){ File sampleDir=Environment.getExternalStorageDirectory(); try{ audiofile=File.createTempFile("ibm", ".3gp",sampleDir); } catch(IOException e){ Log.e(TAG,"sdcard access error"); return; } } // 文件输出路径 mrec.setOutputFile(audiofile.getAbsolutePath()); mrec.prepare(); mrec.start(); } protected void stopRecording(){ mrec.stop(); mrec.release(); processAudiofile(audiofile); } protected void processAudiofile(File audiofile){ ContentValues values=new ContentValues(3); long current=System.currentTimeMillis(); values.put(MediaStore.Audio.Media.TITLE,"audio"+audiofile.getName()); values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName()); values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000)); values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp"); values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath()); ContentResolver contentResolver=getContentResolver(); Uri base=MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Uri newUri=contentResolver.insert(base,values); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,newUri)); } }
Intent intent=new Intent(android.content.Intent.ACTION_VIEW); File sdcardPath=Environment.getExternalStorageDirectory(); Uri audioFileUri=Uri.fromFile(sdcardPath); intent.setDataAndType(audioFileUri,"audio/mp3"); startActivity(intent);权限: