如何录制在SurfaceView中显示的Ip Camera视频?

时间:2016-01-19 11:13:06

标签: android video-recording

我目前正在开展一个项目。我现在可以从Ip相机获取视频,我想录制在surfaceView中显示的视频。我不知道如何做到这一点。这是我写的视频预览代码。任何帮助将不胜感激。

public class Camera_Fragment1 extends Fragment implements IVideoPlayer, View.OnTouchListener, View.OnClickListener {
private RotateCam Task = new RotateCam();
private StopCam Task1 = new StopCam();
private ConnectionAdapter adapter = new ConnectionAdapter();
private TextView tv;
private Button Cam1_up, Cam1_dowm, Cam1_right, Cam1_left, Cam_Record;
private String GotUrl;
private SurfaceView Cam1View;
private String Cam1_VideoUrl = "http://192.168.0.4:81/videostream.cgi?user=admin&password=admin";
private SurfaceHolder Cam1_Holder;
private Surface Cam1_surface = null;
private LibVLC Cam1_libVLC;
private Boolean recording;
private int mVideoHeight;
private int mVideoWidth;
private int mVideoVisibleHeight;
private int mVideoVisibleWidth;
private int mSarNum;
private int mSarDen;

@Nullable
@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.cam1, container, false);
    Cam1View = (SurfaceView) view.findViewById(R.id.Cam1_CamVIew);

    tv = (TextView) view.findViewById(R.id.textView2);

    Cam1_up = (Button) view.findViewById(R.id.Cam_up);
    Cam1_dowm = (Button) view.findViewById(R.id.Cam_down);
    Cam1_right = (Button) view.findViewById(R.id.Cam_right);
    Cam1_left = (Button) view.findViewById(R.id.Cam_left);
    Cam_Record = (Button) view.findViewById(R.id.Cam1_record);

    Cam1_up.setOnTouchListener(Camera_Fragment1.this);
    Cam1_dowm.setOnTouchListener(Camera_Fragment1.this);
    Cam1_right.setOnTouchListener(Camera_Fragment1.this);
    Cam1_left.setOnTouchListener(Camera_Fragment1.this);
    Cam_Record.setOnClickListener(this);
    GetUrl();
    Cam_Play();
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);



}

private void GetUrl() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
    GotUrl = prefs.getString("URL", null);
    tv.setText("" + GotUrl);
}

private void Cam_Play() {
    Cam1_Holder = Cam1View.getHolder();
    try {
        Cam1_libVLC = new LibVLC();
        Cam1_libVLC.setVout(LibVLC.VOUT_ANDROID_SURFACE);
        Cam1_libVLC.setHardwareAcceleration(LibVLC.HW_ACCELERATION_AUTOMATIC);
        Cam1_libVLC.init(getActivity().getApplicationContext());
    } catch (LibVlcException e) {
        e.printStackTrace();
    }
    Cam1_surface = Cam1_Holder.getSurface();
    Cam1_libVLC.attachSurface(Cam1_surface, this);
    Cam1_libVLC.playMRL(Cam1_VideoUrl);
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    Cam1_libVLC.stop();
}

@Override
public void setSurfaceLayout(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den) {
    if (width * height == 0)
        return;

    mVideoHeight = height;
    mVideoWidth = width;
    mVideoVisibleHeight = visible_height;
    mVideoVisibleWidth = visible_width;
    mSarNum = sar_num;
    mSarDen = sar_den;
}

@Override
public int configureSurface(Surface surface, int width, int height, int hal) {
    return -1;
}

@Override
public void eventHardwareAccelerationError() {

}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.cam_fragment1_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.Settings_fragment_1:
            Intent i = new Intent(getActivity(), Settings_Fragment_1.class);
            startActivity(i);
            break;
    }
    return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (v.getId()) {
        case R.id.Cam_up:
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    String upUrl = "up";
                    Task = new RotateCam();
                    Task.execute(upUrl);
                    break;
                case MotionEvent.ACTION_UP:
                    Task1 = new StopCam();
                    Task1.execute("stop");
                    break;
            }
            break;


        case R.id.Cam_down:
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    String DownUrl = "down";
                    Task = new RotateCam();
                    Task.execute(DownUrl);
                    break;
                case MotionEvent.ACTION_UP:
                    Task1 = new StopCam();
                    Task1.execute("stop");
                    break;
            }
            break;
        case R.id.Cam_right:
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    String RightUrl = "right";
                    Task = new RotateCam();
                    Task.execute(RightUrl);
                    break;
                case MotionEvent.ACTION_UP:
                    Task1 = new StopCam();
                    Task1.execute("stop");
                    break;
            }
            break;
        case R.id.Cam_left:
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    String LeftUrl = "left";
                    Task = new RotateCam();
                    Task.execute(LeftUrl);
                    break;
                case MotionEvent.ACTION_UP:
                    Task1 = new StopCam();
                    Task1.execute("stop");
                    break;
            }
            break;
    }
    return false;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.Cam1_record:
            if (recording) {
            } else {
            }
            break;
    }
}

private class RotateCam extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        adapter.CamRotation(params[0]);
        return null;
    }
}

private class StopCam extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        adapter.StopRotation();
        return null;
    }
}}

我已经使用Lib-Vlc预览视频到surfaceview,因为android不支持基于tcp的视频传输。

1 个答案:

答案 0 :(得分:0)