2014-12-31 201 views
1

我正在開發一款Android應用程序,通過本機視頻錄製視頻,代碼如下。現在,我想限制視頻尺寸不超過10M,比如在視頻超過限制時應停止錄製。如何使用Android錄製少於10 MB的視頻

private void recordVideo() { 
     Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 
     fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); 
     Toast.makeText(getApplicationContext(), fileUri.toString(), Toast.LENGTH_LONG).show(); 
     intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
     startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE); 
    } 


    public Uri getOutputMediaFileUri(int type) { 
     return Uri.fromFile(getOutputMediaFile(type)); 
    } 

    /* 
    * returning image/video 
    */ 
    private static File getOutputMediaFile(int type) { 

     // External sdcard location 
     File mediaStorageDir = new File(
       Environment 
         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
       IMAGE_DIRECTORY_NAME); 

     // Create the storage directory if it does not exist 
     if (!mediaStorageDir.exists()) { 
      if (!mediaStorageDir.mkdirs()) { 
       Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " 
         + IMAGE_DIRECTORY_NAME + " directory"); 
       return null; 
      } 
     } 

     // Create a media file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
       Locale.getDefault()).format(new Date()); 
     File mediaFile; 
     if (type == MEDIA_TYPE_IMAGE) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "IMG_" + timeStamp + ".jpg"); 
     } else if (type == MEDIA_TYPE_VIDEO) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "VID_" + timeStamp + ".mp4"); 
     } else { 
      return null; 
     } 

     return mediaFile; 
    } 
+0

感謝您的回覆@DerGolem。如果超過10 MB,我想在視頻中查看它。請幫我在這裏 –

回答

4

您需要添加一個參數MediaStore.EXTRA_SIZE_LIMIT開始錄製時,

ie:

private void recordVideo() { 
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); 
    Toast.makeText(getApplicationContext(), fileUri.toString(), Toast.LENGTH_LONG).show(); 
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

//add one more parameter 
    long maxVideoSize = 10*1024*1024; // 10 MB 
    intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, maxVideoSize); 

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
    startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE); 
} 
0

可以連續檢查文件的大小,同時記錄和停止它時,大小超過這樣的 -

public long TotalMemory()//Environment.getExternalStorageDirectory().getAbsolutePath() 
    { 
     StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath()); 
     long Total = ((long) statFs.getBlockCount() * (long) statFs.getBlockSize())/1048576; 
     return Total; 
    } 

public long FreeMemory() 
{ 
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath()); 
    long Free = (statFs.getAvailableBlocks() * (long) statFs.getBlockSize())/1048576; 
    return Free; 
} 

public long BusyMemory() 
{ 
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath()); 
    long Total = ((long) statFs.getBlockCount() * (long) statFs.getBlockSize())/1048576; 
    long Free = (statFs.getAvailableBlocks() * (long) statFs.getBlockSize())/1048576; 
    long Busy = Total - Free; 
    return Busy; 
} 

希望這有助於你:)

+0

感謝您的回覆。主席先生,請把你的方法放在我給定的代碼中,這樣我可以更方便地解決這個問題。 –