Android棒棒糖上的MediaRecorder问题

我正在testing新的Android Lollipop上的libstreaming ,而这个在以前的版本上工作的代码似乎启动了exception

try { mMediaRecorder = new MediaRecorder(); mMediaRecorder.setCamera(mCamera); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mMediaRecorder.setVideoEncoder(mVideoEncoder); mMediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface()); mMediaRecorder.setVideoSize(mRequestedQuality.resX,mRequestedQuality.resY); mMediaRecorder.setVideoFrameRate(mRequestedQuality.framerate); // The bandwidth actually consumed is often above what was requested mMediaRecorder.setVideoEncodingBitRate((int)(mRequestedQuality.bitrate*0.8)); // We write the ouput of the camera in a local socket instead of a file ! // This one little trick makes streaming feasible quiet simply: data from the camera // can then be manipulated at the other end of the socket mMediaRecorder.setOutputFile(mSender.getFileDescriptor()); mMediaRecorder.prepare(); mMediaRecorder.start(); } catch (Exception e) { throw new ConfNotSupportedException(e.getMessage()); } 

推出的例外是:

MediaRecorder:启动失败-38

 11-18 09:50:21.028: W/System.err(15783): net.majorkernelpanic.streaming.exceptions.ConfNotSupportedException 11-18 09:50:21.028: W/System.err(15783): at net.majorkernelpanic.streaming.video.VideoStream.encodeWithMediaRecorder(VideoStream.java:442) 11-18 09:50:21.028: W/System.err(15783): at net.majorkernelpanic.streaming.MediaStream.start(MediaStream.java:250) 

我试着评论:

 mMediaRecorder.setOutputFile(mSender.getFileDescriptor()); 

没有例外启动,但是当我开始stream媒体对话告诉我需要一个输出文件。

帮助赞赏。

Solutions Collecting From Web of "Android棒棒糖上的MediaRecorder问题"

我提交了AOSP的错误报告。 https://code.google.com/p/android/issues/detail?id=80715

“目前的SELinux策略不允许mediaserver处理应用生成的抽象的unix域套接字。

相反,我build议您创build一个由Android 5.0策略所允许的pipe道对( http://developer.android.com/reference/android/os/ParcelFileDescriptor.html#createPipe() )。 “我不知道他们为什么这样做,或者我们应该怎么知道。

我正在使用libstreaming的一个非常旧的/修改的(不能说)版本,其中mediastream仍然是从mediarecorder扩展的,但是在MediaStream中查看当前版本,您可能需要将createSockets更改为以下内容:

  ParcelFileDescriptor[] parcelFileDescriptors =ParcelFileDescriptor.createPipe(); parcelRead = new ParcelFileDescriptor(parcelFileDescriptors[0]); parcelWrite = new ParcelFileDescriptor(parcelFileDescriptors[1]); 

然后在你的video/audiostream

 setOutputFile(parcelWrite.getFileDescriptor()); 

并在相同的文件更改

  // The packetizer encapsulates the bit stream in an RTP stream and send it over the network mPacketizer.setInputStream(mReceiver.getInputStream()); mPacketizer.start(); 

  InputStream is = null; try{ is = new ParcelFileDescriptor.AutoCloseInputStream(parcelRead); } catch (Exception e){} mPacketizer.setInputStream(is); 

正如andreasperelli在注释中指出的那样,确保在closeSockets()中closuresParcelFileDescriptors,或者在调用MediaRecorder.stop()之前,根据您的实现和版本closuresParcelFileDescriptors。

在Android 6.0我解决这个问题的代码

 new Thread(new Runnable() { @Override public void run() { FileInputStream inputStream = null; try { inputStream = new FileInputStream(path); } catch (FileNotFoundException e) { e.printStackTrace(); } while (true) { byte[] buffer = new byte[0]; try { buffer = new byte[inputStream.available()]; } catch (IOException e) { e.printStackTrace(); } try { inputStream.read(buffer); } catch (IOException e) { e.printStackTrace(); } try { mSender.getOutputStream().write(buffer); mSender.getOutputStream().flush(); } catch (IOException e) { e.printStackTrace(); } } } }).start(); 

我使用一个文件作为缓冲区,并在另一个线程写入字节。MediaRecorder输出到文件。