由于项目需要,要回放一些视频资料,经过多方调研,最后决定使用vlc方案。好处优点请大家自行百度谷歌,这里就不废话了。

开发环境:

工具: VS2010 .net4.0

程序类型:WindowsForm

语言:C#

实现功能:读取视频文件、为后续开发做铺垫。

vlc网上资料很多,但能用过编译的少之又少,经过一天的尝试终于整理成可以运行的vlcSDK。先上图,有图有真相。

.如上图,右侧两图为使用vlc开发的视频播放效果。由于该项目不变透露过多信息。所以针对vlc特意编写一个播放器软件供大家学习研讨,也是通过研究后才正式写的项目程序。稍后会把视软件上传CSDN。

视频播放软件包括工具栏和播放窗口,工具栏包括按钮和表示视频播放时间的进度条。播放器如下图所示:

主要做如下介绍:

CLV2.2 SDK
包括内容:
1、libvlc.dll  
2、libvlc.dll
3、plugins文件夹
4、VlcPlayer.cs

前三个为clvSDK文件,VlcPlayer.cs为C#编写的调用SDK的类和函数

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Collections.Generic;
using System.Text;

namespace vlc.net
{
    class VlcPlayer
    {
        private IntPtr libvlc_instance_;
        private IntPtr libvlc_media_player_;

private double duration_;

public VlcPlayer(string pluginPath)
        {
            string plugin_arg = "--plugin-path=" + pluginPath;
            string[] arguments = { "-I", "dummy", "--ignore-config", "--no-video-title", plugin_arg };
            libvlc_instance_ = LibVlcAPI.libvlc_new(arguments);

libvlc_media_player_ = LibVlcAPI.libvlc_media_player_new(libvlc_instance_);
        }

public void SetRenderWindow(int wndHandle)
        {
            if (libvlc_instance_ != IntPtr.Zero && wndHandle != 0)
            {
                LibVlcAPI.libvlc_media_player_set_hwnd(libvlc_media_player_, wndHandle);
            }
        }

public void PlayFile(string filePath)
        {
            IntPtr libvlc_media = LibVlcAPI.libvlc_media_new_path(libvlc_instance_, filePath);
            if (libvlc_media != IntPtr.Zero)
            {
                LibVlcAPI.libvlc_media_parse(libvlc_media);
                //   duration_ = LibVlcAPI.libvlc_media_get_duration(libvlc_media) / 1000.0;
                LibVlcAPI.libvlc_media_player_set_media(libvlc_media_player_, libvlc_media);
                LibVlcAPI.libvlc_media_release(libvlc_media);
                LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);

duration_ = LibVlcAPI.libvlc_media_get_duration(libvlc_media) / 1000.0;

if (duration_ == 0)
                {
                    duration_ = 597;
                }

}
        }
        public void Play()
        {
            LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);
        }

public void Pause()
        {
            if (libvlc_media_player_ != IntPtr.Zero)
            {
                LibVlcAPI.libvlc_media_player_pause(libvlc_media_player_);
            }
        }

public void Stop()
        {
            if (libvlc_media_player_ != IntPtr.Zero)
            {
                LibVlcAPI.libvlc_media_player_stop(libvlc_media_player_);
            }
        }

public double GetPlayTime()
        {
            return LibVlcAPI.libvlc_media_player_get_time(libvlc_media_player_) / 1000.0;
        }

public void SetPlayTime(double seekTime)
        {
            LibVlcAPI.libvlc_media_player_set_time(libvlc_media_player_, (Int64)(seekTime * 1000));
        }

public int GetVolume()
        {
            return LibVlcAPI.libvlc_audio_get_volume(libvlc_media_player_);
        }

public void SetVolume(int volume)
        {
            LibVlcAPI.libvlc_audio_set_volume(libvlc_media_player_, volume);
        }

public void SetFullScreen(bool istrue)
        {
            LibVlcAPI.libvlc_set_fullscreen(libvlc_media_player_, istrue ? 1 : 0);
        }

public double Duration()
        {
            return duration_;
        }

public string Version()
        {
            return LibVlcAPI.libvlc_get_version();
        }
    }

internal static class LibVlcAPI
    {
        internal struct PointerToArrayOfPointerHelper
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
            public IntPtr[] pointers;
        }

