View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  /*
21   * This file was originally written by Mike Perham. 
22   * Modifications were made to fit it into XNap.
23   */
24  package org.xnap.util.launcher;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.lang.reflect.Method;
29  import java.net.URL;
30  
31  import org.xnap.XNap;
32  
33  /***
34   * JMF 2.1-based MP3 audio player.  Note that this class
35   * uses nothing but reflection to run the JMF code so that
36   * this class can be loaded without dependencies on the
37   * JMF jar being installed on the user's system.
38   *
39   * @see <A HREF="http://java.sun.com/products/java-media/jmf/index.html">JMF</A>
40   */
41  public class JMFPlayer extends DefaultPlayer
42  {
43  
44      //--- Constant(s) ---
45  
46      //--- Data field(s) ---
47  
48      private Object player = null;
49  
50      //--- Constructor(s) ---
51  
52      public JMFPlayer()
53      {
54  	super("jmf", XNap.tr("Java Media Player"), "");
55      }
56  
57      //--- Method(s) ---
58  
59      public boolean isEditable()
60      {
61  	return false;
62      }
63  
64      public boolean isEnabled()
65      {
66  	try {
67  	    Class.forName("javax.media.Manager");
68  	}
69  	catch (Throwable t) {
70  	    return false;
71  	}
72  
73  	return true;
74      }
75  
76      public void enqueue(File file) throws IOException
77      {
78  	stop();
79  
80  	URL url = file.toURL();
81  	try {
82  	    Class m = Class.forName("javax.media.Manager");
83  	    Method meth = m.getMethod("createRealizedPlayer", 
84  				      new Class[] { url.getClass() });
85  	    
86  	    // Player player = Manager.createRealizedPlayer(...);
87  	    Object o = meth.invoke(null, new Object[] {url});
88  	    
89  	    // player.start();
90  	    o.getClass().getMethod("start", null).invoke(o, null); 		
91  
92  	    player = o;
93  	}
94  	catch (Exception e) {
95  	    throw new IOException(XNap.tr("Could not instanciate class."));
96  	}
97      }
98  
99      /***
100      * Does nothing.
101      */
102     public void start() throws IOException
103     {
104     }
105 
106     public void stop()
107     {
108 	if (player != null) {
109 	    try {
110 		// just a shortcut
111 		Object o = player;
112 		o.getClass().getMethod("stop", null).invoke(o, null);
113 		o.getClass().getMethod("close", null).invoke(o, null);
114 		o.getClass().getMethod("deallocate", null).invoke(o, null);
115 		player = null;
116 	    }
117 	    catch (Exception ex) {
118 	    }
119 	}
120     }
121     
122 }