merge the formfield patch from ooo-build
[ooovba.git] / avmedia / source / java / Player.java
bloba5b1aacdddc4af7432677fa20daf368bb8cfde78
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Player.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // UNO
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.uno.XComponentContext;
34 import com.sun.star.uno.AnyConverter;
35 import com.sun.star.uno.IQueryInterface;
36 import com.sun.star.lang.XInitialization;
38 // awt
39 import com.sun.star.awt.*;
41 // media
42 import com.sun.star.media.*;
44 public class Player implements javax.media.ControllerListener,
45 com.sun.star.lang.XServiceInfo,
46 com.sun.star.media.XPlayer,
47 com.sun.star.lang.XComponent
51 private com.sun.star.lang.XMultiServiceFactory maFactory;
52 private String maURL;
53 private javax.media.Player maPlayer;
54 private javax.media.GainControl maGainControl;
55 private boolean mbStarted = false;
56 private boolean mbLooping = false;
58 // -------------------------------------------------------------------------
60 public Player( com.sun.star.lang.XMultiServiceFactory aFactory,
61 javax.media.Player aPlayer, String aURL )
63 maFactory = aFactory;
64 maURL = aURL;
65 maPlayer = aPlayer;
66 maPlayer.addControllerListener( this );
67 maGainControl = maPlayer.getGainControl();
70 // -------------------------------------------------------------------------
72 public synchronized void controllerUpdate( javax.media.ControllerEvent aEvt )
74 if( aEvt instanceof javax.media.EndOfMediaEvent ||
75 aEvt instanceof javax.media.StopAtTimeEvent )
77 mbStarted = false;
79 if( mbLooping )
81 setMediaTime( 0.0 );
82 start();
84 else if( aEvt instanceof javax.media.EndOfMediaEvent )
85 setMediaTime( getDuration() );
89 // -----------
90 // - XPlayer -
91 // -----------
93 public synchronized void start()
95 if( !mbStarted )
97 maPlayer.start();
98 mbStarted = true;
102 // -------------------------------------------------------------------------
104 public synchronized void stop()
106 if( mbStarted )
108 maPlayer.stop();
109 mbStarted = false;
113 // -------------------------------------------------------------------------
115 public synchronized boolean isPlaying()
117 return mbStarted;
120 // -------------------------------------------------------------------------
122 public synchronized double getDuration()
124 return maPlayer.getDuration().getSeconds();
127 // -------------------------------------------------------------------------
129 public synchronized void setMediaTime( double fTime )
131 if( fTime >= 0.0 && fTime <= getDuration() )
132 maPlayer.setMediaTime( new javax.media.Time( fTime ) );
135 // -------------------------------------------------------------------------
137 public synchronized double getMediaTime()
139 return maPlayer.getMediaTime().getSeconds();
142 // -------------------------------------------------------------------------
144 public synchronized void setStopTime( double fTime )
146 boolean bOldStarted = mbStarted;
148 if( mbStarted )
149 stop();
151 maPlayer.setStopTime( new javax.media.Time( fTime ) );
153 if( bOldStarted )
154 start();
157 // -------------------------------------------------------------------------
159 public synchronized double getStopTime()
161 return maPlayer.getStopTime().getSeconds();
164 // -------------------------------------------------------------------------
166 public synchronized void setRate( double fRate )
168 boolean bOldStarted = mbStarted;
170 if( mbStarted )
171 stop();
173 maPlayer.setRate( (float) fRate );
175 if( bOldStarted )
176 start();
179 // -------------------------------------------------------------------------
181 public synchronized double getRate()
183 return (double) maPlayer.getRate();
186 // -------------------------------------------------------------------------
188 public synchronized void setPlaybackLoop( boolean bSet )
190 mbLooping = bSet;
193 // -------------------------------------------------------------------------
195 public synchronized boolean isPlaybackLoop()
197 return mbLooping;
200 // -------------------------------------------------------------------------
202 public synchronized void setVolumeDB( short nVolumeDB )
204 if( maGainControl != null )
205 maGainControl.setDB( nVolumeDB );
208 // -------------------------------------------------------------------------
210 public synchronized short getVolumeDB()
212 return( maGainControl != null ? (short) maGainControl.getDB() : 0 );
215 // -------------------------------------------------------------------------
217 public synchronized void setMute( boolean bSet )
219 if( maGainControl != null )
220 maGainControl.setMute( bSet );
223 // -------------------------------------------------------------------------
225 public synchronized boolean isMute()
227 return( maGainControl != null ? maGainControl.getMute() : false );
230 // -------------------------------------------------------------------------
232 public synchronized com.sun.star.awt.Size getPreferredPlayerWindowSize()
234 java.awt.Component aVisualComponent = maPlayer.getVisualComponent();
235 com.sun.star.awt.Size aSize = new com.sun.star.awt.Size( 0, 0 );
237 if( aVisualComponent != null )
239 java.awt.Dimension aDim = aVisualComponent.getPreferredSize();
241 aSize.Width = Math.max( aDim.width, 0 );
242 aSize.Height = Math.max( aDim.height, 0 );
245 return aSize;
248 // -------------------------------------------------------------------------
250 public synchronized com.sun.star.media.XPlayerWindow createPlayerWindow( java.lang.Object[] aArgs )
254 com.sun.star.media.XPlayerWindow xPlayerWindow = ( ( ( aArgs.length > 1 ) && ( AnyConverter.toInt( aArgs[ 0 ] ) > 0 ) ) ?
255 new PlayerWindow( maFactory, aArgs, maPlayer ) :
256 null );
258 // check if it is a real player window (video window)
259 if( xPlayerWindow != null && xPlayerWindow.getZoomLevel() == com.sun.star.media.ZoomLevel.NOT_AVAILABLE )
260 xPlayerWindow = null;
262 return xPlayerWindow;
264 catch( com.sun.star.lang.IllegalArgumentException e )
266 return null;
270 // -------------------------------------------------------------------------
272 public synchronized com.sun.star.media.XFrameGrabber createFrameGrabber()
274 return( (com.sun.star.media.XFrameGrabber) new FrameGrabber( maFactory, maURL ) );
277 // --------------
278 // - XComponent -
279 // --------------
281 public synchronized void addEventListener( com.sun.star.lang.XEventListener xListener )
285 // -------------------------------------------------------------------------
287 public synchronized void removeEventListener( com.sun.star.lang.XEventListener xListener )
291 // -------------------------------------------------------------------------
293 public synchronized void dispose()
295 if( maPlayer != null )
297 maPlayer.stop();
298 maPlayer.close();
299 maPlayer = null;
303 // ----------------
304 // - XServiceInfo -
305 // ----------------
307 private static final String s_implName = "com.sun.star.comp.Player_Java";
308 private static final String s_serviceName = "com.sun.star.media.Player_Java";
310 public synchronized String getImplementationName()
312 return s_implName;
315 // -------------------------------------------------------------------------
317 public synchronized String [] getSupportedServiceNames()
319 return new String [] { s_serviceName };
322 // -------------------------------------------------------------------------
324 public synchronized boolean supportsService( String serviceName )
326 return serviceName.equals( s_serviceName );