zoukankan      html  css  js  c++  java
  • 设置视频MediaRecorder类

    题记:写这篇博客要主是加深自己对设置视频的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢。

        

    MediaRecorder类

        MediaRecorder类用来停止体媒采样,包含音频和视频。

        


        

    上面是一段最单简的音录代码示例:

        

    MediaRecorder recorder= newMediaRecorder();  

        

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  

        

    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);  

        

    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);  

        

    recorder.setOutputFile(PATH_NAME);  

        

    recorder.prepare();  

        

    recorder.start(); // Recording is nowstarted  ...  

        

    recorder.stop();  

        

    recorder.reset(); // You can reuse the objectby going back to setAudioSource()step   

        recorder.release();// Now the object cannot bereused  

        每日一道理
    书,各种各样的书。书,寄托着人类热切的希望;书,蕴含着人类丰富的感悟。提起书,会有说不完的话语……

        



    MediaRecorder类的用常方法:

    MediaRecorder() 构造方法
    getMaxAmplitude() 到得目前为止大最的幅度
    prepare() 备准音录机
    release() 释放MediaRecorder对象 
    reset() 置重MediaRecorder对象,使其为闲暇状态
    setAudioEncoder() 设置音频编码
    setAudioSource() 设置音频源
    setCamera() 设置摄像机
    setMaxDuration() 设置大最限期
    setMaxFileSize() 设置件文的大最尺寸
    setOnErrorListener() 错误监听
    setOutputFile() 设置输出件文
    setOutputFormat() 设置输出件文的式格
    setPreviewDisplay() 设置预览
    setVideoEncoder() 设置视频编码
    setVideoFrameRate() 设置视频帧的频率
    setVideoSize() 设置视频的宽度和高度(分辨率)
    setVideoSource() 设置视频源
    start() 开始制录
    stop() 停止制录



    取自《Pro Android》的示例

    main.xml:

    <?xml version="1.0"encoding="utf-8"?>
    <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          >
    <Button
          android:id="@+id/bgnBtn"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Begin Recording"
          />
    <Button 
          android:id="@+id/stpBtn"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Stop Recording"
          />
    <Button
          android:id="@+id/playRecordingBtn"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Play Recording"
          />
    <Button
          android:id="@+id/stpPlayingRecordingBtn"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Stop Playing Recording"
          />

    </LinearLayout>




    MediaRecorderActivity.java :

    package com.hxxy2011.MediaRecorder;

    import java.io.File;

    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class MediaRecorderActivity extends Activity {
         
    private MediaPlayer mediaPlayer;
    private MediaRecorder recorder;
    private static final StringOUTPUT_FILE="/sdcard/recordoutput.3gpp";
          @Override
          publicvoid onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
             
              ButtonstartBtn=(Button)findViewById(R.id.bgnBtn);
              ButtonendBtn=(Button)findViewById(R.id.stpBtn);
              ButtonplayRecordingBtn=(Button)findViewById(R.id.playRecordingBtn);
              ButtonstpPlayingRecordingBtn=(Button)findViewById(R.id.stpPlayingRecordingBtn);
             
             
              startBtn.setOnClickListener(newOnClickListener(){

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    try{
    beginRecording();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
             
              });
              endBtn.setOnClickListener(newOnClickListener(){

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    try{
    stopRecording();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
             
              });
              playRecordingBtn.setOnClickListener(newOnClickListener(){

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    try{
    playRecording();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
             
              });
              stpPlayingRecordingBtn.setOnClickListener(newOnClickListener(){

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    try{
    stopPlayingRecording();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
             
              });
          }
          privatevoid beginRecording()throws Exception{
          killMediaRecorder();
         
          FileoutFile=new File(OUTPUT_FILE);
         
          if(outFile.exists())
          {
          outFile.delete();
          }
          recorder=new MediaRecorder();
          recorder.setAudioEncoder(MediaRecorder.AudioSource.MIC);
          recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
          recorder.setOutputFile(OUTPUT_FILE);
          recorder.prepare();
          recorder.start();
          }
          privatevoid stopRecording() throws Exception{
          if(recorder!=null){
          recorder.stop();
          }
          }
          privatevoid killMediaRecorder(){
          if(recorder!=null)
          {
          recorder.release();
          }
          }
         
          privatevoid killMediaPlayer(){
          if(mediaPlayer!=null){
          try{
          mediaPlayer.release();
          }catch(Exception e){
          e.printStackTrace();
          }
          }
          }
          privatevoid playRecording() throws Exception{
          killMediaPlayer();
         
          mediaPlayer=new MediaPlayer();
          mediaPlayer.setDataSource(OUTPUT_FILE);
         
          mediaPlayer.prepare();
          mediaPlayer.start();
          }
          privatevoid stopPlayingRecording() throws Exception{
          if(mediaPlayer!=null)
          {
          mediaPlayer.stop();
          }
          }
         
          protected void onDestroy(){
          super.onDestroy();
         
          killMediaRecorder();
          }
    }

    文章结束给大家分享下程序员的一些笑话语录: 自从有了Photoshop,我再也不相信照片了!(没有Photoshop的年代,胶片照片年代做假的也不少,那时候都相信假的!)

  • 相关阅读:
    Java基础--JDBC
    DQL---连接查询(内连接、外连接)、子查询、分页查询
    Java基础--注解、反射
    1、Centos7系统安装docker,并配置阿里云镜像加速器
    linux——vim命令
    linux——yum命令
    linux——ps命令
    31、springboot——缓存之JSR107——@Caching和@CacheConfig的使用⑤
    30、springboot——缓存之JSR107——@CacheEvict的使用④
    30、springboot——缓存之JSR107——@CachePut的使用③
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3069953.html
Copyright © 2011-2022 走看看