VLC播放器
为了将多个视频放在一个窗口,最开始想用的是windows media player ,6个视频,把整个电脑卡得不动了(显卡太弱,是多输出口的,没法换),于是又想把视频压缩成一个,网上的大部分软件要收费,还是研究播放,就弄了VLC。
代码网上基本都一样的,但我调试了很久才走通。
主要是在调用,以及库的问题上,总结成3点,
一、关于库的问题,不要纠结,直接在网上搜索VLC media player 下载,国内站的也可以,(官网有可能要翻墙)
安装后,可以看到三个需要的,plugins目录 ,libvlc.dll,libvlccore.dll ,放到你的debug目录
二、关于调用
string p = System.Environment.CurrentDirectory + "\\plugins\\";
VlcPlayer v = new VlcPlayer(p);
v.SetRenderWindow(pictureBox1.Handle.ToInt32());
v.PlayFile(@"1.mp4");
类是下面代码封装好的,大概就是这样
三、如果出现其它一些状况,请新建一个工程,不要在原工程上纠结。
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Security;
- using System.Text;
- namespace WindowsFormsApplication2
- {
- 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 Play()
- {
- if (libvlc_media_player_ != IntPtr.Zero)
- {
- LibVlcAPI.libvlc_media_player_play(libvlc_media_player_);
- // 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 void FastForward()
- // {
- // if (libvlc_media_player_ != IntPtr.Zero)
- // {
- // LibVlcAPI.libvlc_media_player_fastforward(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导出函数
- // 创建一个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);
- /// <summary>
- ///
- /// </summary>
- /// <param name="libvlc_mediaplayer"></param>
- //[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
- //[SuppressUnmanagedCodeSecurity]
- // public static extern void libvlc_media_player_fastforward(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);
- }
- }
VLC播放器的更多相关文章
- 使用vlc播放器做rtsp流媒体服务器
可参考: 使用vlc播放器播放rtsp视频 web网页中使用vlc插件播放相机rtsp流视频 使用vlc进行二次开发做自己的播放器 首先需要安装vlc播放器,下载及安装步骤略 使用vlc播放器做rts ...
- 使用vlc播放器播放rtsp流视频
可参考: 使用vlc播放器做rtsp服务器 web网页中使用vlc插件播放相机rtsp流视频 使用vlc进行二次开发做自己的播放器 首先需要安装vlc播放器,下载及安装步骤略 使用vlc播放器播放rt ...
- Android VLC播放器二次开发1——程序结构分析
最近因为一个新项目需要一个多媒体播放器,所以需要做个视频.音频.图片方面的播放器.也查阅了不少这方面的资料,如果要从头做一个播放器工作量太大了,而且难度也很大.所以最后选择了VLC作为基础,进行二次开 ...
- centos7安装VLC播放器
centos7安装VLC播放器 1.安装eple 下载地址:https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noar ...
- Centos 上使用mmsh协议听猫扑网络电台 VLC播放器
Centos 上使用mmsh协议听猫扑网络电台 VLC播放器 安装CentOS已经有一段时间了,但是由于在Linux下除了学习,其他是事情都干不了.今天想闲来无事开了CentOS就想听一下歌,突然想起 ...
- Ubuntu安装VLC播放器
Ubuntu安装VLC官方介绍:http://www.videolan.org/vlc/download-ubuntu.html sudo apt-get update sudo apt-get in ...
- Android VLC播放器二次开发3——音乐播放(歌曲列表+歌词同步滚动)
今天讲一下对VLC播放器音频播放功能进行二次开发,讲解如何改造音乐播放相关功能.最近一直在忙着优化视频解码部分代码,因为我的视频播放器需要在一台主频比较低的机器上跑(800M主频),所以视频解码能力受 ...
- iOS实现基于VLC播放器的封装效果
前言: 在一些特定场景下,我们获取到的音视频,由于格式比较特殊,用avplayer等播放器是无法播放的,此时,我们可以借助强大的VLC播放器来处理. 原理这里不再赘述,下面我们讲一下如何添加VLC播放 ...
- Android VLC播放器二次开发2——CPU类型检查+界面初始化
上一篇讲了VLC整个程序的模块划分和界面主要使用的技术,今天分析一下VLC程序初始化过程,主要是初始化界面.加载解码库的操作.今天主要分析一下org.videolan.vlc.gui.MainActi ...
- 网页IE轻松调用VLC播放器实现监控(组件+方法大全)【转】
公司突发奇想,要把刚买回来的网络监控机用自己内部网站在线监控. 作为网站的开发员,我接下了这个任务. 网络上有很多资料参与,但是都不全都不尽人意,最后经过多次的不同关键字的查找和测试,总算让我成功了. ...
随机推荐
- Leet Code 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- websocket的属性readyState
webSocket的readyState属性用来定义连接状态,该属性的值有下面几种: 0 :对应常量CONNECTING (numeric value 0), 正在建立连接连接,还没有完成.The c ...
- effective_java 第34条:用接口模拟可伸缩的枚举
例如: /** * 加减乘除枚举 * Created by yulinfeng on 8/20/17. */ public enum Operation { PLUS { double apply(d ...
- spring的历史和设计科学
Spring的起源 要谈Spring的历史,就要先谈J2EE.J2EE应用程序的广泛实现是在1999年和2000年开始的,它的出现带来了诸如事务管理之类的核心中间层概念的标准化,但是在实践中并没有获得 ...
- L347
Even Educators Believe These 7 Myths About Learning1. Individuals learn better when they receive inf ...
- Spring Boot 与 swagger 结合
. 配置pom.xml 2. 更改端口号, 在src/main/resources 下面添加一个application.yml文件. 3. 添加一个ModelCase entity. 4. 添加一个i ...
- Mysql慢查询-Mysql慢查询详细教程
一.简介开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能.二.参数说明slow_query_log 慢查询开启状态slow_quer ...
- 泊爷带你学go -- 经典的继承与接口 简直吊炸天 !
package main import ( "fmt" ) type TeamBase struct { m_TeamId uint64 m_Rid uint32 m_RoomRu ...
- Wisdom RESTClient 使用教程
Wisdom RESTClient 一款自动化测试REST API的工具,它可以自动化测试RESTful API并生成精美的测试报告,同时基于测试过的历史API,可以生成精美的RESTful API文 ...
- 免费的DDos网络测试工具集合
今天晚上看YT上的hulk VS monster Dogs 然后想看电影资源,给我推送了hulk这款工具了解下,发现了一些东西,收藏下 1.卢瓦(LOIC) (Low Orbit Ion Canon) ...