欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java中初始化MediaRecorder的实现方法

程序员文章站 2024-04-02 09:16:52
java中初始化mediarecorder 实现代码: private boolean initializevideo() { log.v(tag...

java中初始化mediarecorder

实现代码:

private boolean initializevideo() {  
    log.v(tag, "initializevideo");  
     
    if (msurfaceholder == null) {  
      log.v(tag, "surfaceholder is null");  
      return false;  
    }  
 
    mmediarecorderrecording = true;  
 
    if (mmediarecorder == null)  
        mmediarecorder = new mediarecorder();  
    else 
        mmediarecorder.reset();  
    if (mcamera != null) {  
        if (integer.parseint(build.version.sdk) >= 8)  
            videocameranew2.reconnect(mcamera);  
        mcamera.release();  
        mcamera = null;  
    }  
 
    if (usefront && integer.parseint(build.version.sdk) >= 5) {  
            if (isavailablesprintffc)  
            {  
                try 
                {  
                    method method = class.forname("android.hardware.htcfrontfacingcamera").getdeclaredmethod("getcamera", null);  
                    mcamera = (camera) method.invoke(null, null);  
                }  
                catch (exception ex)  
                {  
                    log.d(tag, ex.tostring());  
                }  
            } else {  
                mcamera = camera.open();  
                camera.parameters parameters = mcamera.getparameters();  
                parameters.set("camera-id", 2);  
                mcamera.setparameters(parameters);  
            }  
            videocameranew.unlock(mcamera);  
            mmediarecorder.setcamera(mcamera);  
        mvideopreview.setonclicklistener(this);  
    }  
    mvideopreview.setonlongclicklistener(this);  
    mmediarecorder.setvideosource(mediarecorder.videosource.camera);  
    mmediarecorder.setoutputformat(mediarecorder.outputformat.three_gpp);  
    mmediarecorder.setoutputfile(sender.getfiledescriptor());  
 
        mmediarecorder.setvideoframerate(20);  
    if (videoqualityhigh) {  
      mmediarecorder.setvideosize(352,288);  
    } else {  
      mmediarecorder.setvideosize(176,144);  
    }  
    mmediarecorder.setvideoencoder(mediarecorder.videoencoder.h263);  
    mmediarecorder.setpreviewdisplay(msurfaceholder.getsurface());  
 
    try {  
      mmediarecorder.prepare();  
      mmediarecorder.setonerrorlistener(this);  
      mmediarecorder.start();  
    } catch (ioexception exception) {  
      releasemediarecorder();  
      finish();  
      return false;  
    }  
    return true;  
  } 
private boolean initializevideo() { 
    log.v(tag, "initializevideo"); 
     
    if (msurfaceholder == null) { 
      log.v(tag, "surfaceholder is null"); 
      return false; 
    } 
 
    mmediarecorderrecording = true; 
 
    if (mmediarecorder == null) 
        mmediarecorder = new mediarecorder(); 
    else 
        mmediarecorder.reset(); 
    if (mcamera != null) { 
        if (integer.parseint(build.version.sdk) >= 8) 
            videocameranew2.reconnect(mcamera); 
        mcamera.release(); 
        mcamera = null; 
    } 
 
    if (usefront && integer.parseint(build.version.sdk) >= 5) { 
            if (isavailablesprintffc) 
            { 
                try 
                { 
                    method method = class.forname("android.hardware.htcfrontfacingcamera").getdeclaredmethod("getcamera", null); 
                    mcamera = (camera) method.invoke(null, null); 
                } 
                catch (exception ex) 
                { 
                    log.d(tag, ex.tostring()); 
                } 
            } else { 
                mcamera = camera.open();  
                camera.parameters parameters = mcamera.getparameters();  
                parameters.set("camera-id", 2);  
                mcamera.setparameters(parameters);  
            } 
            videocameranew.unlock(mcamera); 
            mmediarecorder.setcamera(mcamera); 
        mvideopreview.setonclicklistener(this); 
    } 
    mvideopreview.setonlongclicklistener(this); 
    mmediarecorder.setvideosource(mediarecorder.videosource.camera); 
    mmediarecorder.setoutputformat(mediarecorder.outputformat.three_gpp); 
    mmediarecorder.setoutputfile(sender.getfiledescriptor()); 
 
        mmediarecorder.setvideoframerate(20); 
    if (videoqualityhigh) { 
      mmediarecorder.setvideosize(352,288); 
    } else { 
      mmediarecorder.setvideosize(176,144); 
    } 
    mmediarecorder.setvideoencoder(mediarecorder.videoencoder.h263); 
    mmediarecorder.setpreviewdisplay(msurfaceholder.getsurface()); 
 
    try { 
      mmediarecorder.prepare(); 
      mmediarecorder.setonerrorlistener(this); 
      mmediarecorder.start(); 
    } catch (ioexception exception) { 
      releasemediarecorder(); 
      finish(); 
      return false; 
    } 
    return true; 
  } 
 
private void releasemediarecorder() {  
    log.v(tag, "releasing media recorder.");  
    if (mmediarecorder != null) {  
      mmediarecorder.reset();  
      if (mcamera != null) {  
            if (integer.parseint(build.version.sdk) >= 8)  
                videocameranew2.reconnect(mcamera);  
            mcamera.release();  
            mcamera = null;  
      }  
      mmediarecorder.release();  
      mmediarecorder = null;  
    }  
  }  
      
  public void onerror(mediarecorder mr, int what, int extra) {  
    if (what == mediarecorder.media_recorder_error_unknown) {  
      finish();  
    }  
  } 
private void releasemediarecorder() { 
    log.v(tag, "releasing media recorder."); 
    if (mmediarecorder != null) { 
      mmediarecorder.reset(); 
      if (mcamera != null) { 
            if (integer.parseint(build.version.sdk) >= 8) 
                videocameranew2.reconnect(mcamera); 
            mcamera.release(); 
            mcamera = null; 
      } 
      mmediarecorder.release(); 
      mmediarecorder = null; 
    } 
  } 
     
  public void onerror(mediarecorder mr, int what, int extra) { 
    if (what == mediarecorder.media_recorder_error_unknown) { 
      finish(); 
    } 
  } 
 
private void stopvideorecording() {  
    log.v(tag, "stopvideorecording");  
    if (mmediarecorderrecording || mmediarecorder != null) {  
        receiver.listener_video = null;  
        t.interrupt();  
      rtpstreamsender.delay = 0;  
 
      if (mmediarecorderrecording && mmediarecorder != null) {  
        try {  
          mmediarecorder.setonerrorlistener(null);  
          mmediarecorder.setoninfolistener(null);  
          mmediarecorder.stop();  
        } catch (runtimeexception e) {  
          log.e(tag, "stop fail: " + e.getmessage());  
        }  
 
        mmediarecorderrecording = false;  
      }  
      releasemediarecorder();  
    }  
  } 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!