Java Reference
In-Depth Information
VLCJ
A commonly used third-party solution uses the open source
VLC Media Player
program
,
connecting it to a Java
Canvas
to display video. Of course, in addition to downloading
vlcj
,
you must download and install
VLC
itself. That is outside the scope of this topic, and is quite
platform-dependent.
This project depends on JNA, an unsupported
com.sun
package. You use this to tell VLCJ
where VLC is installed; this is one of the first statements in the
MyVideoCanvas
constructor
getMediaPlayer()
method, and tell the resulting player object to
prepareMedia()
,
parseMedia()
, and
play()
. These steps are shown in the context of a full example in
Example 12-8
.
The URL is supplied on the command line, and can be a local file, a remote
streaming video, or anything else that the underlying VLC player can handle.
Example 12-8. VlcjVideo.java
public
public class
class
VlcjVideo
VlcjVideo
extends
extends
JFrame
{
private
private static
static final
final
long
long
serialVersionUID
=
1L
;
public
public static
static
void
void
main
(
String
[]
args
) {
new
new
VlcjVideo
(
args
[
0
]);
}
public
public
VlcjVideo
(
String url
) {
setTitle
(
"VLCJ Video"
);
setDefaultCloseOperation
(
EXIT_ON_CLOSE
);
setSize
(
800
,
600
);
JPanel player
=
new
new
MyVideoPanel
();
add
(
player
,
BorderLayout
.
CENTER
);
pack
();
setVisible
(
true
true
);
((
MyVideoPanel
)
player
).
play
(
url
);
}
class
class
MyVideoPanel
MyVideoPanel
extends
extends
JPanel
{
private
private static
static final
final
long
long
serialVersionUID
=
1L
;
private
private
File vlcWhere
=
new
new
File
(
"/usr/local/lib"
);
private
private
EmbeddedMediaPlayer player
;
public
public
MyVideoPanel
() {
NativeLibrary
.
addSearchPath
(
"libvlc"
,
vlcWhere
.
getAbsolutePath
());
EmbeddedMediaPlayerComponent videoCanvas
=