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.awt.Canvas;
29 import java.util.ArrayList;
30 import java.util.EnumSet;
31 import java.util.List;
33 import org.videolan.jvlc.event.MediaPlayerCallback;
34 import org.videolan.jvlc.event.MediaPlayerListener;
35 import org.videolan.jvlc.internal.LibVlc;
36 import org.videolan.jvlc.internal.LibVlcEventType;
37 import org.videolan.jvlc.internal.LibVlc.LibVlcEventManager;
38 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaPlayer;
39 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
41 import com.sun.jna.Native;
42 import com.sun.jna.Platform;
45 public class MediaPlayer
48 private final LibVlcMediaPlayer instance;
50 private final LibVlc libvlc;
52 private final LibVlcEventManager eventManager;
54 private List<MediaPlayerCallback> callbacks = new ArrayList<MediaPlayerCallback>();
56 private MediaDescriptor mediaDescriptor;
58 private volatile boolean released;
60 MediaPlayer(JVLC jvlc, LibVlcMediaPlayer instance)
62 libvlc_exception_t exception = new libvlc_exception_t();
63 this.instance = instance;
64 libvlc = jvlc.getLibvlc();
65 eventManager = libvlc.libvlc_media_player_event_manager(instance, exception);
68 public MediaPlayer(MediaDescriptor mediaDescriptor)
70 libvlc_exception_t exception = new libvlc_exception_t();
71 libvlc = mediaDescriptor.getLibvlc();
72 instance = libvlc.libvlc_media_player_new_from_media(mediaDescriptor.getInstance(), exception);
73 eventManager = libvlc.libvlc_media_player_event_manager(instance, exception);
74 this.mediaDescriptor = mediaDescriptor;
77 public MediaDescriptor getMediaDescriptor()
79 return mediaDescriptor;
84 libvlc_exception_t exception = new libvlc_exception_t();
85 libvlc.libvlc_media_player_play(instance, exception);
90 libvlc_exception_t exception = new libvlc_exception_t();
91 libvlc.libvlc_media_player_stop(instance, exception);
96 libvlc_exception_t exception = new libvlc_exception_t();
97 libvlc.libvlc_media_player_pause(instance, exception);
100 public long getLength()
102 libvlc_exception_t exception = new libvlc_exception_t();
103 return libvlc.libvlc_media_player_get_length(instance, exception);
106 public long getTime()
108 libvlc_exception_t exception = new libvlc_exception_t();
109 return libvlc.libvlc_media_player_get_time(instance, exception);
112 public void setTime(long time)
114 libvlc_exception_t exception = new libvlc_exception_t();
115 libvlc.libvlc_media_player_set_time(instance, time, exception);
118 public float getPosition()
120 libvlc_exception_t exception = new libvlc_exception_t();
121 return libvlc.libvlc_media_player_get_position(instance, exception);
124 public void setPosition(float position)
126 libvlc_exception_t exception = new libvlc_exception_t();
127 libvlc.libvlc_media_player_set_position(instance, position, exception);
130 public boolean willPlay()
132 libvlc_exception_t exception = new libvlc_exception_t();
133 return (libvlc.libvlc_media_player_will_play(instance, exception) == 1);
136 public float getRate()
138 libvlc_exception_t exception = new libvlc_exception_t();
139 return libvlc.libvlc_media_player_get_rate(instance, exception);
142 public void setRate(float rate)
144 libvlc_exception_t exception = new libvlc_exception_t();
145 libvlc.libvlc_media_player_set_rate(instance, rate, exception);
148 public boolean hasVideoOutput()
150 libvlc_exception_t exception = new libvlc_exception_t();
151 return (libvlc.libvlc_media_player_has_vout(instance, exception) == 1);
154 public float getFPS()
156 libvlc_exception_t exception = new libvlc_exception_t();
157 return libvlc.libvlc_media_player_get_fps(instance, exception);
160 public boolean isPlaying()
162 libvlc_exception_t exception = new libvlc_exception_t();
163 return libvlc.libvlc_media_player_is_playing(instance, exception) == 1 ? true : false;
166 public void addListener(final MediaPlayerListener listener)
168 MediaPlayerCallback callback = new MediaPlayerCallback(this, listener);
169 libvlc_exception_t exception = new libvlc_exception_t();
170 for (LibVlcEventType event : EnumSet.range(
171 LibVlcEventType.libvlc_MediaPlayerPlaying,
172 LibVlcEventType.libvlc_MediaPlayerTimeChanged))
174 libvlc.libvlc_event_attach(eventManager, event.ordinal(), callback, null, exception);
176 callbacks.add(callback);
179 public void setParent(Canvas canvas)
181 long drawable = Native.getComponentID(canvas);
182 libvlc_exception_t exception = new libvlc_exception_t();
183 if (Platform.isWindows())
185 libvlc.libvlc_media_player_set_hwnd(instance, drawable, exception);
189 libvlc.libvlc_media_player_set_xwindow(instance, drawable, exception);
197 protected void finalize() throws Throwable
203 public void release()
211 libvlc_exception_t exception = new libvlc_exception_t();
212 for (MediaPlayerCallback callback : callbacks)
214 for (LibVlcEventType event : EnumSet.range(
215 LibVlcEventType.libvlc_MediaPlayerPlaying,
216 LibVlcEventType.libvlc_MediaPlayerPositionChanged))
218 libvlc.libvlc_event_detach(eventManager, event.ordinal(), callback, null, exception);
221 libvlc.libvlc_media_player_release(instance);
226 * Returns the instance.
227 * @return the instance
229 LibVlcMediaPlayer getInstance()