C#简易播放器(基于开源VLC)
可见光通信技术(Visible Light Communication,VLC)是指利用可见光波段的光作为信息载体,不使用光纤等有线信道的传输介质,而在空气中直接传输光信号的通信方式。LED可见光通信是基于可见光发光二极管(Light Emitting Diode,LED)比荧光灯和白炽灯切换速度快的特点,利用配备LED的室内外大型显示屏、照明设备、信号器和汽车前尾灯等发出的用肉眼观察不到的高速调制光波信号来对信息调制和传输,然后利用光电二极管等光电转换器件接收光载波信号并获得信息。无论应用于室内还是室外的可见光LED通信系统,在其物理实现上均分为光信号发射和光信号接收两部分。光信号发射部分包括:将信号源信号转换成便于光信道传输的电信号的输入和处理电路、将电信号变化调制成光载波强度变化的LED可见光驱动调制电路。光信号接收部分包括:能对信号光源实现最佳接收的光学系统、将光信号还原成电信号的光电探测器和前置放大电路、将电信号转换成可被终端识别的信号处理和输出电路。
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Collections.Generic;
using System.Text; namespace DXApplication1
{
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 != )
{
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_);
}
} 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 * ));
} 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 ? : );
} public double Duration()
{
return duration_;
} public string Version()
{
return LibVlcAPI.libvlc_get_version();
}
} internal static class LibVlcAPI
{
internal struct PointerToArrayOfPointerHelper
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
public IntPtr[] pointers;
} public static IntPtr libvlc_new(string[] arguments)
{
PointerToArrayOfPointerHelper argv = new PointerToArrayOfPointerHelper();
argv.pointers = new IntPtr[]; for (int i = ; 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 = ; i < arguments.Length + ; 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 + );
Marshal.Copy(bytes, , pMrl, bytes.Length);
Marshal.WriteByte(pMrl, bytes.Length, );
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 + );
Marshal.Copy(bytes, , pMrl, bytes.Length);
Marshal.WriteByte(pMrl, bytes.Length, );
return libvlc_media_new_path(libvlc_instance, pMrl);
}
finally
{
if (pMrl != IntPtr.Zero)
{
Marshal.FreeHGlobal(pMrl);
}
}
} // ----------------------------------------------------------------------------------------
// 以下是libvlc.dll导出函数
// DllImport的CallingConvention的属性,默认值是CallingCovention.Stdcall, 此处更改成Cdecl
// 创建一个libvlc实例,它是引用计数的
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
private static extern IntPtr libvlc_new(int argc, IntPtr argv); // 释放libvlc实例
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_release(IntPtr libvlc_instance); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern String libvlc_get_version(); // 从视频来源(例如Url)构建一个libvlc_meida
[DllImport("libvlc", 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.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_media_release(IntPtr libvlc_media_inst); // 创建libvlc_media_player(播放核心)
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance); // 将视频(libvlc_media)绑定到播放器上
[DllImport("libvlc", 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.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer); // 解析视频资源的媒体信息(如时长等)
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_media_parse(IntPtr libvlc_media); // 返回视频的时长(必须先调用libvlc_media_parse之后,该函数才会生效)
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media); // 当前播放的时间
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer); // 设置播放位置(拖动)
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer); // 获取和设置音量
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player); [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume); // 设置全屏
[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen);
}
}

C#简易播放器(基于开源VLC)的更多相关文章
- FFmpeg简易播放器的实现-视频播放
本文为作者原创:https://www.cnblogs.com/leisure_chn/p/10047035.html,转载请注明出处 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...
- FFmpeg简易播放器的实现-音视频同步
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10284653.html 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...
- FFmpeg简易播放器的实现-音视频播放
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10235926.html 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...
- FFmpeg简易播放器的实现-音频播放
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10068490.html 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...
- FFmpeg简易播放器的实现-最简版
本文为作者原创:https://www.cnblogs.com/leisure_chn/p/10040202.html,转载请注明出处 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...
- <Win32_17>集音频和视频播放功能于一身的简易播放器
前段时间,在学习中科院杨老师的教学视频时,他说了一句话: "我很反对百八十行的教学程序,要来就来一个完整的程序" 对此,我很是赞同.所谓真刀真枪的做了,你才会发现其中的奥秘——然而 ...
- 仿迅雷播放器教程 -- 封装VLC (5)
虽然上个教程中10多行代码便做出了一个播放器,但如果加上快进快退等功能的话,代码都会挤在一团,阅读性很差,所以这个版本将对VLC进行封装,由于第一个教程已经进行了ffmpeg的封装,所以这里将 ...
- Opencv实现简易播放器
实现了在MFC中显示图片,再要显示一个视频就是轻而易举的事了,本篇介绍使用Opencv制作一个简易的播放器,实现打开文件.暂停.继续播放.再次播放和总\当前帧数显示功能. 首先还是先看一下界面效果: ...
- winform下的简易播放器
编写这个播放器,遇到很多问题,比如目前只实现了wav音频文件的播放,而对于这个图中中间所标注的按钮 不能实现让其暂停的功能,同时当点击的时候,让其文本变为"▷",对于这部分功能不知 ...
随机推荐
- duilib进阶教程 -- 扩展duilib的消息 (11)
duilib并没有提供双击和右键消息,所以需要我们自行扩展,这里以添加双击消息为例, 在UIDefine.h里,我们只看到了DUI_MSGTYPE_CLICK消息,却没有看到双击消息,因此需要在这里添 ...
- EntityFramework 4/5/6 中执行自定义SQL语句
参考:http://www.cnblogs.com/chengxiaohui/articles/2092001.html 在EF4(.NET 4)中,我们有了全新的API:ObjectContext ...
- .NET Remoting学习笔记(二)激活方式
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科 ♂风车车.Net 激活方式概念 在 ...
- Java集合——题目
第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...
- Leetcode 1 Two Sum STL
题意:给定一个目标值target,找到两个数的和为target,返回这两个数的下标 用map记录每个数的下标,并用于查找 class Solution { public: vector<int& ...
- 家中Win7 安装 Maven的步骤及参考文章
Maven 实战系列之在Windows上安装Maven cy163注:Path中的值: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32 ...
- Python学习笔记(1):列表元组结构
Python的列表元组功能强大,令人印象深刻.一是非常灵活,二是便于集体操作.特别是以元组作为列表项的结构,和数据访问的结果能够对应起来,和习惯的二维表理解上也一致,有很多的用途. 以学习笔记(3)中 ...
- JVM常见的七种垃圾收集器的简单比较
1.Serial收集器曾经是虚拟机新生代收集的唯一选择,是一个单线程的收集器,在进行收集垃圾时,必须stop the world,它是虚拟机运行在Client模式下的默认新生代收集器. 2.Seria ...
- Docker实践(2)—虚拟网络
1 docker(container)的虚拟网络 docker的虚拟网络结构: host创建一个虚拟bridge,每个container对应一个虚拟网络设备(TAP设备),与bridge一起构成一个虚 ...
- 用node开发repl应用
前言 每次看到一些库npm -g install xx然后,执行xx就可以跑起来,这不就是一个shell工具了吗,那么我不就可以不用学习shell语法,直接用js写命令行脚本了吗! 什么是REPL应用 ...