簡而言之,我正在尋找一種在錄製視頻時從攝像機獲取字節流的方法。 目標是在保存當前錄製的某些部分的同時連續錄製,而不必停止實際的錄製過程來訪問輸出文件。這甚至是可能的,還是我需要實際停止錄製並保存爲可播放?錄製時訪問輸出視頻
我見過允許通過本地套接字和ParcelFileDescriptor類從相機直接傳輸到服務器的項目和開源庫,所以我假設(也許不正確)記錄器字節流必須以某種方式訪問。
任何建議或幫助將不勝感激。
簡而言之,我正在尋找一種在錄製視頻時從攝像機獲取字節流的方法。 目標是在保存當前錄製的某些部分的同時連續錄製,而不必停止實際的錄製過程來訪問輸出文件。這甚至是可能的,還是我需要實際停止錄製並保存爲可播放?錄製時訪問輸出視頻
我見過允許通過本地套接字和ParcelFileDescriptor類從相機直接傳輸到服務器的項目和開源庫,所以我假設(也許不正確)記錄器字節流必須以某種方式訪問。
任何建議或幫助將不勝感激。
設置輸出文件的FileDescriptor:
mRecorder.setOutputFile(getStreamFd());
然後使用此功能:
private FileDescriptor getStreamFd() {
ParcelFileDescriptor[] pipe = null;
try {
pipe = ParcelFileDescriptor.createPipe();
new TransferThread(new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]),
new FileOutputStream(getOutputFile())).start();
} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Exception opening pipe", e);
}
return (pipe[1].getFileDescriptor());
}
private File getOutputFile() {
return (new File(Environment.getExternalStorageDirectory().getPath().toString() + "/YourDirectory/filename"));
}
新線程代碼:
static class TransferThread extends Thread {
InputStream in;
FileOutputStream out;
TransferThread(InputStream in, FileOutputStream out) {
this.in = in;
this.out = out;
}
@Override
public void run() {
byte[] buf = new byte[8192];
int len;
try {
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.flush();
out.getFD().sync();
out.close();
} catch (IOException e) {
Log.e(getClass().getSimpleName(),
"Exception transferring file", e);
}
}
}
不要忘記persmissions添加到您的清單file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我有一個類似的問題,並希望在訪問H264 NAL單元相機字節流(將其重定向到libRTMP)時在MP4文件中記錄H264。下面的示例幫助了很多(至少需要的Android 4.3):
http://bigflake.com/mediacodec/ < - 的CameraToMpegTest.java和「機器人突圍遊戲錄音機補丁」的例子
基本上,機器人會MediaCodec類提供到裝置的編碼器低級別的訪問/解碼器。就拿上面的例子來看看功能drainEncoder():
例子:
int old_pos = encodedData.position();
encodedData.position(0);
byte[] encoded_array = new byte[encodedData.remaining()];
encodedData.get(encoded_array);
encodedData.position(old_pos);
通過使用這種方法,我得到的是不能播放的視頻文件。有什麼建議麼 ? –