2017-06-01 73 views
-8

我想通过摄像头录制视频,我们开始录制摄像头预览应该有前,后,我们停止录制相机预览应该有我想通过摄像头录制视频的Android

我写了这个代码,它只是预览相机开始录制之前,当我停止录制相机不预览。帮我

MainActivity 


public class MainActivity extends Activity implements View.OnClickListener, SurfaceHolder.Callback { 
    MediaRecorder recorder; 
    SurfaceHolder holder; 
    Button st; 
    TextView live, stopWatch; 
    boolean recording = false; 
    boolean loop = false; 
    EditText barcode; 
    TextView head; 
    ArrayList<Subtitle> Sub; 
    long MillisecondTime, StartTime, TimeBuff, UpdateTime = 0L; 
    int Seconds, Minutes, MilliSeconds; 
    Handler handler; 
    AVLoadingIndicatorView loading; 
    LinearLayout videoSurface; 
    LinearLayout cameraSurface; 

    Timer t; 
    SimpleDateFormat d2; 
    SimpleDateFormat d1; 

    boolean duration = true; 
    boolean durationVal = false; 
    int p = 0; 


    private CameraPreview mCameraView = null; 
    FrameLayout camera_view; 
    SurfaceView cameraView; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

     recorder = new MediaRecorder(); 
     File myDirectory = new File(Environment.getExternalStorageDirectory(), "C3dssImageProcessing"); 

     if (!myDirectory.exists()) { 
      myDirectory.mkdirs(); 
     } 

     setContentView(R.layout.activity_main2); 

     cameraView = (SurfaceView) findViewById(CameraView); 





     st = (Button) findViewById(R.id.actionbtn); 
     live = (TextView) findViewById(R.id.liveStatus); 
     barcode = (EditText) findViewById(R.id.barcode); 
     stopWatch = (TextView) findViewById(R.id.stopwatch); 

     loading = (AVLoadingIndicatorView) findViewById(R.id.load); 
     head = (TextView) findViewById(R.id.header1); 



     initiateCamera(); 


     barcode.setEnabled(false); 


     getWindow().setSoftInputMode(
       WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN 
     ); 
     live.setVisibility(View.INVISIBLE); 

     holder = cameraView.getHolder(); 
     holder.addCallback(this); 
     holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

     st.setClickable(true); 
     st.setOnClickListener(this); 



     barcode.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

       File myDirectory = new File(Environment.getExternalStorageDirectory(), "beforeTextChanged"); 

