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