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);
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;
}
public int RecordStop() {
if(this.libvlc_media_player_ != IntPtr.Zero) {
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;
}