]> git.sesse.net Git - vlc/blob - bindings/java/core/src/main/java/org/videolan/jvlc/MediaListPlayer.java
Add some locking.
[vlc] / bindings / java / core / src / main / java / org / videolan / jvlc / MediaListPlayer.java
1 /*****************************************************************************
2  * MediaListPlayer.java: VLC Java Bindings, MediaList player
3  *****************************************************************************
4  * Copyright (C) 1998-2008 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;
27
28 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaListPlayer;
29 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
30
31
32 public class MediaListPlayer
33 {
34
35     private final LibVlcMediaListPlayer instance;
36
37     private final JVLC jvlc;
38
39     private volatile boolean released;
40     
41     public MediaListPlayer(JVLC jvlc)
42     {
43         libvlc_exception_t exception = new libvlc_exception_t();
44         this.jvlc = jvlc;
45         instance = jvlc.getLibvlc().libvlc_media_list_player_new(jvlc.getInstance(), exception);
46     }
47
48     public void setMediaList(MediaList list)
49     {
50         libvlc_exception_t exception = new libvlc_exception_t();
51         jvlc.getLibvlc().libvlc_media_list_player_set_media_list(instance, list.getInstance(), exception);
52     }
53
54     public boolean isPlaying()
55     {
56         libvlc_exception_t exception = new libvlc_exception_t();
57         return jvlc.getLibvlc().libvlc_media_list_player_is_playing(instance, exception) == 1;
58     }
59
60     
61     /**
62      * 
63      */
64     public void play()
65     {
66         libvlc_exception_t exception = new libvlc_exception_t();
67         jvlc.getLibvlc().libvlc_media_list_player_play(instance, exception);
68         try
69         {
70             while (jvlc.getLibvlc().libvlc_media_list_player_is_playing(instance, exception) == 0)
71             {
72                 Thread.sleep(25);
73             }
74         }
75         catch(InterruptedException e)
76         {
77             //
78         }
79     }
80
81     public void stop()
82     {
83         libvlc_exception_t exception = new libvlc_exception_t();
84         jvlc.getLibvlc().libvlc_media_list_player_stop(instance, exception);
85     }
86
87     public void pause()
88     {
89         libvlc_exception_t exception = new libvlc_exception_t();
90         jvlc.getLibvlc().libvlc_media_list_player_pause(instance, exception);
91     }
92     
93     public void next()
94     {
95         libvlc_exception_t exception = new libvlc_exception_t();
96         jvlc.getLibvlc().libvlc_media_list_player_next(instance, exception);
97     }
98
99     /**
100      * Plays the given descriptor and returns only when the player has started to play.
101      * @param descriptor The media descriptor to play
102      */
103     public void playItem(MediaDescriptor descriptor)
104     {
105         playItem(descriptor, true);
106     }
107     
108     /**
109      * @param descriptor The media descriptor to play
110      * @param synchronous If true it does not return until the player is not playing.
111      */
112     public void playItem(MediaDescriptor descriptor, boolean synchronous)
113     {
114         libvlc_exception_t exception = new libvlc_exception_t();
115         jvlc.getLibvlc().libvlc_media_list_player_play_item(instance, descriptor.getInstance(), exception);
116         if (!synchronous)
117         {
118             return;
119         }
120         
121         try
122         {
123             while (jvlc.getLibvlc().libvlc_media_list_player_is_playing(instance, exception) == 0)
124             {
125                 Thread.sleep(25);
126             }
127         }
128         catch(InterruptedException e)
129         {
130             //
131         }
132         
133     }
134
135     /**
136      * Plays the item at the given index and returns only when the player has started to play.
137      * @param index The item index to play.
138      */
139     public void playItem(int index)
140     {
141         playItem(index, true);
142     }
143     
144     /**
145      * @param index The item index to play.
146      * @param synchronous If true it does not return until the player is not playing.
147      */
148     public void playItem(int index, boolean synchronous)
149     {
150         libvlc_exception_t exception = new libvlc_exception_t();
151         jvlc.getLibvlc().libvlc_media_list_player_play_item_at_index(instance, index, exception);
152         try
153         {
154             while (jvlc.getLibvlc().libvlc_media_list_player_is_playing(instance, exception) == 0)
155             {
156                 Thread.sleep(25);
157             }
158         }
159         catch(InterruptedException e)
160         {
161             //
162         }
163     }
164     
165     public void setMediaInstance(MediaPlayer mediaInstance)
166     {
167         libvlc_exception_t exception = new libvlc_exception_t();
168         jvlc.getLibvlc().libvlc_media_list_player_set_media_player(instance, mediaInstance.getInstance(), exception);        
169     }
170
171     /**
172      * {@inheritDoc}
173      */
174     @Override
175     protected void finalize() throws Throwable
176     {
177         release();
178         super.finalize();
179     }
180
181     /**
182      * 
183      */
184     public void release()
185     {
186         if (released)
187         {
188             return;
189         }
190         released = true;
191         jvlc.getLibvlc().libvlc_media_list_player_release(instance);
192         
193     }
194
195 }