xref: /trunk/main/avmedia/source/java/Player.java (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // UNO
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XComponentContext;
31 import com.sun.star.uno.AnyConverter;
32 import com.sun.star.uno.IQueryInterface;
33 import com.sun.star.lang.XInitialization;
34 
35 // awt
36 import com.sun.star.awt.*;
37 
38 // media
39 import com.sun.star.media.*;
40 
41 public class Player implements javax.media.ControllerListener,
42                                com.sun.star.lang.XServiceInfo,
43                                com.sun.star.media.XPlayer,
44                                com.sun.star.lang.XComponent
45 
46 
47 {
48     private com.sun.star.lang.XMultiServiceFactory maFactory;
49     private String maURL;
50     private javax.media.Player maPlayer;
51     private javax.media.GainControl maGainControl;
52     private boolean mbStarted = false;
53     private boolean mbLooping = false;
54 
55     // -------------------------------------------------------------------------
56 
57     public Player( com.sun.star.lang.XMultiServiceFactory aFactory,
58                    javax.media.Player aPlayer, String aURL )
59     {
60         maFactory = aFactory;
61         maURL = aURL;
62         maPlayer = aPlayer;
63         maPlayer.addControllerListener( this );
64         maGainControl = maPlayer.getGainControl();
65     }
66 
67     // -------------------------------------------------------------------------
68 
69     public synchronized void controllerUpdate( javax.media.ControllerEvent aEvt )
70     {
71         if( aEvt instanceof javax.media.EndOfMediaEvent ||
72             aEvt instanceof javax.media.StopAtTimeEvent )
73         {
74             mbStarted = false;
75 
76             if( mbLooping )
77             {
78                 setMediaTime( 0.0 );
79                 start();
80             }
81             else if( aEvt instanceof javax.media.EndOfMediaEvent )
82                 setMediaTime( getDuration() );
83         }
84     }
85 
86     // -----------
87     // - XPlayer -
88     // -----------
89 
90     public synchronized void start()
91     {
92         if( !mbStarted )
93         {
94             maPlayer.start();
95             mbStarted = true;
96         }
97     }
98 
99     // -------------------------------------------------------------------------
100 
101     public synchronized void stop()
102     {
103         if( mbStarted )
104         {
105             maPlayer.stop();
106             mbStarted = false;
107         }
108     }
109 
110     // -------------------------------------------------------------------------
111 
112     public synchronized boolean isPlaying()
113     {
114         return mbStarted;
115     }
116 
117     // -------------------------------------------------------------------------
118 
119     public synchronized double getDuration()
120     {
121         return maPlayer.getDuration().getSeconds();
122     }
123 
124     // -------------------------------------------------------------------------
125 
126     public synchronized void setMediaTime( double fTime )
127     {
128         if( fTime >= 0.0 && fTime <= getDuration() )
129             maPlayer.setMediaTime( new javax.media.Time( fTime ) );
130     }
131 
132     // -------------------------------------------------------------------------
133 
134     public synchronized double getMediaTime()
135     {
136         return maPlayer.getMediaTime().getSeconds();
137     }
138 
139     // -------------------------------------------------------------------------
140 
141     public synchronized void setStopTime( double fTime )
142     {
143         boolean bOldStarted = mbStarted;
144 
145         if( mbStarted )
146             stop();
147 
148         maPlayer.setStopTime( new javax.media.Time( fTime ) );
149 
150         if( bOldStarted )
151             start();
152     }
153 
154     // -------------------------------------------------------------------------
155 
156     public synchronized double getStopTime()
157     {
158         return maPlayer.getStopTime().getSeconds();
159     }
160 
161     // -------------------------------------------------------------------------
162 
163     public synchronized void setRate( double fRate )
164     {
165         boolean bOldStarted = mbStarted;
166 
167         if( mbStarted )
168             stop();
169 
170         maPlayer.setRate( (float) fRate );
171 
172         if( bOldStarted )
173             start();
174     }
175 
176     // -------------------------------------------------------------------------
177 
178     public synchronized double getRate()
179     {
180         return (double) maPlayer.getRate();
181     }
182 
183     // -------------------------------------------------------------------------
184 
185     public synchronized void setPlaybackLoop( boolean bSet )
186     {
187         mbLooping = bSet;
188     }
189 
190     // -------------------------------------------------------------------------
191 
192     public synchronized boolean isPlaybackLoop()
193     {
194         return mbLooping;
195     }
196 
197     // -------------------------------------------------------------------------
198 
199     public synchronized void setVolumeDB( short nVolumeDB )
200     {
201         if( maGainControl != null )
202             maGainControl.setDB( nVolumeDB );
203     }
204 
205     // -------------------------------------------------------------------------
206 
207     public synchronized short getVolumeDB()
208     {
209         return( maGainControl != null ? (short) maGainControl.getDB() : 0 );
210     }
211 
212     // -------------------------------------------------------------------------
213 
214     public synchronized void setMute( boolean bSet )
215     {
216         if( maGainControl != null )
217             maGainControl.setMute( bSet );
218     }
219 
220     // -------------------------------------------------------------------------
221 
222     public synchronized boolean isMute()
223     {
224         return( maGainControl != null ? maGainControl.getMute() : false );
225     }
226 
227     // -------------------------------------------------------------------------
228 
229     public synchronized com.sun.star.awt.Size getPreferredPlayerWindowSize()
230     {
231         java.awt.Component      aVisualComponent = maPlayer.getVisualComponent();
232         com.sun.star.awt.Size   aSize = new com.sun.star.awt.Size( 0, 0 );
233 
234         if( aVisualComponent != null )
235         {
236             java.awt.Dimension aDim = aVisualComponent.getPreferredSize();
237 
238             aSize.Width = Math.max( aDim.width, 0 );
239             aSize.Height = Math.max( aDim.height, 0 );
240         }
241 
242         return aSize;
243     }
244 
245     // -------------------------------------------------------------------------
246 
247     public synchronized com.sun.star.media.XPlayerWindow createPlayerWindow( java.lang.Object[] aArgs )
248     {
249         try
250         {
251             com.sun.star.media.XPlayerWindow xPlayerWindow = ( ( ( aArgs.length > 1 ) && ( AnyConverter.toInt( aArgs[ 0 ] ) > 0 ) ) ?
252                                                              new PlayerWindow( maFactory, aArgs, maPlayer ) :
253                                                              null );
254 
255             // check if it is a real player window (video window)
256             if( xPlayerWindow != null && xPlayerWindow.getZoomLevel() == com.sun.star.media.ZoomLevel.NOT_AVAILABLE )
257                 xPlayerWindow = null;
258 
259             return xPlayerWindow;
260         }
261         catch( com.sun.star.lang.IllegalArgumentException e )
262         {
263             return null;
264         }
265     }
266 
267     // -------------------------------------------------------------------------
268 
269     public synchronized com.sun.star.media.XFrameGrabber createFrameGrabber()
270     {
271         return( (com.sun.star.media.XFrameGrabber) new FrameGrabber( maFactory, maURL ) );
272     }
273 
274     // --------------
275     // - XComponent -
276     // --------------
277 
278     public synchronized void addEventListener( com.sun.star.lang.XEventListener xListener )
279     {
280     }
281 
282     // -------------------------------------------------------------------------
283 
284     public synchronized void removeEventListener( com.sun.star.lang.XEventListener xListener )
285     {
286     }
287 
288     // -------------------------------------------------------------------------
289 
290     public synchronized void dispose()
291     {
292         if( maPlayer != null )
293         {
294             maPlayer.stop();
295             maPlayer.close();
296             maPlayer = null;
297         }
298     }
299 
300     // ----------------
301     // - XServiceInfo -
302     // ----------------
303 
304     private static final String s_implName = "com.sun.star.comp.Player_Java";
305     private static final String s_serviceName = "com.sun.star.media.Player_Java";
306 
307     public synchronized String getImplementationName()
308     {
309         return s_implName;
310     }
311 
312     // -------------------------------------------------------------------------
313 
314     public synchronized String [] getSupportedServiceNames()
315     {
316         return new String [] { s_serviceName };
317     }
318 
319     // -------------------------------------------------------------------------
320 
321     public synchronized boolean supportsService( String serviceName )
322     {
323         return serviceName.equals( s_serviceName );
324     }
325 }
326