C# 视频播放器 采用迅雷Aplayer 绿色使用无需安装 支持VS2022

引擎介绍

APlayer 媒体播放引擎是迅雷公司从 2009 年开始开发的通用音视频媒体文件播放内核。

迅雷看看播放器和迅雷影音就是使用 APlayer 作为播放内核。

本质上APlayer播放引擎是一个ActiveX控件,可以被本地其他外部程序嵌入调用,也可以直接被网页嵌入,类似于(Adobe Flash Player)。APlayer不包含界面,但另一个依赖于APlayer的ActiveX控件APlayerUI可以提供丰富的界面元素(播放控制条,Flash广告等),APlayerUI 也被包含在 APlayerSDK 中。

官方地址:迅雷APlayer媒体播放引擎-首页

重点

本文章与目前网上的均不一致,请注意查看,完全可以做到绿色版。

本次开发使用的是APlayerCaller.dll插件  由原来的COM的方式改为用插件的方式。完美支持VS2022以上版本。

上部分代码

1.引入DLL

//创建视频窗口
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_Create", SetLastError = true)]
        public static extern int APlayer_Create
        (
        int hParent,
        int x,
        int y,
        int nWidth,
        int nHeight,
        IntPtr OnMessage,
        IntPtr OnStateChanged,
        IntPtr OnOpenSuccess,
        IntPtr OnSeekCompleted,
        IntPtr OnBuffer,
        IntPtr OnVideoSizeChanged,
        IntPtr ondownloadcodec,
        IntPtr OnEvent
        );
        //打开 指定 地址的视频
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_OpenW", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool APlayer_OpenW(int hand, string url);

        //开始播放
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_Play", SetLastError = true)]
        public static extern bool APlayer_Play(int hand);

        //设置Logo
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_SetCustomLogo", SetLastError = true)]
        public static extern bool APlayer_SetCustomLogo(int hand, int logo);

        //设置参数
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_SetConfigA", SetLastError = true)]
        public static extern int APlayer_SetConfigA(int hand, int configid, string val);

        //获取版本号
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_GetVersion", SetLastError = true)]
        public static extern string APlayer_GetVersion(int hand);

        //获取视频宽度
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_GetVideoWidth", SetLastError = true)]
        public static extern int APlayer_GetVideoWidth(int hand);

        //获取视频高度
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_GetVideoHeight", SetLastError = true)]
        public static extern int APlayer_GetVideoHeight(int hand);

        //销毁
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_Destroy", SetLastError = true)]
        public static extern bool APlayer_Destroy(int hand);

        //关闭
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_Close", SetLastError = true)]
        public static extern bool APlayer_Close(int hand);

        //暂停
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_Pause", SetLastError = true)]
        public static extern bool APlayer_Pause(int hand);

        //获取播放器的当前状态,返回整数型状态代码。0:准备就绪,1:正在打开,2:正在暂停,3:暂停中,4:正在开始播放,5:播放中,6:正在开始关闭。
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_GetState", SetLastError = true)]
        public static extern int APlayer_GetState(int hand);

        //获取时长
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_GetDuration", SetLastError = true)]
        public static extern int APlayer_GetDuration(int hand);

        //获取当前位置
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_GetPosition", SetLastError = true)]
        public static extern int APlayer_GetPosition(int hand);

        //设置当前位置
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_SetPosition", SetLastError = true)]
        public static extern int APlayer_SetPosition(int hand, int position);

        //获取声音大小
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_GetVolume", SetLastError = true)]
        public static extern int APlayer_GetVolume(int hand);

        //设置当前的播放音量,取值范围为 0 - 1000,100时就是正常的满音量,取值1000为十倍音量。
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_SetVolume", SetLastError = true)]
        public static extern int APlayer_SetVolume(int hand, int volume);

        //判断是否处于设置播放进度过程中。当调用[设置当前播放位置]后,播放器执行设置操作,这个过程需要花费一定的时间,所以提供了这个方法来供调用者查询当前是否处于设置过程中。0:不在设置过程中,1:在设置过程中。
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_IsSeeking", SetLastError = true)]
        public static extern int APlayer_IsSeeking(int hand);

        //用于获取APlayer播放器在播放网络文件时的数据缓冲进度。-1:不在缓冲过程中,0-99:缓冲进度。
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_GetBufferProgress", SetLastError = true)]
        public static extern int APlayer_GetBufferProgress(int hand);

        //获取设置
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_GetConfigA", SetLastError = true)]
        public static extern int APlayer_GetConfigA(int hand, int configid);

        //获取窗口句柄
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "APlayer_GetWindow", SetLastError = true)]
        public static extern int APlayer_GetWindow(int hand);

        //载入一个位图、图标或指针 执行成功则返回对象的一个句柄;零表示失败
        [DllImportAttribute("APlayerCaller.dll", EntryPoint = "LoadImage", SetLastError = true)]
        public static extern int LoadImage(
            int hand, //要从其中载入图象的DLL或应用程序模块或实例句柄。零表示装载一幅固有图象
            string flag, //欲载入图象的名字。如指定了hInst,就用这个参数指定资源或资源的标志符(标志符是一个长整数)。如hInst为空,而且已指定了LR_LOADFROMFILE,那么这个参数代表文件名(位图、图标或指针文件)。如果是个Long型值,这个参数就代表固有位图、图标或指针的编号
            int type, //下述常数之一,指定了欲载入的图象类型:IMAGE_BITMAP, IMAGE_CURSOR, IMAGE_ICON
            int width, // 要求的图象宽度和高度。图象会根据情况自动伸缩。如设为零,表示用图象的默认大小
            int height, //同上
            int zh //下述常数的任意组合,它们都在api32.txt文件中得到了定义:;LR_DEFAULTCOLOR:以常规方式载入图象;LR_LOADREALSIZE:不对图象进行缩放处理。忽略n1和n2的设置;LR_CREATEDIBSECTION:如果指定了IMAGE_BITMAP,就返回DIBSection的句柄,而不是位图的句柄;LR_DEFAULTSIZE:如果n1和n2为零,就使用由系统定义的图象默认大小,而不是图象本身定义的大小;LR_LOADFROMFILE:如hInst为零,lpsz就代表要载入适当类型的一个文件的名字,仅适用于Win95;LR_LOADMAP3DCOLORS:将图象中的深灰、灰、以及浅灰像素都替换成COLOR_3DSHADOW,COLOR_3DFACE以及COLOR_3DLIGHT的当前设置;LR_LOADTRANSPARENT:与图象中第一个像素相符的所有像素都由系统替换;LR_MONOCHROME:将图象转换成单色;LR_SHARED:将图象作为一个共享资源载入。在NT 4.0中装载固有资源时要用到这个设置;

            );

