“Seekable” file descriptor to use with MediaRecorder Android 6.0 (API 23)

a 夏天 提交于 2019-12-10 23:49:09

问题


So I am trying to use my Android as a webcam. My code is working perfectly for API <= 22. I am using ParcelFileDescriptor's createPipe() method to create pipe for reading and writing.

In short, my writing to the pipe looks like following:

ParcelFileDescriptor[] parcelFileDescriptors = ParcelFileDescriptor.createPipe();

ParcelFileDescriptor mParcelWrite =new ParcelFileDescriptor(mParcelFileDescriptors[1]);

MediaRecorder mMediaRecorder = new MediaRecorder();
mMediaRecorder.setOutputFile(mParcelWrite.getFileDescriptor());
..
..// Other settings.
mMediaRecorder.start();

When I run this, I get the following exception,

E/MediaRecorder: start failed: -2147483648
W/System.err: java.lang.RuntimeException: start failed. W/System.err: at android.media.MediaRecorder.start(Native Method)
W/System.err: at com.ksy.recordlib.service.recoder.RecoderVideoSource.prepare(RecoderVideoSource.java:105)
W/System.err: at com.ksy.recordlib.service.recoder.RecoderVideoSource.run(RecoderVideoSource.java:173) 
W/System.err: at java.lang.Thread.run(Thread.java:818)

I got to know, that in API 23, they made a change which prevented MediaRecorder to work with File descriptors that are not seekable.

ParcelFileDescriptor when used with createPipe() doesn't seem to be seekable.

My question is, How can I make it seekable? Is there any alternative to it? Thanks.


回答1:


I have been investigating this for a week. There is a hidden format in the output formats you can set which is

mMediaRecorder.setOutputFormat(8);

From the documentation

**@hide H.264/AAC data encapsulated in MPEG2/TS 

public static final int OUTPUT_FORMAT_MPEG2TS = 8;**

However it does not still work on Marshmallow or Nougat. I made it work for kit kat. Let me know if this helps.



来源:https://stackoverflow.com/questions/39994345/seekable-file-descriptor-to-use-with-mediarecorder-android-6-0-api-23

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!