       if (!myDirectory.exists()) { 
        myDirectory.mkdirs(); 
       } 

      } 

      @Override 
      public void onTextChanged(CharSequence ss, int start, int before, int count) { 
       File myDirectory = new File(Environment.getExternalStorageDirectory(), "onTextChanged"); 

       if (!myDirectory.exists()) { 
        myDirectory.mkdirs(); 
       } 

       // Toast.makeText(getApplicationContext(), "on text changed ", Toast.LENGTH_LONG).show(); 
       Subtitle subtitle = new Subtitle(); 
       subtitle.setBarcode(barcode.getText().toString()); 

       if (barcode.getText().length() > 12 && !(Sub == null)) { 
        barcode.setEnabled(false); 

        // Toast.makeText(getApplicationContext(), "barcode fired ", Toast.LENGTH_LONG).show(); 
        if (stopWatch.getText().length() <= 5) { 
         subtitle.setStartTime("00:"+stopWatch.getText().toString()); 
         String str = stopWatch.getText().toString(); 
         String substring = str.length() > 2 ? str.substring(str.length() - 2) : str; 
         int second = Integer.parseInt(substring); 
         second += 5; 
         if (second == 60) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          int minitute = Integer.parseInt(s); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 00; 
           subtitle.setEndTime("01:" + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 00; 
           subtitle.setEndTime("00:" + minitute + ":" + second); 
          } 
         } 

         if (second == 61) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          int minitute = Integer.parseInt(s); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 02; 
           subtitle.setEndTime("01:" + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 02; 
           subtitle.setEndTime("00:" + minitute + ":" + second); 
          } 
         } 
         if (second == 62) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          int minitute = Integer.parseInt(s); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 03; 
           subtitle.setEndTime("01:" + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 03; 
           subtitle.setEndTime("00:" + minitute + ":" + second); 
          } 
         } 

         if (second == 62) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          int minitute = Integer.parseInt(s); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 04; 
           subtitle.setEndTime("01:" + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 04; 
           subtitle.setEndTime("00:" + minitute + ":" + second); 
          } 
         } 
         if (second == 63) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          int minitute = Integer.parseInt(s); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 05; 
           subtitle.setEndTime("01:" + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 05; 
           subtitle.setEndTime("00:" + minitute + ":" + second); 
          } 
         } 
         if (second == 64) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          int minitute = Integer.parseInt(s); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 06; 
           subtitle.setEndTime("01:" + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 06; 
           subtitle.setEndTime("00:" + minitute + ":" + second); 
          } 
         } 
         if (second < 60) { 
          String s = str.length() < 2 ? str : str.substring(0, 2); 
          int minitute = Integer.parseInt(s); 
          subtitle.setEndTime("00:" + minitute + ":" + second); 
         } 
        } else { 
         subtitle.setStartTime(stopWatch.getText().toString()); 
         String str = stopWatch.getText().toString(); 
         String substring = str.length() > 2 ? str.substring(str.length() - 2) : str; 
         int second = Integer.parseInt(substring); 
         second += 5; 

         if (second == 60) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          String s1 = str.substring(4, 5); 
          int hour = Integer.parseInt(s); 
          int minitute = Integer.parseInt(s1); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 00; 
           hour += 1; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 00; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } 
         } 

         if (second == 61) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          String s1 = str.substring(4, 5); 
          int hour = Integer.parseInt(s); 
          int minitute = Integer.parseInt(s); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 02; 
           hour += 1; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 02; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } 
         } 
         if (second == 62) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          String s1 = str.substring(4, 5); 
          int hour = Integer.parseInt(s); 
          int minitute = Integer.parseInt(s); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 03; 
           hour += 1; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 03; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } 
         } 

         if (second == 62) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          String s1 = str.substring(4, 5); 
          int hour = Integer.parseInt(s); 
          int minitute = Integer.parseInt(s); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 04; 
           hour += 1; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 04; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } 
         } 
         if (second == 63) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          String s1 = str.substring(4, 5); 
          int hour = Integer.parseInt(s); 
          int minitute = Integer.parseInt(s); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 05; 
           hour += 1; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 05; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } 
         } 
         if (second == 64) { 

          String s = str.length() < 2 ? str : str.substring(0, 2); 
          String s1 = str.substring(4, 5); 
          int hour = Integer.parseInt(s); 
          int minitute = Integer.parseInt(s); 
          if (minitute == 59) { 
           minitute = 00; 
           second = 06; 
           hour += 1; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } else { 
           minitute += 1; 
           second = 06; 
           subtitle.setEndTime(hour + minitute + ":" + second); 
          } 
         } 
         if (second > 60) { 
          String s = str.length() < 2 ? str : str.substring(0, 2); 
          String s1 = str.substring(4, 5); 
          int hour = Integer.parseInt(s); 
          int minitute = Integer.parseInt(s1); 
          second = 00; 
          subtitle.setEndTime(hour + minitute + ":" + second); 
         } 

        } 
        Sub.add(subtitle); 

        ValueHolder.Sub = Sub; 

        Timer t = new Timer(true); 
        t.schedule(new TimerTask() { 
         @Override 
         public void run() { 
          runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            barcode.setText(""); 
            barcode.setEnabled(true); 
           } 


          }); 
         } 
        }, 5000); 
        File myDirectorytest = new File(Environment.getExternalStorageDirectory(), "onTextChangedValueHolder"); 

        if (!myDirectorytest.exists()) { 
         myDirectorytest.mkdirs(); 
        } 

       } 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 


     }); 
    } 

    private void initRecorder() { 
     recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

     CamcorderProfile cpHigh = CamcorderProfile 
       .get(CamcorderProfile.QUALITY_480P); 

     recorder.setProfile(cpHigh); 
     setDayTimestamp(); 
     setvideoNamestamp(); 
     recorder.setOutputFile("/sdcard/C3dssImageProcessing/" + ValueHolder.dayFile + "/" + ValueHolder.videoName + "/" + ValueHolder.videoName + ".mp4"); 
     recorder.setMaxDuration(86400000); // 50 seconds 
     recorder.setMaxFileSize(1000000000); // Approximately 5 megabytes 
    } 

    private void prepareRecorder() { 
     recorder.setPreviewDisplay(holder.getSurface()); 

     try { 
      recorder.prepare(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
      Log.e("illegal :",e.toString()); 
      // finish(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Log.e("IOEXCEPTION :",e.toString()); 
      //finish(); 
     } 
    } 

    public void onClick(final View v) { 


     final Thread stopWh = new Thread() { 
      final int[] hour = new int[1]; 
      final int[] minute = new int[1]; 
      final int[] second = new int[1]; 


      @Override 
      public void run() { 
       while (!isInterrupted()) { 
        if (!durationVal) { 
         second[0] = 00; 
         minute[0] = 00; 
         hour[0] = 00; 

        } 
        if (duration) { 
         runOnUiThread(new Runnable() { 

          @Override 
          public void run() { 
           durationVal = true; 
           second[0] += 1; 
           if (second[0] == 59) { 

            minute[0] += 1; 
            second[0] = 0; 
           } 
           if (minute[0] == 60) { 

            hour[0] = 1; 
            minute[0] = 0; 
           } 
           if (hour[0] > 0) { 
            DecimalFormat formatter = new DecimalFormat("00"); 

            stopWatch.setText(formatter.format(hour[0]) + ":" + formatter.format(minute[0]) + "" + formatter.format(second[0])); 
           } else { 
            DecimalFormat formatter = new DecimalFormat("00"); 
            stopWatch.setText(formatter.format(minute[0]) + ":" + formatter.format(second[0])); 
           } 
          } 
         }); 
        } 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 


     }; 


     if (recording) { 

      barcode.setEnabled(false); 
      barcode.setFocusable(false); 


      cameraView.setVisibility(View.INVISIBLE); 


      initiateCamera(); 





      loading.hide(); 

     camera_view.setVisibility(View.VISIBLE); 



      duration = false; 
      durationVal = false; 
      recording = false; 

      createSRTfile(ValueHolder.Sub); 
      stopWatch.setText("00:00"); 
      p = 1; 
      live.setVisibility(View.INVISIBLE); 
      live.setSelected(false); 
      st.setText("START"); 

      barcode.setFocusableInTouchMode(true); 
      barcode.setFocusable(true); 
      getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN 
      ); 

      loop = true; 


     } else { 


      recording = true; 



      barcode.setEnabled(true); 
      barcode.setFocusable(true); 

      if (p == 0) { 


      camera_view.setVisibility(View.INVISIBLE); 

       stopCamera(); 



       initRecorder(); 
       stopWh.start(); 
       cameraView.setVisibility(View.VISIBLE); 
      } else { 

      camera_view.setVisibility(View.INVISIBLE); 


       stopCamera(); 

       initRecorder(); 
       duration = true; 
       cameraView.setVisibility(View.VISIBLE); 

      } 


      recorder.start(); 
      loading.show(); 
      barcode.setFocusableInTouchMode(true); 
      barcode.setFocusable(true); 
      getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN 
      ); 
      Sub = new ArrayList<>(); 


      live.setVisibility(View.VISIBLE); 
      st.setText("STOP"); 

      live.setText("Live Recording..."); 
      live.setSelected(true); 


      File myDirectoryDay1 = new File(Environment.getExternalStorageDirectory(), "C3dssImageProcessing/" + ValueHolder.dayFile); 

      if (!myDirectoryDay1.exists()) { 
       myDirectoryDay1.mkdirs(); 
      } 


      File myDirectoryDay = new File(Environment.getExternalStorageDirectory(), "C3dssImageProcessing/" + ValueHolder.dayFile + "/" + ValueHolder.videoName); 

      if (!myDirectoryDay.exists()) { 
       myDirectoryDay.mkdirs(); 
      } 


     } 


    } 

    public void surfaceCreated(SurfaceHolder holder) { 



     prepareRecorder(); 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
           int height) { 


    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     Toast.makeText(getApplicationContext(), "surface destroyed on main Activity", Toast.LENGTH_LONG).show(); 




     if (recording) { 
      recorder.stop(); 

      recording = false; 
     } 
     recorder.reset(); 



    } 

    private void setDayTimestamp() { 

     ValueHolder.dayFile = new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date()); 
     File myDirectory = new File(Environment.getExternalStorageDirectory(), "C3dssImageProcessing/" + ValueHolder.dayFile); 

     if (!myDirectory.exists()) { 
      myDirectory.mkdirs(); 
     } 

    } 

    private void setvideoNamestamp() { 

     ValueHolder.videoName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new java.util.Date()); 

     File myDirectoryDay = new File(Environment.getExternalStorageDirectory(), "C3dssImageProcessing/" + ValueHolder.dayFile + "/" + ValueHolder.videoName); 

     if (!myDirectoryDay.exists()) { 
      // Toast.makeText(getApplicationContext(), "v name : "+ValueHolder.videoName, Toast.LENGTH_LONG).show(); 
      myDirectoryDay.mkdirs(); 
     } 
    } 




    private void createSRTfile(ArrayList<Subtitle> sub) { 

     if (!(sub == null)) { 

      if (sub.size() > 0) { 
       try { 

        BufferedWriter writer1 = new BufferedWriter(new FileWriter("/sdcard/C3dssImageProcessing/" + ValueHolder.dayFile + "/" + ValueHolder.videoName + "/" + ValueHolder.videoName + ".srt")); 

        // PrintWriter writer = new PrintWriter("/sdcard/C3dssImageProcessing/" + ValueHolder.dayFile + "/" + ValueHolder.videoName + ".srt", "UTF-8"); 
        for (int i = 0; i < sub.size(); i++) { 

         if (i == 0) { 
          writer1.write("1"); 

          //writer.println("1"); 
          writer1.newLine(); 
         } else { 
          int p = i+1; 
          writer1.write(p); 
          writer1.newLine(); 
         } 

         writer1.write(sub.get(i).getStartTime() + ",560 --> " + sub.get(i).getEndTime() + ",580 "); 
         // writer1.write(newline); 
         writer1.newLine(); 
         writer1.write(sub.get(i).getBarcode()+"\n"); 
         writer1.newLine(); 
         writer1.newLine(); 


        } 
        writer1.close(); 
       } catch (IOException e) { 
        // do something 
       } 
      } 
     } 

    } 

    @Override 
    public void onBackPressed() { 


     super.onBackPressed(); 
    } 




    private void initiateCamera(){ 

     camera_view = (FrameLayout)findViewById(R.id.camerPreview); 
     Camera mCamera = null; 
     try{ 
      mCamera = Camera.open();//you can use open(int) to use different cameras 
     } catch (Exception e){ 
      Log.d("ERROR", "Failed to get camera: " + e.getMessage()); 
     } 

     if(mCamera != null) { 
      mCameraView = new CameraPreview(this, mCamera);//create a SurfaceView to show camera data 
      ValueHolder.cam = mCamera; 

      camera_view.addView(mCameraView);//add the SurfaceView to the layout 
     } 


    } 

    private void stopCamera(){ 


     ValueHolder.cam.stopPreview(); 

     ValueHolder.cam.release(); 



    } 

} 
+4

