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.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;
41 public class MediaPlayer
44 private final LibVlcMediaPlayer instance;
46 private final LibVlc libvlc;
48 private final LibVlcEventManager eventManager;
50 private List<MediaPlayerCallback> callbacks = new ArrayList<MediaPlayerCallback>();
52 private MediaDescriptor mediaDescriptor;
54 private volatile boolean released;
56 MediaPlayer(JVLC jvlc, LibVlcMediaPlayer instance)
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);
64 public MediaPlayer(MediaDescriptor mediaDescriptor)
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;
73 public MediaDescriptor getMediaDescriptor()
75 return mediaDescriptor;
80 libvlc_exception_t exception = new libvlc_exception_t();
81 libvlc.libvlc_media_player_play(instance, exception);
86 libvlc_exception_t exception = new libvlc_exception_t();
87 libvlc.libvlc_media_player_stop(instance, exception);
92 libvlc_exception_t exception = new libvlc_exception_t();
93 libvlc.libvlc_media_player_pause(instance, exception);
96 public long getLength()
98 libvlc_exception_t exception = new libvlc_exception_t();
99 return libvlc.libvlc_media_player_get_length(instance, exception);
102 public long getTime()
104 libvlc_exception_t exception = new libvlc_exception_t();
105 return libvlc.libvlc_media_player_get_time(instance, exception);
108 public void setTime(long time)
110 libvlc_exception_t exception = new libvlc_exception_t();
111 libvlc.libvlc_media_player_set_time(instance, time, exception);
114 public float getPosition()
116 libvlc_exception_t exception = new libvlc_exception_t();
117 return libvlc.libvlc_media_player_get_position(instance, exception);
120 public void setPosition(float position)
122 libvlc_exception_t exception = new libvlc_exception_t();
123 libvlc.libvlc_media_player_set_position(instance, position, exception);
126 public boolean willPlay()
128 libvlc_exception_t exception = new libvlc_exception_t();
129 return (libvlc.libvlc_media_player_will_play(instance, exception) == 1);
132 public float getRate()
134 libvlc_exception_t exception = new libvlc_exception_t();
135 return libvlc.libvlc_media_player_get_rate(instance, exception);
138 public void setRate(float rate)
140 libvlc_exception_t exception = new libvlc_exception_t();
141 libvlc.libvlc_media_player_set_rate(instance, rate, exception);
144 public boolean hasVideoOutput()
146 libvlc_exception_t exception = new libvlc_exception_t();
147 return (libvlc.libvlc_media_player_has_vout(instance, exception) == 1);
150 public float getFPS()
152 libvlc_exception_t exception = new libvlc_exception_t();
153 return libvlc.libvlc_media_player_get_fps(instance, exception);
156 public boolean isPlaying()
158 libvlc_exception_t exception = new libvlc_exception_t();
159 return libvlc.libvlc_media_player_is_playing(instance, exception) == 1? true : false;
162 public void addListener(final MediaPlayerListener listener)
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))
170 libvlc.libvlc_event_attach(eventManager, event.ordinal(), callback, null, exception);
172 callbacks.add(callback);
179 protected void finalize() throws Throwable
185 public void release()
193 libvlc_exception_t exception = new libvlc_exception_t();
194 for (MediaPlayerCallback callback : callbacks)
196 for (LibVlcEventType event : EnumSet.range(
197 LibVlcEventType.libvlc_MediaPlayerPlaying,
198 LibVlcEventType.libvlc_MediaPlayerPositionChanged))
200 libvlc.libvlc_event_detach(eventManager, event.ordinal(), callback, null, exception);
203 libvlc.libvlc_media_player_release(instance);
208 * Returns the instance.
209 * @return the instance
211 LibVlcMediaPlayer getInstance()