📜  vlcj教程(1)

📅  最后修改于: 2023-12-03 14:48:20.976000             🧑  作者: Mango

Vlcj教程

Vlcj是一个基于Java的VLC媒体播放器库,允许Java开发人员通过代码运行VLC播放器。本教程将引导您如何使用Vlcj播放视频、音频文件、网络视听节目以及流媒体。

基础使用
准备工作

在使用Vlcj之前,需要安装并配置好VLC媒体播放器。可以从VLC官网下载适合你的操作系统版本。安装完成后,需要将VLC的路径添加到系统环境变量中。

添加依赖

在Maven中添加以下依赖:

<dependency>
    <groupId>uk.co.caprica</groupId>
    <artifactId>vlcj</artifactId>
    <version>4.3.0</version>
</dependency>
播放本地视频
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.base.MediaPlayer;

import java.nio.file.Path;
import java.nio.file.Paths;

public class PlayLocalVideo {
    public static void main(String[] args) {

        // 获取MediaPlayerFactory实例
        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();

        // 创建MediaPlayer实例
        MediaPlayer mediaPlayer = mediaPlayerFactory.mediaPlayers().newMediaPlayer();

        // 播放本地视频
        Path videoPath = Paths.get("path/to/video.mp4");
        mediaPlayer.media().play(videoPath.toUri().toString());

        // 等待视频播放完成之后释放资源
        mediaPlayer.events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
            @Override
            public void finished(MediaPlayer mediaPlayer) {
                mediaPlayer.release();
                mediaPlayerFactory.release();
            }
        });
    }
}
播放网络视频
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.base.MediaPlayer;

public class PlayInternetVideo {
    public static void main(String[] args) {

        // 获取MediaPlayerFactory实例
        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();

        // 创建MediaPlayer实例
        MediaPlayer mediaPlayer = mediaPlayerFactory.mediaPlayers().newMediaPlayer();

        // 播放网络视频
        mediaPlayer.media().play("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");

        // 等待视频播放完成之后释放资源
        mediaPlayer.events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
            @Override
            public void finished(MediaPlayer mediaPlayer) {
                mediaPlayer.release();
                mediaPlayerFactory.release();
            }
        });
    }
}
播放网络音频
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.base.MediaPlayer;

public class PlayInternetAudio {
    public static void main(String[] args) {

        // 获取MediaPlayerFactory实例
        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();

        // 创建MediaPlayer实例
        MediaPlayer mediaPlayer = mediaPlayerFactory.mediaPlayers().newMediaPlayer();

        // 播放网络音频
        mediaPlayer.media().play("http://listen.radionomy.com/80-s-planet");

        // 等待音频播放完成之后释放资源
        mediaPlayer.events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
            @Override
            public void finished(MediaPlayer mediaPlayer) {
                mediaPlayer.release();
                mediaPlayerFactory.release();
            }
        });
    }
}
播放网络电视台
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.base.MediaPlayer;

public class PlayInternetTV {
    public static void main(String[] args) {

        // 获取MediaPlayerFactory实例
        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();

        // 创建MediaPlayer实例
        MediaPlayer mediaPlayer = mediaPlayerFactory.mediaPlayers().newMediaPlayer();

        // 播放网络电视台
        mediaPlayer.media().play("http://10.0.0.5:8080/?action=stream");

        // 等待电视播放完成之后释放资源
        mediaPlayer.events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
            @Override
            public void finished(MediaPlayer mediaPlayer) {
                mediaPlayer.release();
                mediaPlayerFactory.release();
            }
        });
    }
}
拍照
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.base.MediaPlayerOptions;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.player.embedded.videosurface.callback.BufferFormat;
import uk.co.caprica.vlcj.player.embedded.videosurface.callback.RenderCallback;
import uk.co.caprica.vlcj.player.embedded.videosurface.callback.format.RGBBufferFormat;
import uk.co.caprica.vlcj.player.embedded.videosurface.swing.SwingVideoSurface;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CaptureImage {

    private final EmbeddedMediaPlayer mediaPlayer;

    private final SwingVideoSurface videoSurface;

    private volatile boolean snap;

    public static void main(String[] args) throws Exception {
        // 获取MediaPlayerFactory实例
        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();

        // 创建MediaPlayer实例
        MediaPlayerOptions options = new MediaPlayerOptions();
        options.setVideoSurface(MediaPlayerFactory.videoSurface());
        MediaPlayer mediaPlayer = mediaPlayerFactory.mediaPlayers(options).newMediaPlayer();

        // 创建JFrame
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Capture Image");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            try {
                CaptureImage captureImage = new CaptureImage(mediaPlayer);
                frame.getContentPane().add(captureImage.videoSurface, BorderLayout.CENTER);
                frame.pack();
                while (true) {
                    Thread.sleep(1);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    public CaptureImage(MediaPlayer mediaPlayer) {
        this.mediaPlayer = (EmbeddedMediaPlayer) mediaPlayer;
        this.videoSurface = new SwingVideoSurface(new RenderCallback() {
            @Override
            public void display(BufferFormat bufferFormat, BufferedImage bufferedImage) {
                if (snap) {
                    Path dir = Paths.get("path/to/save/image");
                    try {
                        Files.createDirectories(dir);
                        ImageIO.write(bufferedImage, "png", dir.resolve(System.currentTimeMillis() + ".png").toFile());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    snap = false;
                }
            }

            @Override
            public void onFormatChange(BufferFormat bufferFormat) {
            }

            @Override
            public void onTitleChange(String s) {
            }

            @Override
            public void onSeekableChanged(int i) {
            }

            @Override
            public void onPausableChanged(int i) {
            }

            @Override
            public void onLengthChanged(long l) {
            }

            @Override
            public void onVideoOutput(MediaPlayer mediaPlayer, int i) {
            }

            @Override
            public void onMediaPlayerBuffering(MediaPlayer mediaPlayer, float v) {
            }

            @Override
            public void onMediaPlayerStateChanged(MediaPlayer mediaPlayer, int i) {
            }

            @Override
            public void onMediaPlayerTimeChanged(MediaPlayer mediaPlayer, long l) {
            }
        });
        this.mediaPlayer.videoSurface().set(videoSurface);
        this.mediaPlayer.media().play("path/to/video.mp4");
        Thread snapThread = new Thread(() -> {
            try {
                while (true) {
                    Thread.sleep(1000);
                    snap = true;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        snapThread.setDaemon(true);
        snapThread.start();
    }

    private static BufferedImage toBufferedImage(Canvas canvas) {
        BufferedImage image = new BufferedImage(canvas.getWidth(), canvas.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        canvas.paint(g2);
        g2.dispose();
        return image;
    }
}
总结

本教程展示了如何使用Vlcj库播放本地视频、网络视频、网络音频以及网络电视。使用Vlcj库还可以进行许多其他操作,例如屏幕录像、滤镜等等。无论你想要做什么,Vlcj都能够提供帮助。