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