]> git.sesse.net Git - vlc/blob - bindings/java/core/src/main/java/org/videolan/jvlc/internal/LibVlcImpl.java
Add some locking.
[vlc] / bindings / java / core / src / main / java / org / videolan / jvlc / internal / LibVlcImpl.java
1 /*****************************************************************************
2  * ${file_name}: VLC Java Bindings
3  *****************************************************************************
4  * Copyright (C) 1998-2007 the VideoLAN team
5  *
6  * Authors: Filippo Carone <filippo@carone.org>
7  *
8  *
9  * $Id $
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 package org.videolan.jvlc.internal;
27
28 import java.awt.Canvas;
29
30 import javax.swing.JFrame;
31 import javax.swing.JPanel;
32
33 import org.videolan.jvlc.internal.LibVlc.LibVlcCallback;
34 import org.videolan.jvlc.internal.LibVlc.LibVlcEventManager;
35 import org.videolan.jvlc.internal.LibVlc.LibVlcInstance;
36 import org.videolan.jvlc.internal.LibVlc.LibVlcMedia;
37 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaPlayer;
38 import org.videolan.jvlc.internal.LibVlc.libvlc_event_t;
39 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
40
41 import com.sun.jna.Platform;
42 import com.sun.jna.Pointer;
43
44
45 public class LibVlcImpl
46 {
47
48     public static boolean done;
49
50     public static void main(String[] args) throws InterruptedException
51     {
52         LibVlc libVlc = LibVlc.SYNC_INSTANCE;
53         libvlc_exception_t exception = new libvlc_exception_t();
54         libVlc.libvlc_exception_init(exception);
55
56         final Object lock = new Object();
57
58         System.out.println("Starting vlc");
59         System.out.println("version: " + libVlc.libvlc_get_version());
60         System.out.println("changeset: " + libVlc.libvlc_get_changeset());
61         System.out.println("compiler: " + libVlc.libvlc_get_compiler());
62         
63         LibVlcInstance libvlc_instance_t = libVlc.libvlc_new(0, new String[] {"/usr/local/bin/vlc"}, exception);
64
65         LibVlcMedia mediaDescriptor = libVlc
66             .libvlc_media_new(libvlc_instance_t, "/home/carone/apps/a.avi", exception);
67
68         LibVlcMediaPlayer mediaPlayer = libVlc.libvlc_media_player_new_from_media(mediaDescriptor, exception);
69
70         LibVlcEventManager mediaInstanceEventManager = libVlc.libvlc_media_player_event_manager(mediaPlayer, exception);
71
72         LibVlcCallback played = new LibVlcCallback()
73         {
74
75             public void callback(libvlc_event_t libvlc_event_t, Pointer pointer)
76             {
77                 System.out.println("Playing started.");
78             }
79         };
80
81         LibVlcCallback endReached = new LibVlcCallback()
82         {
83
84             public void callback(libvlc_event_t libvlc_event_t, Pointer pointer)
85             {
86                 synchronized (lock)
87                 {
88                     System.out.println("Playing finished.");
89                     LibVlcImpl.done = true;
90                 }
91             }
92         };
93
94         libVlc.libvlc_event_attach(
95             mediaInstanceEventManager,
96             LibVlcEventType.libvlc_MediaPlayerPlaying.ordinal(),
97             played,
98             null,
99             exception);
100
101         libVlc.libvlc_event_attach(
102             mediaInstanceEventManager,
103             LibVlcEventType.libvlc_MediaPlayerEndReached.ordinal(),
104             endReached,
105             null,
106             exception);
107
108         JFrame frame = new JFrame("title");
109         frame.setVisible(true);
110         frame.setLocation(100, 100);
111         frame.setSize(500, 500);
112         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
113         
114         JPanel panel = new JPanel();
115         Canvas canvas = new Canvas();
116         canvas.setSize(500, 500);
117         panel.add(canvas);
118         frame.getContentPane().add(panel);
119         frame.pack();
120         
121         int drawable = (int) com.sun.jna.Native.getComponentID(canvas);
122
123         if (Platform.isWindows())
124         {
125             libVlc.libvlc_media_player_set_hwnd(mediaPlayer, drawable, exception);
126         }
127         else
128         {
129             libVlc.libvlc_media_player_set_xwindow(mediaPlayer, drawable, exception);
130         }
131         libVlc.libvlc_media_player_play(mediaPlayer, exception);
132     }
133 }