public static IntPtr libvlc_new(string[] arguments)
        {
            PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper();
            argv.pointers = new IntPtr[11];

for (int i = 0; i < arguments.Length; i++)
            {
                argv.pointers[i] = Marshal.StringToHGlobalAnsi(arguments[i]);
            }

IntPtr argvPtr = IntPtr.Zero;
            try
            {
                int size = Marshal.SizeOf(typeof(PointerToArrayOfPointerHelper));
                argvPtr = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(argv, argvPtr, false);

return libvlc_new(arguments.Length, argvPtr);
            }
            finally
            {
                for (int i = 0; i < arguments.Length + 1; i++)
                {
                    if (argv.pointers[i] != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(argv.pointers[i]);
                    }
                }
                if (argvPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(argvPtr);
                }
            }
        }

public static IntPtr libvlc_media_new_path(IntPtr libvlc_instance, string path)
        {
            IntPtr pMrl = IntPtr.Zero;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(path);
                pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
                Marshal.Copy(bytes, 0, pMrl, bytes.Length);
                Marshal.WriteByte(pMrl, bytes.Length, 0);
                return libvlc_media_new_path(libvlc_instance, pMrl);
            }
            finally
            {
                if (pMrl != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pMrl);
                }
            }
        }

public static IntPtr libvlc_media_new_location(IntPtr libvlc_instance, string path)
        {
            IntPtr pMrl = IntPtr.Zero;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(path);
                pMrl = Marshal.AllocHGlobal(bytes.Length + 1);
                Marshal.Copy(bytes, 0, pMrl, bytes.Length);
                Marshal.WriteByte(pMrl, bytes.Length, 0);
                return libvlc_media_new_path(libvlc_instance, pMrl);
            }
            finally
            {
                if (pMrl != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pMrl);
                }
            }
        }

// ----------------------------------------------------------------------------------------
        // 以下是libvlc.dll导出函数
        //       [DllImport("xx.dll", EntryPoint=“xxFunction”, CallingConvention = CallingConvention.Cdecl)]
        // 创建一个libvlc实例,它是引用计数的
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_new(int argc, IntPtr argv);

// 释放libvlc实例
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_release", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_release(IntPtr libvlc_instance);

//        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_get_version", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern String libvlc_get_version();

// 从视频来源(例如Url)构建一个libvlc_meida
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_new_location", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path);

// 从本地文件路径构建一个libvlc_media
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_new_path", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path);

//        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_release", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_release(IntPtr libvlc_media_inst);

// 创建libvlc_media_player(播放核心)
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_player_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance);

// 将视频(libvlc_media)绑定到播放器上
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_player_set_media", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media);

// 设置图像输出的窗口
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_player_set_hwnd", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int64 drawable);

//        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_player_play", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer);

//        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_player_pause", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer);

//        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_player_stop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer);

// 解析视频资源的媒体信息(如时长等)
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_parse", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_parse(IntPtr libvlc_media);

// 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效)
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_get_duration", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media);

// 当前播放的时间
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_player_get_time", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer);

// 设置播放位置(拖动)
        //       [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_player_set_time", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time);

//        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_media_player_release", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer);

// 获取和设置音量
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_audio_get_volume", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player);

//        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_audio_set_volume", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume);

// 设置全屏
        //        [DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        [DllImport("libvlc.dll", EntryPoint = "libvlc_set_fullscreen", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen);
    }
}

代码不做过多介绍,已经将源码上传MSDN,

链接:

播放器源码(包括SDK):https://download.csdn.net/download/w267309080/11967649

SDK:https://download.csdn.net/download/w267309080/11967641

