简体   繁体   English

Android Lollipop上的MediaRecorder问题

[英]MediaRecorder issue on Android Lollipop

I'm testing libstreaming on new Android Lollipop , and this code that worked on previous release, seems to launch exception. 我正在测试新的Android Lollipop上的libstreaming ,这个代码在之前的版本中运行,似乎启动了异常。

    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());
    }

Launched exception is: 推出的例外是:

MediaRecorder: start failed -38 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)

I've tried to comment: 我试过评论:

mMediaRecorder.setOutputFile(mSender.getFileDescriptor());

no exception launched, but when I start streaming a dialog tell me that need an outputfile. 没有异常启动,但是当我开始流式传输对话框时告诉我需要一个输出文件。

Help appreciated. 帮助赞赏。

I filed a bug report on AOSP. 我提交了一份关于AOSP的错误报告。 https://code.google.com/p/android/issues/detail?id=80715 https://code.google.com/p/android/issues/detail?id=80715

"The current SELinux policies don't allow for mediaserver to handle app generated abstract unix domain sockets. “目前的SELinux政策不允许mediaserver处理应用程序生成的抽象unix域套接字。

Instead, I'd recommend you create a pipe-pair ( http://developer.android.com/reference/android/os/ParcelFileDescriptor.html#createPipe() ) which is allowed by the Android 5.0 policy. 相反,我建议您创建一个Android 5.0策略允许的管道对( http://developer.android.com/reference/android/os/ParcelFileDescriptor.html#createPipe() )。 " I don't know why they did this or how we were supposed to know. “我不知道他们为什么这样做或我们应该如何知道。

I'm using a very old/modified (can't tell) version of libstreaming where mediastream is still extended from mediarecorder, but looking at the current version, in MediaStream you'll probably want to change createSockets to something including the following: 我正在使用一个非常古老/修改过的版本的libstreaming,其中mediastream仍然从mediarecorder扩展,但是看看当前版本,在MediaStream中你可能想要将createSockets更改为包括以下内容:

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

then in your video/audio stream 然后在你的视频/音频流中

setOutputFile(parcelWrite.getFileDescriptor());

and in that same file change 并在同一个文件中更改

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

to

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

As andreasperelli pointed out in the comment, make sure to close the ParcelFileDescriptors in closeSockets(), or depending on your implementation and version, before closeSockets() and before you call MediaRecorder.stop(). 正如andreasperelli在评论中指出的那样,确保在closeSockets()之前和调用MediaRecorder.stop()之前关闭closeSockets()中的ParcelFileDescriptors,或者取决于你的实现和版本。

at Android 6.0 I resolve this problem with the code 在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();

I use a file as buffer and write bytes at another thread.the MediaRecorder output to the file. 我使用一个文件作为缓冲区并在另一个线程上写入字节.MediaRecorder输出到文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM