C # player supports all formats

Source: Internet
Author: User
Original article: C # player supports all formats

--- Restore content start ---

Directly Add code

Internal static class libvlcapi
{
Internal struct pointertoarrayofpointerhelper
{
[Financialas (unmanagedtype. byvalarray, sizeconst = 1000)]
Public intptr [] pointers;
}

Public static intptr libvlc_new (string [] arguments)
{
Pointertoarrayofpointerhelper argv = new pointertoarrayofpointerhelper ();
Argv. Pointers = new intptr [2, 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 );
}
}
}
// Construct a video file from the path
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 );
}

}
// Construct a video file from the Network Location
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 );
}
}
}

// Configure //----------------------------------------------------------------------------------------
// The following is the libvlc. dll export Function

// Create a libvlc instance with reference count
[Dllimport ("libvlc", callingconvention = callingconvention. stdcall, exactspelling = true)]
[Suppressunmanagedcodesecurity]
Private Static extern intptr libvlc_new (INT argc, intptr argv );

// Release the libvlc instance
[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 ();

// Construct a libvlc_meida file from a video source (such as a URL)
[Dllimport ("libvlc", callingconvention = callingconvention. stdcall, exactspelling = true)]
[Suppressunmanagedcodesecurity]
Private Static extern intptr libvlc_media_new_location (intptr libvlc_instance, intptr path );

// Construct a libvlc_media file from the local file path
[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 );

// Create libvlc_media_player (playback core)
[Dllimport ("libvlc", callingconvention = callingconvention. stdcall, exactspelling = true)]
[Suppressunmanagedcodesecurity]
Public static extern intptr libvlc_media_player_new (intptr libvlc_instance );

// Bind the video (libvlc_media) to the player.
[Dllimport ("libvlc", callingconvention = callingconvention. stdcall, exactspelling = true)]
[Suppressunmanagedcodesecurity]
Public static extern void libvlc_media_player_set_media (intptr libvlc_media_player, intptr libvlc_media );

// Set the image output window
[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 );

// Parse media information of video resources (such as duration)
[Dllimport ("libvlc", callingconvention = callingconvention. stdcall, exactspelling = true)]
[Suppressunmanagedcodesecurity]
Public static extern void libvlc_media_parse (intptr libvlc_media );

// Return the video duration (this function takes effect only after libvlc_media_parse is called)
[Dllimport ("libvlc", callingconvention = callingconvention. stdcall, exactspelling = true)]
[Suppressunmanagedcodesecurity]
Public static extern int64 libvlc_media_get_duration (intptr libvlc_media );

// Current playback time
[Dllimport ("libvlc", callingconvention = callingconvention. stdcall, exactspelling = true)]
[Suppressunmanagedcodesecurity]
Public static extern int64 libvlc_media_player_get_time (intptr libvlc_mediaplayer );

// Set the playback position (drag)
[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 );

// Obtain and set the volume
[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 );

// Set full screen
[Dllimport ("libvlc", callingconvention = callingconvention. stdcall, exactspelling = true)]
[Suppressunmanagedcodesecurity]
Public static extern void libvlc_set_fullscreen (intptr libvlc_media_player, int isfullscreen );

// Set the playback speed
[Dllimport ("libvlc", callingconvention = callingconvention. stdcall, exactspelling = true)]
[Suppressunmanagedcodesecurity]
Public static extern int libvlc_media_player_set_rate (intptr libvlc_media, float rate );
}

This is to import the APIs in libvlc to the C # development environment.

 

 

Class vlcplayer
{
// Playback video file instance
Private intptr libvlc_instance _;
// Playback instance
Private intptr libvlc_media_player _;
// Video duration
Private double duration _;



Public vlcplayer (string pluginpath)
{
String plugin_arg = "-- Plugin-Path =" + pluginpath;
String [] arguments = {"0", "Dummy", "-- ignore-config", "-- no-video-title", plugin_arg };
// Playback video file instance
Libvlc_instance _ = libvlcapi. libvlc_new (arguments );
// Playback instance
Libvlc_media_player _ = libvlcapi. libvlc_media_player_new (libvlc_instance _);
}

// Accept the window
Public void setrenderwindow (INT wndhandle)
{
If (libvlc_instance _! = Intptr. Zero & wndhandle! = 0)
{
Libvlcapi. libvlc_media_player_set_hwnd (libvlc_media_player _, wndhandle );
}
}

// The player controls video playback.
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 );
// Return the video duration

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 _);
}
}
// The player controls video file parsing.
Public void pause ()
{
If (libvlc_media_player _! = Intptr. Zero)
{
Libvlcapi. libvlc_media_player_pause (libvlc_media_player _);
}
}
// The player controls the stop of the video file
Public void stop ()
{
If (libvlc_media_player _! = Intptr. Zero)
{
Libvlcapi. libvlc_media_player_stop (libvlc_media_player _);
}
}

// The Player obtains the current playback time.
Public double getplaytime ()
{
Return libvlcapi. libvlc_media_player_get_time (libvlc_media_play_)/1000.0;
}

// Set the current playback time for the player
Public void setplaytime (double seektime)
{
Libvlcapi. libvlc_media_player_set_time (libvlc_media_player _, (int64) (seektime x 1000 ));
}

// The Player obtains the current playback sound size.
Public int getvolume ()
{
Return libvlcapi. libvlc_audio_get_volume (libvlc_media_player _);
}

// Set the current playing sound for the player
Public void setvolume (INT volume)
{
Libvlcapi. libvlc_audio_set_volume (libvlc_media_player _, volume );
}
// Set full screen
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 );

}
}

 

This is to call the following imported function to re-encapsulate

 

Next, I will explain how to call a method. Others can be learned in the code.

1. Obtain the path of the video file.

 

2. Package the path and pass it as an element of the array.

 

Pass to your encapsulated API

Passed to API

 

 

I will share the address with you tomorrow if you need to learn the code or you can download a completely secure player.

C # player supports all formats

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.