Search in sources :

Example 1 with MediaPlayerEventAdapter

use of uk.co.caprica.vlcj.player.MediaPlayerEventAdapter in project Fling by entertailion.

the class FlingFrame method vlcTranscode.

protected void vlcTranscode(final String file) {
    // Transcoding does not support jumps
    isTranscoding = true;
    // http://caprica.github.io/vlcj/javadoc/2.1.0/index.html
    try {
        // clean up previous session
        if (mediaPlayer != null) {
            mediaPlayer.release();
        }
        if (mediaPlayerFactory != null) {
            mediaPlayerFactory.release();
        }
        mediaPlayerFactory = new MediaPlayerFactory();
        mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
        // Add a component to be notified of player events
        mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {

            public void opening(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Opening");
            }

            public void buffering(MediaPlayer mediaPlayer, float newCache) {
                Log.d(LOG_TAG, "VLC Transcoding: Buffering");
            }

            public void playing(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Playing");
                setDuration((int) (mediaPlayer.getLength() / 1000.0f));
                Log.d(LOG_TAG, "duration=" + duration);
            }

            public void paused(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Paused");
            }

            public void stopped(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Stopped");
            }

            public void finished(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Finished");
            }

            public void error(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Error");
            }

            public void videoOutput(MediaPlayer mediaPlayer, int newCount) {
                Log.d(LOG_TAG, "VLC Transcoding: VideoOutput");
            }
        });
        // Find a port for VLC HTTP server
        boolean started = false;
        int vlcPort = port + 1;
        while (!started) {
            try {
                ServerSocket serverSocket = new ServerSocket(vlcPort);
                Log.d(LOG_TAG, "Available port for VLC: " + vlcPort);
                started = true;
                serverSocket.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
                vlcPort++;
            } catch (Exception ex) {
                break;
            }
        }
        if (!rampClient.isClosed()) {
            rampClient.stop();
        }
        Inet4Address address = getNetworAddress();
        if (address != null) {
            // Play a particular item, with options if necessary
            final String[] options = { ":sout=#transcode{" + transcodingParameterValues + "}:http{mux=webm,dst=:" + vlcPort + "/cast.webm}", ":sout-keep" };
            // http://192.168.0.8:8087/cast.webm
            final String url = "http://" + address.getHostAddress() + ":" + vlcPort + "/cast.webm";
            Log.d(LOG_TAG, "url=" + url);
            if (true || isChromeCast()) {
                rampClient.launchApp(appId == null ? APP_ID : appId, selectedDialServer);
                // wait for socket to be ready...
                new Thread(new Runnable() {

                    public void run() {
                        while (!rampClient.isStarted() && !rampClient.isClosed()) {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                            }
                        }
                        if (!rampClient.isClosed()) {
                            mediaPlayer.playMedia(file, options);
                            rampClient.load(url);
                        }
                    }
                }).start();
            } else {
                rampClient.load(url);
            }
        } else {
            Log.d(LOG_TAG, "could not find a network interface");
        }
    } catch (Throwable e) {
        Log.e(LOG_TAG, "vlcTranscode: " + file, e);
    }
}
Also used : Inet4Address(java.net.Inet4Address) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) MediaPlayerFactory(uk.co.caprica.vlcj.player.MediaPlayerFactory) MediaPlayerEventAdapter(uk.co.caprica.vlcj.player.MediaPlayerEventAdapter) MediaPlayer(uk.co.caprica.vlcj.player.MediaPlayer)

Aggregations

IOException (java.io.IOException)1 Inet4Address (java.net.Inet4Address)1 ServerSocket (java.net.ServerSocket)1 UnknownHostException (java.net.UnknownHostException)1 MediaPlayer (uk.co.caprica.vlcj.player.MediaPlayer)1 MediaPlayerEventAdapter (uk.co.caprica.vlcj.player.MediaPlayerEventAdapter)1 MediaPlayerFactory (uk.co.caprica.vlcj.player.MediaPlayerFactory)1