2.初始化

private bool initVideo()
        {
            IntPtr POnMessage = Marshal.GetFunctionPointerForDelegate(new fun3(OnMessage));
            IntPtr POnStateChanged = Marshal.GetFunctionPointerForDelegate(new fun2(OnStateChanged));
            IntPtr POnOpenSuccess = Marshal.GetFunctionPointerForDelegate(new fun0(OnOpenSucceeded));
            IntPtr POnSeekCompleted = Marshal.GetFunctionPointerForDelegate(new fun1(OnSeekCompleted));
            IntPtr POnBuffer = Marshal.GetFunctionPointerForDelegate(new fun1(OnBuffer));
            IntPtr POnVideoSizeChanged = Marshal.GetFunctionPointerForDelegate(new fun0(OnVideoSizeChanged));
            IntPtr POnDownloadCodec = Marshal.GetFunctionPointerForDelegate(new funstr(OnDownloadCodec));
            IntPtr POnEvent = Marshal.GetFunctionPointerForDelegate(new fun2(OnEvent));
            if (HAplayer == 0)
            {
                //创建代码
                HAplayer = APlayer_Create(panel1.Handle.ToInt32(), 0, 0, panel1.Width, panel1.Height, POnMessage, POnStateChanged, POnOpenSuccess, POnSeekCompleted, POnBuffer, POnVideoSizeChanged, POnDownloadCodec, POnEvent);
                //int HAplayer = APlayer_Create(panel1.Handle.ToInt32(), 0, 0, 200, 200, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                //MessageBox.Show("句柄是:" + HAplayer.ToString());
                //APlayer_SetCustomLogo(HAplayer, -1);
                if (HAplayer > 0)
                {
                    VolumeTrackBar.Value = APlayer_GetVolume(HAplayer);
                    return true;
                }
            }
            return false;
        }

3.消息回调

void OnMessage(int l1, int l2, int l3)
        {
            //方法体
            Debug.WriteLine(l1 + "=OnMessage=" + l2);
        }
        void OnStateChanged(int l1, int l2)
        {
            Debug.WriteLine(l1 + "=OnStateChanged=" + l2);
            if (l2 == 6)
            {
                PlayButton.Text = "播放";
            }
        }
        void OnOpenSucceeded()
        {
            Debug.WriteLine("=OnOpenSucceeded=");
        }
        void OnSeekCompleted(int l1)
        {
            Debug.WriteLine(l1 + "=OnSeekCompleted=");
        }
        void OnBuffer(int l1)
        {
            Debug.WriteLine(l1 + "=OnBuffer=");
        }
        void OnVideoSizeChanged()
        {
            Debug.WriteLine("=OnVideoSizeChanged=");
        }
        void OnEvent(int nEventCode, int nEventParam)
        {
            Debug.WriteLine(nEventCode + "=OnEvent=" + nEventParam);
        }
        void OnDownloadCodec(string l1)
        {
            Debug.WriteLine(l1 + "==OnDownloadCodec");

        }

效果展示

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值