天天看點

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)

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執行個體

public static extern void libvlc_release(IntPtr libvlc_instance);

public static extern String libvlc_get_version();

// 從視訊來源(例如Url)建構一個libvlc_meida

private static extern IntPtr libvlc_media_new_location(IntPtr libvlc_instance, IntPtr path);

// 從本地檔案路徑建構一個libvlc_media

private static extern IntPtr libvlc_media_new_path(IntPtr libvlc_instance, IntPtr path);

public static extern void libvlc_media_release(IntPtr libvlc_media_inst);

// 建立libvlc_media_player(播放核心)

public static extern IntPtr libvlc_media_player_new(IntPtr libvlc_instance);

// 将視訊(libvlc_media)綁定到播放器上

public static extern void libvlc_media_player_set_media(IntPtr libvlc_media_player, IntPtr libvlc_media);

// 設定圖像輸出的視窗

public static extern void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, Int32 drawable);

public static extern void libvlc_media_player_play(IntPtr libvlc_mediaplayer);

public static extern void libvlc_media_player_pause(IntPtr libvlc_mediaplayer);

public static extern void libvlc_media_player_stop(IntPtr libvlc_mediaplayer);

// 解析視訊資源的媒體資訊(如時長等)

public static extern void libvlc_media_parse(IntPtr libvlc_media);

// 傳回視訊的時長(必須先調用libvlc_media_parse之後,該函數才會生效)

public static extern Int64 libvlc_media_get_duration(IntPtr libvlc_media);

// 目前播放的時間

public static extern Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer);

// 設定播放位置(拖動)

public static extern void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time);

public static extern void libvlc_media_player_release(IntPtr libvlc_mediaplayer);

// 擷取和設定音量

public static extern int libvlc_audio_get_volume(IntPtr libvlc_media_player);

public static extern void libvlc_audio_set_volume(IntPtr libvlc_media_player, int volume);

// 設定全屏

public static extern void libvlc_set_fullscreen(IntPtr libvlc_media_player, int isFullScreen);

//設定播放速度

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()

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

這個是調用 下面導入的函數重新封裝

下面我講解一個方法的調用過程 其他的可以到代碼中學習

c# 播放器 支援所有格式

1. 這個獲得視訊檔案的路徑

c# 播放器 支援所有格式

2.将路徑打包傳遞作為數組的一個元素傳給

c# 播放器 支援所有格式

 傳遞到自己封裝的api

c# 播放器 支援所有格式

傳遞到api裡面 

改天給大家代碼位址  有需要學習的或者自己可以下載下傳一個完全安全的播放器 我明天會給大家分享位址

繼續閱讀