Application 3 of VLC: dynamic call of libvlc. dll of vlc-0.9.4

Source: Internet
Author: User
The libvlc. dll provided by the vlc-0.9.4 can be dynamically called, Jeremiah this blog introduces how to use C # And winform framework to call libvlc. dll as a simple player.

1. vs2005 new project, the vlc-0.9.4 libvlc. dll, libvlccore. dll, plugins directory all copy to the project directory \ bin \ debug.

2. Create an abnormal struct
Using system;
Using system. Collections. Generic;
Using system. text;

Namespace myownplayer
{
// Exception struct
Public struct predictionstruct
{
Private int raised;
Private int code;
Private string message;
}

Class mediaexception
{
}
}

3. corehandle and core classes
Using system;
Using system. runtime. interopservices;

Namespace myownplayer
{
Class corehandle: safehandle
{
// Constructor
Public corehandle ()
: Base (intptr. Zero, true)
{
}

// Rewrite Method
Public override bool isinvalid
{
Get {return handle = intptr. Zero ;}
}

Protected override bool releasehandle ()
{
If (! Isinvalid)
{
Libvlc_release (this );
Handle = intptr. zero;
}
Return true;
}

Protected override void dispose (bool disposing)
{
Releasehandle ();
Base. Dispose (disposing );
}

// DLL dynamic import
[Dllimport ("libvlc")]
Private Static extern void libvlc_release (corehandle );
}
}

Using system;
Using system. runtime. interopservices;

Namespace myownplayer
{
Class Core
{
// Corehandle fields and attributes
Private corehandle;
Public corehandle
{
Get {return corehandle ;}
}

// Constructor
Public core (string [] argv, ref predictionstruct ex)
{
Corehandle = libvlc_new (argv. length, argv, ref ex );
}

// DLL dynamic import
[Dllimport ("libvlc")]
Private Static extern corehandle libvlc_new (INT argc, string [] ARGs, ref predictionstruct ex );
}
}

3. mediahandle and media class. Pay attention to the non-English path processing method.
Using system;
Using system. runtime. interopservices;

Namespace myownplayer
{
Class mediahandle: safehandle
{
// Constructor
Public mediahandle ()
: Base (intptr. Zero, true)
{
}

// Rewrite Method
Public override bool isinvalid
{
Get {return handle = intptr. Zero ;}
}

Protected override bool releasehandle ()
{
If (! Isinvalid)
{
Libvlc_media_release (this );
Handle = intptr. zero;
}
Return true;
}

Protected override void dispose (bool disposing)
{
Releasehandle ();
Base. Dispose (disposing );
}

// DLL dynamic import
[Dllimport ("libvlc")]
Private Static extern void libvlc_media_release (mediahandle );
}
}

Using system;
Using system. text;
Using system. runtime. interopservices;

Namespace myownplayer
{
Class Media
{
// Mediahandle fields and attributes
Private mediahandle;
Public mediahandle
{
Get {return mediahandle ;}
}

// Constructor
Public media (corehandle, string filename, ref exceptionstruct ex)
{
// C # For UTF-16 encoding, libvlc. DLL for UTF-8 encoding, need to convert.
Utf8encoding utf8 = new utf8encoding ();
Mediahandle = libvlc_media_new (corehandle, utf8.getbytes (filename), ref ex );
}

// DLL dynamic import
[Dllimport ("libvlc")]
Private Static extern mediahandle libvlc_media_new
(Corehandle, [exploralas (unmanagedtype. lparray)] Byte [] link, ref exceptionstruct ex );
}
}

5. mediaplayerhandle and mediaplayer
Using system;
Using system. runtime. interopservices;

Namespace myownplayer
{
Class mediaplayerhandle: safehandle
{
// Constructor
Public mediaplayerhandle ()
: Base (intptr. Zero, true)
{
}

// Rewrite Method
Public override bool isinvalid
{
Get {return handle = intptr. Zero ;}
}

Protected override bool releasehandle ()
{
If (! Isinvalid)
{
Libvlc_media_player_release (this); handle = intptr. zero;
}
Return true;
}

Protected override void dispose (bool disposing)
{
Releasehandle ();
Base. Dispose (disposing );
}

// DLL dynamic import
[Dllimport ("libvlc")]
Private Static extern void libvlc_media_player_release (mediaplayerhandle );
}
}

Using system;
Using system. runtime. interopservices;

Namespace myownplayer
{
Class mediaplayer
{
// Mediaplayerhandle field and attribute
Private mediaplayerhandle;
Public mediaplayerhandle
{
Get {return mediaplayerhandle ;}
}

// Constructor
Public mediaplayer (mediahandle, ref predictionstruct ex)
{
Mediaplayerhandle = libvlc_media_player_new_from_media (mediahandle, ref ex );
}

// Set the parent window
Public void vediosetparent (corehandle, intptr HDT, ref exceptionstruct ex)
{
Libvlc_video_set_parent (corehandle, HDT, ref ex );
}

// Play
Public void play (ref predictionstruct ex)
{
Libvlc_media_player_play (mediaplayerhandle, ref ex );
}

// Stop
Public void stop (ref predictionstruct ex)
{
Libvlc_media_player_stop (mediaplayerhandle, ref ex );
}

// DLL dynamic import
[Dllimport ("libvlc")]
Private Static extern mediaplayerhandle libvlc_media_player_new_from_media (mediahandle libvlc_media_handle, ref exceptionstruct ex );

[Dllimport ("libvlc")]
Private Static extern void libvlc_video_set_parent (corehandle, intptr HDT, ref exceptionstruct ex );

[Dllimport ("libvlc")]
Private Static extern void libvlc_media_player_play (mediaplayerhandle, ref exceptionstruct ex );

[Dllimport ("libvlc")]
Private Static extern void libvlc_media_player_stop (mediaplayerhandle, ref exceptionstruct ex );
}
}

6. The basic work is done well. Next, create a form, draw a panel (playing container), draw a Textbox (playing address), draw a button (playing button), and click the button event as follows:
Private void button#click (Object sender, eventargs E)
{
// URI of the file to be played
String uri = This. textbox1.text;

// Handle of the control for playing
Intptr HDL = This. panel1.handle;

// Playback Parameters
String [] argv = new string [] {"-I", "-- ignore-config "};

// Create a VLC object
Predictionstruct EX = new predictionstruct ();
Core core = new core (argv, ref ex );
Media media = new media (core. corehandle, Uri, ref ex );
Mediaplayer player = new mediaplayer (media. mediahandle, ref ex );

// Garbage collection
GC. Collect ();

// Play
Player. vediosetparent (core. corehandle, HDL, ref ex );
Player. Play (ref ex );

// Continue garbage collection and other related operations
GC. Collect ();
GC. waitforpendingfinalizers ();
}
 
7. The basic playback function is implemented in this way. For other interfaces, see the \ include \ VLC \ libvlc. h file under the source code, which provides detailed descriptions of external interfaces.
 
8. aboveCode). Note Step 1 for debugging attachments.

Reference URL:
1. http://forum.videolan.org/viewtopic.php? F = 14 & t = 47385 & St = 0 & Sk = T & SD = A & hilit = C % 23 + wrapper

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.