首页  编辑  

LibVlc录像和停止录像功能

Tags: /C#/多媒体/   Date Created:
videolan/libvlcsharp: Cross-platform .NET/Mono bindings for LibVLC (github.com)
libvlc的C#头文件,参考上面的链接。

C# 中如何实现用libvlc 录像和停止录像功能?
对于32位的libvlc库,里面有两个函数,可以启动和停止录像:
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern int libvlc_media_player_recorder_start(IntPtr libvlc_media_player, IntPtr filename);

[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern int libvlc_media_player_recorder_stop(IntPtr libvlc_media_player);
对于x64版本,libvlc去掉了上面的两个函数,可以用下面的方法:
public int RecordStart(string filePath) {
    if(this.libvlc_media_player_ != IntPtr.Zero) {
      IntPtr pMrl = IntPtr.Zero;
      try {
        string s;
        if(Environment.Is64BitProcess) s = ":sout=#duplicate{dst=display,dst=std{access=file,mux=mp4,dst=" + filePath + ".mp4}}";
        else s = filePath;
        byte[] bytes = Encoding.UTF8.GetBytes(s);
        pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
        Marshal.Copy(bytes, 0, pMrl, bytes.Length);
        Marshal.WriteByte(pMrl, bytes.Length, 0);
        // 64位SDK中,没有libvlc_media_player_recorder_start函数了
        if(Environment.Is64BitProcess) {
          IntPtr p_md = LibVlcAPI.libvlc_media_player_get_media(this.libvlc_media_player_);
          LibVlcAPI.libvlc_media_add_option(p_md, pMrl);
          LibVlcAPI.libvlc_media_player_set_media(libvlc_media_player_, p_md);
          LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);
        } else return LibVlcAPI.libvlc_media_player_recorder_start(this.libvlc_media_player_, pMrl);
      } finally {
        if(pMrl != IntPtr.Zero) {
          Marshal.FreeHGlobal(pMrl);
        }
      }
    }
    return 0;
  }

// 对于32位库,调用对应函数即可停止录像,对于64位库,重复调用一次设置录像,就可以关闭录像,这是一个on/off 选项开关来的。
public int RecordStop() {
  if(this.libvlc_media_player_ != IntPtr.Zero) {
    // 64位库,没有recorder_top函数了
    if(Environment.Is64BitProcess) {
      IntPtr p_md = LibVlcAPI.libvlc_media_player_get_media(this.libvlc_media_player_);
      LibVlcAPI.libvlc_media_player_set_media(libvlc_media_player_, p_md);
      LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);
    } else return LibVlcAPI.libvlc_media_player_recorder_stop(this.libvlc_media_player_);
  }
  return 0;
}