太多的代码。没有人想要全部阅读。请付出一些努力并将问题本地化。 –

+0

使用此..可能对您有所帮助.. http://www.techotopia.com/index.php/Video_Recording_and_Image_Capture_on_Android_using_Camera_Intents –

+0

https://developer.android.com/training/camera/videobasics.html –

回答

-3

你可以用这个库来实现结果。 Material Camera

示例代码使用的lib整合后此lib下,

public class MainActivity extends AppCompatActivity { 

    private final static int CAMERA_RQ = 6969; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     new MaterialCamera(this) 
      .start(CAMERA_RQ); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     // Received recording or error from MaterialCamera 
     if (requestCode == CAMERA_RQ) { 

      if (resultCode == RESULT_OK) { 
       Toast.makeText(this, "Saved to: " + data.getDataString(), Toast.LENGTH_LONG).show(); 
      } else if(data != null) { 
       Exception e = (Exception) data.getSerializableExtra(MaterialCamera.ERROR_EXTRA); 
       e.printStackTrace(); 
       Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 
} 
+1

一个环节都不能作为一个答案 –

+0

@RukshanWithanage进​​行处理,看起来像要得到禁止远远超过得到答案。我会解释一下,什么是问题。 StackOverflow的设计**不是为了解决你的问题**,而是为了**收集解决方案的数据库**。只有链接回答清楚地陈述为一个**不同的答案**。如果你没有阅读规则,那么你既没有使用SO的权限,也没有告诉谁应该闭嘴。 –

+0

@RukshanWithanage对不起的答案感到抱歉。我已经用一些提示更新了答案。 – Vicky

相关问题