C#下基于vlc的视频播放功能开发相关推荐

  1. 嵌入式Linux下基于FFmpeg的视频硬件编解码

    嵌入式Linux下基于FFmpeg的视频硬件编解码[图] http://www.c114.net ( 2012/3/1 15:41 ) 摘要: 对FFmpeg多媒体解决方案中的视频编解码流程进行研究. ...

  2. 基于V4L2的视频驱动开发(2) 华清远见 刘洪涛

    基于V4L2的视频驱动开发(2) 华清远见 刘洪涛 三.            V4L2 API及数据结构 V4L2是V4L的升级版本,为linux下视频设备程序提供了一套接口规范.包括一套数据结构和 ...

  3. Linux下基于qt的视频监控系统

    目录 一.原始需求 二.环境安装 2.1 qt安装 2.2 opencv安装 三.系统设计 3.1. 整体流程设计 3.2 .数据传输交互流程 3.3 .数据库设计 四.关键代码 4.1.如何实现通信 ...

  4. 基于V4L2的视频驱动开发

    基于V4L2的视频驱动开发 编写基于V4L2视频驱动主要涉及到以下几个知识点: ●摄像头方面的知识 要了解选用的摄像头的特性,包括访问控制方法.各种参数的配置方法.信号输出类型等. ●Camera解码 ...

  5. C++:Windows环境下基于Eclipse配置C/C++开发环境

    C++:Windows环境下基于Eclipse配置C/C++开发环境 目录 Windows下的MinGW下载.安装和配置 1.MinGW下载 2.MinGW安装与配置 3.基于Eclipse配置 Wi ...

  6. 基于VLC的播放器开发

    VLC的C++封装 因为工作需要,研究了一段时间的播放器开发,如果从头开始做,可以学习下FFmpeg(http://www.ffmpeg.org/),很多播放器都是基于FFmpeg开发的,但是这样工作 ...

  7. 嵌入式Linux下基于FFmpeg的视频硬件编解码[图]

    转自:http://tech.c114.net/167/a674033.html 摘要: 对FFmpeg多媒体解决方案中的视频编解码流程进行研究.结合对S3C6410处理器视频硬件编解码方法的分析,阐 ...

  8. 基于V4L2的视频驱动开发(1)

    编写基于V4L2视频驱动主要涉及到以下几个知识点: l         摄像头方面的知识 要了解选用的摄像头的特性,包括访问控制方法.各种参数的配置方法.信号输出类型等. l         Came ...

  9. 基于V4L2的视频驱动开发(1)---Camera

    编写基于V4L2视频驱动主要涉及到以下几个知识点: ●    摄像头方面的知识                 要了解选用的摄像头的特性,包括访问控制方法.各种参数的配置方法.信号输出类型等. ●   ...

最新文章

  1. 数据挖掘技术在出行体验上的应用!
  2. SAP S/4HANA使用ABAP获得生产订单的状态
  3. 【机器视觉】 HDevelop语言基础(一)-基本类型和常量
  4. 【转】ABP源码分析三十九:ABP.Hangfire
  5. 【干货】推荐系统中的机器学习算法与评估实战
  6. HTTP协议中,除了GET和POST还有什么请求?
  7. 【分享】终端命令工具 自动生成vue组件文件以及修改router.js
  8. 使用java程序读取配置文件中的相关属性值-asp.net关注
  9. int和Integer有什么区别(转)
  10. Android获取所在城市坐标及城市信息(逆地理位置编码)
  11. 基于胜任力模型的项目经理岗位培训需求分析研究
  12. dell-xps-8930 台式机双硬盘 双系统安装 win10+Ubuntu
  13. 升级Win11后,语言栏不在任务栏上
  14. 大话李白flash系列(在线看,全)
  15. 南京财经的计算机科学与技术,2021年南京财经大学计算机科学与技术(081200)考研专业目录_硕士研究生考试范围 - 学途吧...
  16. 苹果开放降级_苹果官方为什么不开放 iOS 降级验证通道?
  17. 计算机文件右击怎么显示打开方式,电脑右键菜单中没有打开方式怎么办|电脑恢复打开方式选项到右键菜单中的方法...
  18. 智能手机低价成潮,vivo为何执念高端?
  19. c++实现地图控制扫雷(界面优化版
  20. 2015-4-11更新的pdf

热门文章

  1. echarts饼状图隐藏标示线和标示文字
  2. 数据库相关中间件全家桶
  3. CDR-jetson-docker镜像使用及测试教程
  4. linux驱动 — 字符设备驱动模板
  5. 高精度GPS北斗卫星授时仪器(校时器)技术参数详解
  6. 安装Bioperl最基本模块Bio::SeqIO
  7. 王汕7.10期货原油、外汇黄金指导、黄金走势分析及下周操作建议
  8. C语言的预处理器无法先展开宏再拼接符号?可以!
  9. 九齐新型单片机NY8A051F
  10. 2021年R1快开门式压力容器操作最新解析及R1快开门式压力容器操作模拟考试题