2011-10-31 35 views
20

Tôi đang sử dụng android.provider.MediaStore.ACTION_VIDEO_CAPTURE. Tôi đã tự hỏi nếu có một cách để thay đổi thời gian tối đa cho phép cho mỗi bản ghi âm. TÔI ĐÃ ĐƯỢC THÊM Intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,60000);//max of 60 seconds nhưng nó vẫn tiếp tục ghi lại. Cảm ơn trước.Có thể đặt thời gian tối đa cho phép ghi âm android bằng ý định không?

+3

Xin lưu ý rằng MediaStore.EXTRA_DURATION_LIMIT không được tính bằng giây, không phải là mili giây. Nó chỉ hoạt động cho các thiết bị sau 2.0. – user953768

Trả lời

2

Sử dụng MediaRecorder

/** 
    * Starts a new recording. 
    */ 
    public void start() throws IOException { 

    recorder = new MediaRecorder(); 

    String state = android.os.Environment.getExternalStorageState(); 

    if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) { 
     throw new IOException("SD Card is not mounted. It is " + state 
      + "."); 
    } 

    // make sure the directory we plan to store the recording in exists 
    File directory = new File(path).getParentFile(); 
    System.out.println("start() directory > " + directory); 
    if (!directory.exists() && !directory.mkdirs()) { 
     throw new IOException("Path to file could not be created."); 
    } 



    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // Sets the 
    // audio source 
    // to be used 
    // for recording 



    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // Sets 
    // the 
    // format 
    // of 
    // the 
    // output 
    // file 
    // produced 
    // during 
    // recording. 
    // 5 Minutes = 300000 Milliseconds 

    recorder.setMaxDuration(300000); // Sets the maximum duration (in ms) of 
    // the recording session 



    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // Sets the 
    // audio 
    // encoder 
    // to be 
    // used for 
    // recording. 

    recorder.setOutputFile(path); // Sets the path of the output file to be 
    // produced. 
    recorder.prepare(); // Prepares the recorder to begin capturing and 
    // encoding data. 
    recorder.start(); // Recording is now started 

}

+0

Cảm ơn bạn Jennifer, tôi đã thử sử dụng bộ ghi phương tiện để quay video nhưng không ổn định trên một số nền tảng như Samsung Galaxy. Tôi đã hy vọng rằng có một cách để chỉ cần thêm một thời gian tối đa bởi vì tôi cần tất cả mọi thứ mà mục đích action_capture sử dụng. Bất kỳ ý tưởng? – user875139

+1

u đã thử: android.provider.MediaStore.EXTRA_DURATION_LIMIT phải không ?? – jennifer

+0

Vâng, tôi đã thử điều đó và intent.putExtra này ("android.intent.extra.durationLimit", 60000) ;. Vẫn không có gì. – user875139

15
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 
intent.putExtra("android.intent.extra.durationLimit", 30000); 
intent.putExtra("EXTRA_VIDEO_QUALITY", 0); 
startActivityForResult(intent, ActivityRequests.REQUEST_TAKE_VIDEO); 

Mã này hoạt động tốt trên API 2.2, nhưng giới hạn thời gian không hoạt động trên API 2.1

android.intent.extra.durationLimit được giới thiệu vào API Level 8, vì vậy nó không có sẵn trong Eclair và trước đó, thật không may. Một số nhà sản xuất thiết bị có thể có một cách độc quyền để thiết lập thời lượng tối đa trên các thiết bị cũ hơn, điều này giải thích lý do tại sao bạn thấy điều này làm việc trên một số ứng dụng trước Froyo.

+0

bạn có thể thấy cấp API mà tại đó mỗi biến được giới thiệu bằng cách nhìn sang bên phải của thanh màu xám trên trang web tài nguyên Android. Ví dụ: xem biến này (và cấp API) tại đây: http://developer.android.com/reference/android/provider/MediaStore.html#EXTRA_DURATION_LIMIT – jennifer

+0

Kiểm tra liên kết này cũng: http://www.netmite.com /android/mydroid/donut/packages/apps/Camera/src/com/android/camera/VideoCamera.java .. Nó sẽ hữu ích cho bạn – jennifer

+1

Tôi gặp lỗi ActivityRequests ở đây? –

30

Thực tế, MediaStore.EXTRA_DURATION_LIMIT cung cấp thời gian trong giây, KHÔNG tính bằng mili giây! Vì vậy, bạn chỉ cần thay đổi giá trị của bạn 60.000-60;) Android Documentation

3

Sử dụng này, đây là 60 thứ hai Code: intent.putExtra (MediaStore.EXTRA_DURATION_LIMIT, 60);

6

Trong 30 giây, hãy thử mã này.

intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30); 
Các vấn đề liên quan