如何在android中记录来自rtsp url的实时流式视频

时间:2017-03-22 04:43:18

标签: android ffmpeg

我正在做一个Android应用程序。在我的应用程序中,我需要录制直播视频。在这里,我使用 rtsp 协议和 ffmpeg 库来流式传输和录制视频。直播工作正常,但视频录制无法正常工作。我认为这是一个URL连接的问题我在这里用来录制视频。但我不知道什么是正确的连接方法。我搜索了很多关于这个,但找不到任何东西。如果有人知道这一点,请帮助我。

这是我的 startRecording()方法

private void startRecording() {

    try {

        URL url = new URL(path);
        URLConnection urlConnection1 = url.openConnection();
        in1 = new BufferedInputStream(urlConnection1.getInputStream());

    } catch (IOException e) {
        e.printStackTrace();
    } 

    try {

        mIn = new MjpegInputStream(in1,MIN_ARRAY_LENGTH);
        mIn.resetFrameCount();
      //  return new MjpegInputStream(in, MIN_ARRAY_LENGTH);
        recorder.start();
        calltimer();

        audioStatus = Micstatus.equals("1");
        startTime = 0;
        completedFrames = 0;
        audioCompleted = 0;

        isRecording = true;
        if (isJelliBean) {
            resetTextureLayout();
        }
        trial.setCanZoom(isRecording);
        setMenuEnabled();

        threadVideo = new VideoRecording();
        threadVideo.start();

        if (audioStatus) {
            threadAudio = new AudioRecordRunnables();
            threadAudio.start();
        }


    } catch (Exception e) {
        try {

            isRecording = false;
            trial.setCanZoom(isRecording);
            setMenuEnabled();

            Toast.makeText(getApplicationContext(), "Try again1", Toast.LENGTH_SHORT).show();
        } catch (Exception e1) {
        }
    }

}

这是我的 ViedoRecording

private class VideoRecording extends Thread implements Runnable {

    public void run() {
        try {


            int i = -1;
            int cc = 0;
            int completed = 0;

            FileOutputStream out;
            RecordingHelper recordingHelper;
            try {
                Thread.sleep(VIDEO_FRAME_RATE < 6 ? 2000 : 300);
            } catch (Exception e) {

            }
            String file = getCacheDir() + "/temp.jpg";
            opencv_core.IplImage iplImage;
            runOnUiThread(videoTimeRunnable);

            while (isRecording || completed < mIn.totalFrames || true)             {
                try {
                    if (cc < 10) {
                        cc++;
                    }

                    if (i > (MIN_ARRAY_LENGTH - 2)) {
                        i = -1;
                    }
                    recordingHelper = mIn.datas[i + 1];


                    if (recordingHelper != null && recordingHelper.length > 1) {
                        i++;
                        completed++;
                        if (startTime == 0) {
                            startTime = System.currentTimeMillis();
                        }

                        completedFrames++;


                        out = new FileOutputStream(file);
                        out.write(recordingHelper.data);
                        out.flush();

                        out.close();

                        Log.e("recording", "=" + recordingHelper.rotaion);
                        iplImage = cvLoadImage(file, 1);

                        OpenCVFrameConverter.ToIplImage grabberConverter = new OpenCVFrameConverter.ToIplImage();
                        Frame frame = grabberConverter.convert(iplImage);
                        recorder.record(frame);
                        opencv_core.cvReleaseImage(iplImage);
                        mIn.datas[i] = null;
                        totalFramesRecordedByActivity++;
                        runOnUiThread(videoTimeRunnable);

                    } else if (!isRecording && startTime > 0) {
                        break;
                    } else if (cc > 5 && mIn.totalFrames < 1) {
                        break;
                    }

                } catch (Exception e) {

                }

            }

            new File(file).delete();
            endTime = System.currentTimeMillis();
            videoThreadFinished = true;

            finalizeRecording();


        } catch (Throwable t) {
            finishThis();

        }

    }
}

0 个答案:

没有答案