1 /*****************************************************************************
2 * MediaInstance.java: VLC Java Bindings Media Instance
3 *****************************************************************************
4 * Copyright (C) 1998-2008 the VideoLAN team
6 * Authors: Filippo Carone <filippo@carone.org>
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.
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.
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 *****************************************************************************/
26 package org.videolan.jvlc;
28 import java.util.ArrayList;
29 import java.util.EnumSet;
30 import java.util.List;
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;
41 public class MediaPlayer
44 private final LibVlcMediaInstance instance;
46 private final LibVlc libvlc;
48 private final LibVlcEventManager eventManager;
50 private List<MediaInstanceCallback> callbacks = new ArrayList<MediaInstanceCallback>();
52 private MediaDescriptor mediaDescriptor;
54 MediaPlayer(JVLC jvlc, LibVlcMediaInstance instance)
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);
62 public MediaPlayer(MediaDescriptor mediaDescriptor)
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;
71 public MediaDescriptor getMediaDescriptor()
73 return mediaDescriptor;
78 libvlc_exception_t exception = new libvlc_exception_t();
79 libvlc.libvlc_media_player_play(instance, exception);
84 libvlc_exception_t exception = new libvlc_exception_t();
85 libvlc.libvlc_media_player_stop(instance, exception);
90 libvlc_exception_t exception = new libvlc_exception_t();
91 libvlc.libvlc_media_player_pause(instance, exception);
94 public long getLength()
96 libvlc_exception_t exception = new libvlc_exception_t();
97 return libvlc.libvlc_media_player_get_length(instance, exception);
100 public long getTime()
102 libvlc_exception_t exception = new libvlc_exception_t();
103 return libvlc.libvlc_media_player_get_time(instance, exception);
106 public void setTime(long time)
108 libvlc_exception_t exception = new libvlc_exception_t();
109 libvlc.libvlc_media_player_set_time(instance, time, exception);
112 public float getPosition()
114 libvlc_exception_t exception = new libvlc_exception_t();
115 return libvlc.libvlc_media_player_get_position(instance, exception);
118 public void setPosition(float position)
120 libvlc_exception_t exception = new libvlc_exception_t();
121 libvlc.libvlc_media_player_set_position(instance, position, exception);
124 public boolean willPlay()
126 libvlc_exception_t exception = new libvlc_exception_t();
127 return (libvlc.libvlc_media_player_will_play(instance, exception) == 1);
130 public float getRate()
132 libvlc_exception_t exception = new libvlc_exception_t();
133 return libvlc.libvlc_media_player_get_rate(instance, exception);
136 public void setRate(float rate)
138 libvlc_exception_t exception = new libvlc_exception_t();
139 libvlc.libvlc_media_player_set_rate(instance, rate, exception);
142 public boolean hasVideoOutput()
144 libvlc_exception_t exception = new libvlc_exception_t();
145 return (libvlc.libvlc_media_player_has_vout(instance, exception) == 1);
148 public float getFPS()
150 libvlc_exception_t exception = new libvlc_exception_t();
151 return libvlc.libvlc_media_player_get_fps(instance, exception);
154 public void addListener(final MediaInstanceListener listener)
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_MediaPlayerPlaying,
160 LibVlcEventType.libvlc_MediaPlayerTimeChanged))
162 libvlc.libvlc_event_attach(eventManager, event.ordinal(), callback, null, exception);
164 callbacks.add(callback);
171 protected void finalize() throws Throwable
173 libvlc_exception_t exception = new libvlc_exception_t();
174 for (MediaInstanceCallback callback : callbacks)
176 for (LibVlcEventType event : EnumSet.range(
177 LibVlcEventType.libvlc_MediaPlayerPlaying,
178 LibVlcEventType.libvlc_MediaPlayerPositionChanged))
180 libvlc.libvlc_event_detach(eventManager, event.ordinal(), callback, null, exception);
183 libvlc.libvlc_media_player_release(instance);
188 * Returns the instance.
189 * @return the instance
191 LibVlcMediaInstance getInstance()