原文:c# 播放器 支持所有格式

---恢复内容开始---

直接上代码

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

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

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;

{
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);
}

}
//从网络位置构建一个视频文件
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导出函数

// 创建一个libvlc实例,它是引用计数的
[DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
private static extern IntPtr libvlc_new(int argc, IntPtr argv);

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

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

// 从视频来源(例如Url)构建一个libvlc_meida
[DllImport("libvlc", CallingConvention = CallingConvention.StdCall, 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)]
[SuppressUnmanagedCodeSecurity]
private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path);

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

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

// 将视频(libvlc_media)绑定到播放器上
[DllImport("libvlc", CallingConvention = CallingConvention.StdCall, 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)]
[SuppressUnmanagedCodeSecurity]
public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable);

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

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

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

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

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

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

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

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

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

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

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

//设置播放速度
[DllImport("libvlc", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
[SuppressUnmanagedCodeSecurity]
public static extern int libvlc_media_player_set_rate(IntPtr libvlc_media, float rate);
}

这个是将libvlc里面的api导入到c#开发环境中

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 = { "0", "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_location(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 * 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();
}

public int SetRate(float rate){
return LibVlcAPI.libvlc_media_player_set_rate(libvlc_media_player_,rate);

}
}

这个是调用 下面导入的函数重新封装

下面我讲解一个方法的调用过程 其他的可以到代码中学习

1. 这个获得视频文件的路径

2.将路径打包传递作为数组的一个元素传给

传递到自己封装的api

传递到api里面

改天给大家代码地址  有需要学习的或者自己可以下载一个完全安全的播放器 我明天会给大家分享地址

c# 播放器 支持所有格式的更多相关文章

  1. .net C# 网页播放器 支持多种格式 媒体播放器 播放器 代码

    .avi格式代码片断如下:<object id='video' width='400' height='200' border='0' classid='clsid:CFCDAA03-8BE4- ...

  2. WIN32下使用DirectSound接口的简单音频播放器(支持wav和mp3)

    刚好最近接触了一些DirectSound,就写了一个小程序练练手,可以用来添加播放基本的wav和mp3音频文件的播放器.界面只是简单的GDI,dxsdk只使用了DirectSound8相关的接口. D ...

  3. EasyPlayer RTSP Windows(with ActiveX/OCX插件)播放器支持H.265播放与抓图功能

    EasyPlayer作为业界一款比较优秀的RTSP播放器,一直深受用户的好评,经过了近3年的开发和迭代,从一开始的简单PC版本的RTSP播放功能,到如今支持PC(支持ocx插件).Android.iO ...

  4. delphi 插入 HTML代码 播放器

    Delphi在Webbrowser中插入 HTML/java script代码 使用方法将下面的代码赋值到1个记事本里保存,然后保存为xxx.htm就可以看到效果使用PasteHtml实现功能 的事件 ...

  5. Android音乐播放器的设计与实现

    目录 应用开发技术及开发平台介绍 应用需求分析 应用功能设计及其描述 应用UI展示 一.应用开发技术及平台介绍 ①开发技术: 本系统是采用面向对象的软件开发方法,基于Android studio开发平 ...

  6. (jsp/html)网页上嵌入播放器(常用播放器代码整理) http://www.jb51.net/article/37267.htm

    网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助   这个其实很简单,只要在HTML上添加以上代码就O ...

  7. EasyPlayer RTSP Windows播放器D3D,GDI的几种渲染方式的选择区别

    EasyPlayer-RTSP windows播放器支持D3D和GDI两种渲染方式,其中D3D支持格式如下: DISPLAY_FORMAT_YV12 DISPLAY_FORMAT_YUY2 DISPL ...

  8. Web播放器

    web视频播放器的使用及遇到的问题记录 TcPlayer播放器(腾讯Web超级播放器) https://cloud.tencent.com/document/product/881/20207 Ste ...

  9. 最简单的基于Flash的流媒体示例:网页播放器(HTTP,RTMP,HLS)

    http://blog.csdn.net/leixiaohua1020/article/details/43936415 ======================================= ...

随机推荐

  1. Visual Studio 2015使用EF6的CodeFirstFromDB模式操作Sqlite数据库时Provider问题

    传送门 什么是CodeFristFromDB 问题:查询数据是遇到 “/”应用程序中的服务器错误. No Entity Framework provider found for the ADO.NET ...

  2. Team Services and Team Foundation Server官方资料入口

    Team Foundation Server msdn 中文文档入口 Team Services or Team Foundation Server www.visualstudio.com 英文文档 ...

  3. Smart Framework

    Smart Framework:轻量级 Java Web 框架 发表于2年前(2013-09-01 08:39)   阅读(48569) | 评论(188) 544人收藏此文章, 我要收藏 赞83 阿 ...

  4. Codefoces 432 C. Prime Swaps

    哥德巴赫猜想: 任一大于2的偶数,都可表示成两个素数之和. 任一大于5的整数都可写成三个质数之和. 贪心取尽可能大的素数..... C. Prime Swaps time limit per test ...

  5. Cocos2d-x3.0 DrawNode吸取

    DrawNode正如它的绘图形状的节点,相互作用可以使将来更加灵活. DrawNode* DrawLayer::shape() { auto shape = DrawNode::create(); s ...

  6. Eclipse 发布到网站的附加产品的形式 Update Site

    Eclipse 发布到网站的附加产品的形式 Update Site 通过Update Site Project项目将自己做的插件产品公布到公网上,给客户或其它測试人员下载和应用,这样自己的插件就以网站 ...

  7. 表与表的关系把RD搞乱了,记一个Procedure中的bug

    就是6张表的关联查询,写了一个存储过程,使用4层for来处理 bug:最后一个for中,两张表的关联条件少了一个,结果数据多查了. 排查办法:使用dbms_output.printline('');每 ...

  8. 平均得分 【杭州电-HDOJ-2023】 附加题+详细说明

    /* 平均得分 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  9. Same binary weight (位运算)

    题目描述 The binary weight of a positive  integer is the number of 1's in its binary representation.for ...

  10. Ubuntu下一个openldapserver部署步骤

    1:安装zlib 下载zlib-1.2.3.tar.gz(或其它版本号) wget http://down1.chinaunix.net/distfiles/zlib-1.2.3.tar.gz # . ...