I recently spent some time writing a simple program to play a movie file full screen on a Linux machine. I wrote the program in JRuby using the vlcj framework, which wraps the VLC media player library libVLC.
There are a number of vlcj fullscreen examples around, including an example that comes with the vlcj source code. I had not done much JRuby/Java interop work, so it still took some work for me to get this working in JRuby. To save someone else from the same problems, I thought I would share my work here.
The following script expects the vlcj jars to be in the same directory, and expects libvlc to be installed in a standard location on the machine. The first argument to the script is expected to be the path to a movie file (e.g. movie_trailer.mp4), which will be played full screen with no window decoration.
require File.expand_path(File.dirname(__FILE__)) + "/vlcj-2.1.0.jar"
java_import Java::UkCoCapricaVlcjBinding::LibVlc
java_import Java::UkCoCapricaVlcjPlayer::MediaPlayerFactory
java_import Java::UkCoCapricaVlcjPlayerEmbeddedX::XFullScreenStrategy
java_import Java::UkCoCapricaVlcjRuntime::RuntimeUtil
java_import java.awt.Canvas
java_import java.awt.Color
java_import java.awt.BorderLayout
java_import javax.swing.SwingUtilities
java_import javax.swing.JFrame
java_import com.sun.jna.Native
class MediaPlayer
def initialize
library_name = RuntimeUtil.get_lib_vlc_library_name
# If libvlc is not in a default search path add the path
# NativeLibrary.add_search_path library_name, "/path/to/libvlc"
Native.loadLibrary(library_name, LibVlc.java_class)
end
def play(mrl)
SwingUtilities.invokeLater do
play_movie mrl
end
end
private
def play_movie(mrl)
media_player = build_media_player
media_player.play_media mrl
end
def build_media_player
libvlc_args = [
"--no-plugins-cache",
"--no-video-title-show",
"--no-snapshot-preview",
"--quiet",
"--quiet-synchro",
"--intf",
"dummy"
]
# Set up the Swing UI
frame = JFrame.new("Media Player")
frame.background = Color::BLACK
canvas = Canvas.new
canvas.background = Color::BLACK
frame.add(canvas, BorderLayout::CENTER)
frame.undecorated = true
frame.visible = true
# Set up vlcj
media_player_factory = MediaPlayerFactory.new(libvlc_args)
media_player = media_player_factory.new_embedded_media_player(XFullScreenStrategy.new(frame))
media_player.video_surface = media_player_factory.new_video_surface(canvas)
media_player.full_screen = true
media_player
end
end
MediaPlayer.new.play ARGV[0]
This is the simplest player possible, but there are *tons* of options for making something more sophisticated. And thanks to JRuby and vlcj writing a media player can be done from the comfort of a Ruby program.