[英]How to set maximum duration when recording audio in android using an intent
[英]Is it possible to set a maximum time allowed for android recording using intent?
我正在使用android.provider.MediaStore.ACTION_VIDEO_CAPTURE
。 我想知道是否有辦法改變每次錄制允許的最長時間。 我嘗試添加Intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,60000);//max of 60 seconds
但它會繼續記錄傳遞。 提前致謝。
實際上, MediaStore.EXTRA_DURATION_LIMIT
以秒為單位提供時間,而不是以毫秒為單位! 所以你只需要將你的值從60000改為60;) Android文檔
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);
此代碼適用於API 2.2,但持續時間限制不適用於API 2.1
android.intent.extra.durationLimit
是在API Level 8,
引入的API Level 8,
所以不幸的是API Level 8,
它在Eclair和更早版本中不可用。 某些設備制造商可能采用專有方法設置舊設備的最大持續時間,這可以解釋為什么您已經看到這種方法可用於某些Froyo之前的應用程序。
30秒時間,請嘗試此代碼。
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30);
使用此,這里60是第二個代碼:intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,60);
這是一個Kotlin更新,用於啟動ACTION_VIDEO_CAPTURE
Intent, EXTRA_DURATION_LIMIT
設置為60秒。 至於putExtra(MediaStore.EXTRA_DURATION_LIMIT, 60)
以秒為單位作為持續時間限制的值。
val recordVideoIntent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
recordVideoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 60)
startActivityForResult(recordVideoIntent, INTENT_VIDEO_RECORD_REQUEST)
使用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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.