]> git.sesse.net Git - vlc/blob - bindings/java/core/src/main/java/org/videolan/jvlc/MediaPlayer.java
8590e4645992f5febc6ab53c96771aaf1ac39e29
[vlc] / bindings / java / core / src / main / java / org / videolan / jvlc / MediaPlayer.java
1 /*****************************************************************************
2  * MediaInstance.java: VLC Java Bindings Media Instance
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 java.util.ArrayList;
29 import java.util.EnumSet;
30 import java.util.List;
31
32 import org.videolan.jvlc.event.MediaPlayerCallback;
33 import org.videolan.jvlc.event.MediaPlayerListener;
34 import org.videolan.jvlc.internal.LibVlc;
35 import org.videolan.jvlc.internal.LibVlcEventType;
36 import org.videolan.jvlc.internal.LibVlc.LibVlcEventManager;
37 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaPlayer;
38 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
39
40
41 public class MediaPlayer
42 {
43
44     private final LibVlcMediaPlayer instance;
45
46     private final LibVlc libvlc;
47
48     private final LibVlcEventManager eventManager;
49
50     private List<MediaPlayerCallback> callbacks = new ArrayList<MediaPlayerCallback>();
51
52     private MediaDescriptor mediaDescriptor;
53
54     private volatile boolean released;
55
56     MediaPlayer(JVLC jvlc, LibVlcMediaPlayer instance)
57     {
58         libvlc_exception_t exception = new libvlc_exception_t();
59         this.instance = instance;
60         libvlc = jvlc.getLibvlc();
61         eventManager = libvlc.libvlc_media_player_event_manager(instance, exception);
62     }
63
64     public MediaPlayer(MediaDescriptor mediaDescriptor)
65     {
66         libvlc_exception_t exception = new libvlc_exception_t();
67         libvlc = mediaDescriptor.getLibvlc();
68         instance = libvlc.libvlc_media_player_new_from_media(mediaDescriptor.getInstance(), exception);
69         eventManager = libvlc.libvlc_media_player_event_manager(instance, exception);
70         this.mediaDescriptor = mediaDescriptor;
71     }
72
73     public MediaDescriptor getMediaDescriptor()
74     {
75         return mediaDescriptor;
76     }
77
78     public void play()
79     {
80         libvlc_exception_t exception = new libvlc_exception_t();
81         libvlc.libvlc_media_player_play(instance, exception);
82     }
83
84     public void stop()
85     {
86         libvlc_exception_t exception = new libvlc_exception_t();
87         libvlc.libvlc_media_player_stop(instance, exception);
88     }
89
90     public void pause()
91     {
92         libvlc_exception_t exception = new libvlc_exception_t();
93         libvlc.libvlc_media_player_pause(instance, exception);
94     }
95
96     public long getLength()
97     {
98         libvlc_exception_t exception = new libvlc_exception_t();
99         return libvlc.libvlc_media_player_get_length(instance, exception);
100     }
101
102     public long getTime()
103     {
104         libvlc_exception_t exception = new libvlc_exception_t();
105         return libvlc.libvlc_media_player_get_time(instance, exception);
106     }
107
108     public void setTime(long time)
109     {
110         libvlc_exception_t exception = new libvlc_exception_t();
111         libvlc.libvlc_media_player_set_time(instance, time, exception);
112     }
113
114     public float getPosition()
115     {
116         libvlc_exception_t exception = new libvlc_exception_t();
117         return libvlc.libvlc_media_player_get_position(instance, exception);
118     }
119
120     public void setPosition(float position)
121     {
122         libvlc_exception_t exception = new libvlc_exception_t();
123         libvlc.libvlc_media_player_set_position(instance, position, exception);
124     }
125
126     public boolean willPlay()
127     {
128         libvlc_exception_t exception = new libvlc_exception_t();
129         return (libvlc.libvlc_media_player_will_play(instance, exception) == 1);
130     }
131
132     public float getRate()
133     {
134         libvlc_exception_t exception = new libvlc_exception_t();
135         return libvlc.libvlc_media_player_get_rate(instance, exception);
136     }
137
138     public void setRate(float rate)
139     {
140         libvlc_exception_t exception = new libvlc_exception_t();
141         libvlc.libvlc_media_player_set_rate(instance, rate, exception);
142     }
143
144     public boolean hasVideoOutput()
145     {
146         libvlc_exception_t exception = new libvlc_exception_t();
147         return (libvlc.libvlc_media_player_has_vout(instance, exception) == 1);
148     }
149
150     public float getFPS()
151     {
152         libvlc_exception_t exception = new libvlc_exception_t();
153         return libvlc.libvlc_media_player_get_fps(instance, exception);
154     }
155     
156     public boolean isPlaying()
157     {
158         libvlc_exception_t exception = new libvlc_exception_t();
159         return libvlc.libvlc_media_player_is_playing(instance, exception) == 1? true : false;
160     }
161
162     public void addListener(final MediaPlayerListener listener)
163     {
164         MediaPlayerCallback callback = new MediaPlayerCallback(this, listener);
165         libvlc_exception_t exception = new libvlc_exception_t();
166         for (LibVlcEventType event : EnumSet.range(
167             LibVlcEventType.libvlc_MediaPlayerPlaying,
168             LibVlcEventType.libvlc_MediaPlayerTimeChanged))
169         {
170             libvlc.libvlc_event_attach(eventManager, event.ordinal(), callback, null, exception);
171         }
172         callbacks.add(callback);
173     }
174
175     /**
176      * {@inheritDoc}
177      */
178     @Override
179     protected void finalize() throws Throwable
180     {
181         release();
182         super.finalize();
183     }
184
185     public void release()
186     {
187         if (released)
188         {
189             return;
190         }
191         released = true;
192
193         libvlc_exception_t exception = new libvlc_exception_t();
194         for (MediaPlayerCallback callback : callbacks)  
195         {
196             for (LibVlcEventType event : EnumSet.range(
197                 LibVlcEventType.libvlc_MediaPlayerPlaying,
198                 LibVlcEventType.libvlc_MediaPlayerPositionChanged))
199             {
200                 libvlc.libvlc_event_detach(eventManager, event.ordinal(), callback, null, exception);
201             }
202         }
203         libvlc.libvlc_media_player_release(instance);
204         
205     }
206     
207     /**
208      * Returns the instance.
209      * @return the instance
210      */
211     LibVlcMediaPlayer getInstance()
212     {
213         return instance;
214     }
215
216 }